import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.util.HashMap; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class FormMapWithSetPlanes extends JFrame{ public JPanel Mainpanel; private JPanel pictureBoxPlane; private JPanel GroupBoxTools; private JComboBox ComboBoxSelectorMap; private JButton ButtonAddPlane; private JButton ButtonDeletePlane; private JButton ButtonShowStorage; private JButton ButtonShowMap; private JButton ButtonLeft; private JButton ButtonDown; private JButton ButtonUp; private JButton ButtonRight; private JTextField maskedTextBoxPosition; private JTextField textBoxNewMapName; private int picWidth=600; private int picHeight=500; private JButton ButtonAddMap; private JList ListBoxMaps; private JButton ButtonDeleteMap; private JPanel GroupBoxMaps; private JButton ButtonCheckDel; private MapWithSetPlanesGeneric _mapPlanesCollectionGeneric; private final HashMap _mapsDict = new HashMap<>(){{ put("Простая карта",new SimpleMap()); put("Вторая карта",new MyMap()); }}; private final MapsCollection _mapsCollection; public void UpdateWindow(BufferedImage bmp) { pictureBoxPlane.removeAll(); JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(pictureBoxPlane.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); imageWithMapAndObject.setIcon(new ImageIcon(bmp)); pictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); pictureBoxPlane.revalidate(); } private void ReloadMaps(){ int index = ListBoxMaps.getSelectedIndex(); DefaultListModel model1 = (DefaultListModel) ListBoxMaps.getModel(); model1.removeAllElements(); for(int i=0;i<_mapsCollection.Keys().size();i++) { model1.addElement(_mapsCollection.Keys().get(i)); } if (ListBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= ListBoxMaps.getModel().getSize())) { ListBoxMaps.setSelectedIndex(0); } else if (ListBoxMaps.getModel().getSize() > 0 && index > -1 && index < ListBoxMaps.getModel().getSize()) { ListBoxMaps.setSelectedIndex(index); } } private void ButtonMove_Click(String name) { Direction direction = Direction.None; switch (name) { case "buttonLeft": direction = Direction.Left; break; case "buttonUp": direction = Direction.Up; break; case "buttonRight": direction = Direction.Right; break; case "buttonDown": direction = Direction.Down; break; } UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(direction)); } public FormMapWithSetPlanes() { _mapsCollection = new MapsCollection(picWidth, picHeight); ComboBoxSelectorMap.removeAllItems(); for (String elem : _mapsDict.keySet()) { ComboBoxSelectorMap.addItem(elem); } try { Image img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowUp.jpg")); ButtonUp.setIcon(new ImageIcon(img)); img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowDown.jpg")); ButtonDown.setIcon(new ImageIcon(img)); img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowLeft.jpg")); ButtonLeft.setIcon(new ImageIcon(img)); img = ImageIO.read(FormPlane.class.getResource("/Resource/arrowRight.jpg")); ButtonRight.setIcon(new ImageIcon(img)); } catch (Exception ex) { System.out.println(ex); } ButtonAddPlane.addActionListener(e -> { if (ListBoxMaps.getSelectedIndex() == -1) { return; } FormPlane dialog = new FormPlane(); dialog.setSize(800, 600); dialog.setLocation(500, 200); dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); if (dialog.GetSelectedPlane() == null) return; DrawningObjectPlane Plane = new DrawningObjectPlane(dialog.GetSelectedPlane()); if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).addPlane(Plane)!=-1) { JOptionPane.showMessageDialog(null,"Объект добавлен"); UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); } }); ListBoxMaps.addListSelectionListener(e -> { if (ListBoxMaps.getSelectedIndex() == -1) return; UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); }); ButtonDeletePlane.addActionListener(e -> { if (maskedTextBoxPosition.getText().isEmpty()) { return; } int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); if(result==JOptionPane.NO_OPTION) { return; } int pos=Integer.parseInt(maskedTextBoxPosition.getText()); if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).removePlane(pos)!=null) { JOptionPane.showMessageDialog(null, "Объект удален"); UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } else { JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); } }); ButtonShowStorage.addActionListener(e -> { if (ListBoxMaps.getSelectedIndex() == -1) { return; } UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); }); ButtonShowMap.addActionListener(e -> { if (ListBoxMaps.getSelectedIndex() == -1) { return; } UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap()); }); ButtonUp.addActionListener(e -> ButtonMove_Click("buttonUp")); ButtonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft")); ButtonDown.addActionListener(e -> ButtonMove_Click("buttonDown")); ButtonRight.addActionListener(e -> ButtonMove_Click("buttonRight")); maskedTextBoxPosition.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if ( ((c < '0') || (c > '9')) || maskedTextBoxPosition.getText().length() >= 2) { e.consume(); } } }); ButtonAddMap.addActionListener(e -> { if (ComboBoxSelectorMap.getSelectedIndex() == -1 || textBoxNewMapName.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE); return; } if(!_mapsDict.containsKey(ComboBoxSelectorMap.getSelectedItem())) { JOptionPane.showMessageDialog(null,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE); } _mapsCollection.AddMap(textBoxNewMapName.getText(),_mapsDict.get(ComboBoxSelectorMap.getSelectedItem().toString())); ReloadMaps(); }); ButtonDeleteMap.addActionListener(e -> { if (ListBoxMaps.getSelectedIndex() == -1) { return; } if(JOptionPane.showConfirmDialog(null,"Удалить карту"+ListBoxMaps.getSelectedValue().toString()+"?","Удаление",JOptionPane.YES_NO_OPTION)==0) { _mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString()); ReloadMaps(); } }); ButtonCheckDel.addActionListener(e -> { if (ListBoxMaps.getSelectedIndex() == -1) { return; } DrawningObjectPlane Plane=_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).GetPlanesDeleted(); if(Plane==null) { JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE); return; } FormPlane formPlane=new FormPlane(Plane); formPlane.setSize(1000,800); formPlane.setVisible(true); }); } private void createUIComponents() { DefaultListModel dlm = new DefaultListModel(); ListBoxMaps = new JList(dlm); } }