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; public class FormMapWithSetPlanesGeneric { public JPanel MainPanel; private JPanel PictureBoxPlane; private JPanel ButtonGroupPanel; private JComboBox ComboBoxSelectorMap; private JButton ButtonLeft; private JButton ButtonDown; private JButton ButtonRight; private JButton ButtonUp; private JButton ButtonShowOnMap; private JButton ButtonShowStorage; private JButton ButtonAddPlane; private JTextField MaskedTextBoxPosition; private JButton ButtonRemovePlane; private JTextField TextBoxNewMapName; private JButton ButtonAddMap; private JList list1; private JButton ButtonDeleteMap; //объект от класса карты с набором объектов private MapWithSetPlanesGeneric _mapPlanesCollectionGeneric; //обновление отрисовки 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(); } public FormMapWithSetPlanesGeneric() { //загрузка изображений для кнопок try { Image img = ImageIO.read(getClass().getResource("resourses/Up.png")); ButtonUp.setIcon(new ImageIcon(img)); img = ImageIO.read(getClass().getResource("resourses/Left.png")); ButtonLeft.setIcon(new ImageIcon(img)); img = ImageIO.read(getClass().getResource("resourses/Down.png")); ButtonDown.setIcon(new ImageIcon(img)); img = ImageIO.read(getClass().getResource("resourses/Right.png")); ButtonRight.setIcon(new ImageIcon(img)); } catch (Exception ex) { System.out.println(ex.getMessage()); } //добавление самолёта ButtonAddPlane.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(_mapPlanesCollectionGeneric == null) { return; } FormPlane form = new FormPlane(); form.setSize(700,400); form.setVisible(true); form.setModal(true); DrawningObjectPlane plane = new DrawningObjectPlane(form.GetSelectedShip()); if(_mapPlanesCollectionGeneric.Add(plane) != -1) { JOptionPane.showMessageDialog(null,"Объект добавлен"); UpdateWindow(_mapPlanesCollectionGeneric.ShowSet()); } else { JOptionPane.showMessageDialog(null,"Не удалось добавить объект"); } } }); //удаление самолёта ButtonRemovePlane.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent 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(_mapPlanesCollectionGeneric.Delete(pos) != null) { JOptionPane.showMessageDialog(null,"Объект удалён"); UpdateWindow(_mapPlanesCollectionGeneric.ShowSet()); } else { JOptionPane.showMessageDialog(null,"Не удалось удалить объект"); } } }); //показ хранилища ButtonShowStorage.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(_mapPlanesCollectionGeneric == null) { return; } UpdateWindow(_mapPlanesCollectionGeneric.ShowSet()); } }); //показ карты ButtonShowOnMap.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (_mapPlanesCollectionGeneric == null) { return; } UpdateWindow(_mapPlanesCollectionGeneric.ShowOnMap()); } }); ButtonUp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(_mapPlanesCollectionGeneric == null) { return; } PictureBoxPlane.removeAll(); JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Up)))); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.revalidate(); PictureBoxPlane.repaint(); } }); ButtonLeft.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(_mapPlanesCollectionGeneric == null) { return; } PictureBoxPlane.removeAll(); JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Left)))); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.revalidate(); PictureBoxPlane.repaint(); } }); ButtonDown.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(_mapPlanesCollectionGeneric == null) { return; } PictureBoxPlane.removeAll(); JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Down)))); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.revalidate(); PictureBoxPlane.repaint(); } }); ButtonRight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(_mapPlanesCollectionGeneric == null) { return; } PictureBoxPlane.removeAll(); JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Right)))); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.revalidate(); PictureBoxPlane.repaint(); } }); //выпадающий список с вариантами карт ComboBoxSelectorMap.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { AbstractMap map = null; ComboBoxSelectorMap = (JComboBox)e.getSource(); String item = (String)ComboBoxSelectorMap.getSelectedItem(); switch(item) { case "Простая карта": map = new SimpleMap(); break; case "Буря в пустыне": map = new DesertStormMap(); break; case "Звёздные войны": map = new StarWarsMap(); break; } if(map != null) { _mapPlanesCollectionGeneric = new MapWithSetPlanesGeneric( PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), map); } else { _mapPlanesCollectionGeneric = null; } } }); 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(); } } }); } }