From 571b302e4be59df0352539b90493b7151e3fd962 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Sun, 6 Nov 2022 00:34:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC=D1=8B,=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D0=B8=D1=84=D0=B8=D0=BA=D0=BA=D0=B0=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?FormPlane=20=D0=B8=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Generic.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project/src/FormMapWithSetPlanes.java | 17 -- ....form => FormMapWithSetPlanesGeneric.form} | 4 +- Project/src/FormMapWithSetPlanesGeneric.java | 271 ++++++++++++++++++ Project/src/FormPlane.form | 82 ++++-- Project/src/FormPlane.java | 125 ++++++-- Project/src/MapWithSetPlanesGeneric.java | 12 +- 6 files changed, 428 insertions(+), 83 deletions(-) delete mode 100644 Project/src/FormMapWithSetPlanes.java rename Project/src/{FormMapWithSetPlanes.form => FormMapWithSetPlanesGeneric.form} (99%) create mode 100644 Project/src/FormMapWithSetPlanesGeneric.java diff --git a/Project/src/FormMapWithSetPlanes.java b/Project/src/FormMapWithSetPlanes.java deleted file mode 100644 index d6d4ea5..0000000 --- a/Project/src/FormMapWithSetPlanes.java +++ /dev/null @@ -1,17 +0,0 @@ -import javax.swing.*; - -public class FormMapWithSetPlanes { - private 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 __TextField; - private JButton ButtonRemovePlane; -} diff --git a/Project/src/FormMapWithSetPlanes.form b/Project/src/FormMapWithSetPlanesGeneric.form similarity index 99% rename from Project/src/FormMapWithSetPlanes.form rename to Project/src/FormMapWithSetPlanesGeneric.form index 3ca34c9..dd21465 100644 --- a/Project/src/FormMapWithSetPlanes.form +++ b/Project/src/FormMapWithSetPlanesGeneric.form @@ -1,5 +1,5 @@ -
+ @@ -133,7 +133,7 @@ - + diff --git a/Project/src/FormMapWithSetPlanesGeneric.java b/Project/src/FormMapWithSetPlanesGeneric.java new file mode 100644 index 0000000..75b63af --- /dev/null +++ b/Project/src/FormMapWithSetPlanesGeneric.java @@ -0,0 +1,271 @@ +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 { + private 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 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(1000,700); + 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.ShowSet()); + } + }); + + 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(); + } + } + }); + } +} diff --git a/Project/src/FormPlane.form b/Project/src/FormPlane.form index 9f4bf18..a801c1a 100644 --- a/Project/src/FormPlane.form +++ b/Project/src/FormPlane.form @@ -1,16 +1,16 @@ - + - + - + @@ -23,7 +23,7 @@ - + @@ -36,36 +36,28 @@ - + + + + + - - - - + + + + - - - - - - - - - - - - - + @@ -73,7 +65,7 @@ - + @@ -86,7 +78,7 @@ - + @@ -97,14 +89,46 @@ - + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Project/src/FormPlane.java b/Project/src/FormPlane.java index 27b4444..de4e378 100644 --- a/Project/src/FormPlane.java +++ b/Project/src/FormPlane.java @@ -1,16 +1,12 @@ import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; +import java.awt.event.*; +import java.awt.image.BufferedImage; import java.util.Random; -public class FormPlane +public class FormPlane extends JDialog { - protected DrawingPlane plane; - public JPanel MainPanel; private JButton ButtonCreate; private JButton ButtonLeft; @@ -19,12 +15,54 @@ public class FormPlane private JButton ButtonUp; private JToolBar StatusStrip; private JPanel PictureBoxPlane; + private JButton ButtonCreateModif; + private JButton ButtonSelectPlane; private JLabel LabelSpeed = new JLabel(); private JLabel LabelWeight = new JLabel(); private JLabel LabelColor = new JLabel(); + protected DrawingPlane plane; + protected DrawingPlane _selectedPlane; + + public DrawingPlane GetSelectedShip() + { + return _selectedPlane; + } + + //метод отрисовки + public void Draw(DrawingPlane _plane) { + PictureBoxPlane.removeAll(); + BufferedImage bmp = new BufferedImage(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + + gr.setColor(new Color(238, 238, 238)); + gr.fillRect(0, 0, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + + if (plane.GetPlane() != null) { + _plane.DrawTransport(gr); + JLabel imageOfLogo = new JLabel(); + imageOfLogo.setPreferredSize(PictureBoxPlane.getSize()); + imageOfLogo.setMinimumSize(new Dimension(1, 1)); + imageOfLogo.setIcon(new ImageIcon(bmp)); + PictureBoxPlane.add(imageOfLogo, BorderLayout.CENTER); + } + + validate(); + } + + //создание всплывающего окна public FormPlane() { + super(new Frame("Airbus")); + CreateWindow(); + setModal(true); + getContentPane().add(MainPanel); + } + + public void CreateWindow() + { + setModal(true); + //создание строки отображения скорости, веса и цвета объекта Box LableBox = Box.createHorizontalBox(); LableBox.setMinimumSize(new Dimension(1, 20)); @@ -49,30 +87,60 @@ public class FormPlane System.out.println(ex.getMessage()); } + _selectedPlane = plane; ButtonCreate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Random rnd = new Random(); + Color colorSimple = JColorChooser.showDialog(null, "Выберите цвет", null); + plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + colorSimple); plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + LabelSpeed.setText("Скорость: " + plane.GetPlane().GetSpeed() + " "); LabelWeight.setText("Вес: " + plane.GetPlane().GetWeight() + " "); LabelColor.setText("Цвет: r = " + plane.GetPlane().GetColor().getRed() + " g = " + plane.GetPlane().GetColor().getGreen() + " b = " + plane.GetPlane().GetColor().getBlue()); - Draw(); + + Draw(plane); } }); - //обновление размеров формы - MainPanel.addComponentListener(new ComponentAdapter() { + ButtonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + Color colorSimple = JColorChooser.showDialog(null, "Выберите цвет", null); + Color colorModif = JColorChooser.showDialog(null, "Выберите цвет", null); + + plane = new DrawingAirbus(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + colorSimple, colorModif, rnd.nextBoolean(), rnd.nextBoolean()); + plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), + PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + + LabelSpeed.setText("Скорость: " + plane.GetPlane().GetSpeed() + " "); + LabelWeight.setText("Вес: " + plane.GetPlane().GetWeight() + " "); + LabelColor.setText("Цвет: r = " + plane.GetPlane().GetColor().getRed() + " g = " + plane.GetPlane().GetColor().getGreen() + + " b = " + plane.GetPlane().GetColor().getBlue()); + + Draw(plane); + } + }); + + PictureBoxPlane.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { super.componentResized(e); - plane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); - Draw(); + + if(plane != null) + { + plane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + PictureBoxPlane.revalidate(); + Draw(plane); + } } }); @@ -80,7 +148,8 @@ public class FormPlane @Override public void actionPerformed(ActionEvent e) { plane.MoveTransport(Direction.Up); - Draw(); + PictureBoxPlane.revalidate(); + Draw(plane); } }); @@ -88,7 +157,8 @@ public class FormPlane @Override public void actionPerformed(ActionEvent e) { plane.MoveTransport(Direction.Left); - Draw(); + PictureBoxPlane.revalidate(); + Draw(plane); } }); @@ -96,7 +166,8 @@ public class FormPlane @Override public void actionPerformed(ActionEvent e) { plane.MoveTransport(Direction.Down); - Draw(); + PictureBoxPlane.revalidate(); + Draw(plane); } }); @@ -104,18 +175,18 @@ public class FormPlane @Override public void actionPerformed(ActionEvent e) { plane.MoveTransport(Direction.Right); - Draw(); + PictureBoxPlane.revalidate(); + Draw(plane); + } + }); + + //кнопка подтверждение выбора + ButtonSelectPlane.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _selectedPlane = plane; + dispose(); } }); } - - public void Draw() - { - if(plane.GetPlane() == null) - { - return; - } - - plane.DrawTransport(PictureBoxPlane.getGraphics()); - } } diff --git a/Project/src/MapWithSetPlanesGeneric.java b/Project/src/MapWithSetPlanesGeneric.java index 8050679..1a56b30 100644 --- a/Project/src/MapWithSetPlanesGeneric.java +++ b/Project/src/MapWithSetPlanesGeneric.java @@ -32,19 +32,15 @@ public class MapWithSetPlanesGeneric map, T plane) + public int Add(T plane) { - return map._setPlanes.Insert(plane); + return _setPlanes.Insert(plane); } - //перегрузка оператора вычитания - public static T operator -(MapWithSetPlanesGeneric map, int position) + public T Delete(int position) { - return map._setPlanes.Remove(position); + return _setPlanes.Remove(position); } - */ //вывод всего набора объектов public BufferedImage ShowSet()