From 1934acdcae4ea499f72900fac395a92bdf914b73 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Wed, 30 Nov 2022 14:15:45 +0400 Subject: [PATCH 1/5] generic classes --- AbstractMap.java | 2 +- MapWithSetPlanesGeneric.java | 141 +++++++++++++++++++++++++++++++++++ SetPlanesGeneric.java | 44 +++++++++++ 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 MapWithSetPlanesGeneric.java create mode 100644 SetPlanesGeneric.java diff --git a/AbstractMap.java b/AbstractMap.java index cf2e1b3..2f797e6 100644 --- a/AbstractMap.java +++ b/AbstractMap.java @@ -83,7 +83,7 @@ public abstract class AbstractMap { return true; } - private BufferedImage DrawMapWithObject() + BufferedImage DrawMapWithObject() { BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); if (_drawningObject == null || _map == null) diff --git a/MapWithSetPlanesGeneric.java b/MapWithSetPlanesGeneric.java new file mode 100644 index 0000000..d49924b --- /dev/null +++ b/MapWithSetPlanesGeneric.java @@ -0,0 +1,141 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetPlanesGeneric +{ + private int _pictureWidth; + private int _pictureHeight; + private int _placeSizeWidth = 210; + private int _placeSizeHeight = 90; + private SetPlanesGeneric _setPlanes; + private U _map; + + public MapWithSetPlanesGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setPlanes = new SetPlanesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int addPlane(T planes) + { + return _setPlanes.Insert(planes); + } + + public T removePlane(int position) + { + return _setPlanes.Remove(position); + } + + public BufferedImage ShowSet() + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D gr = (Graphics2D) img.getGraphics(); + DrawBackground(gr); + DrawPlanes(gr); + return img; + } + + public BufferedImage ShowOnMap() + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = (Graphics2D) img.getGraphics(); + Shaking(); + for (int i = 0; i < _setPlanes.Count(); i++) + { + var car = _setPlanes.Get(i); + if (car != null) + { + _map.CreateMap(_pictureWidth, _pictureHeight, car); + _map.DrawMapWithObject(); + return img; + } + } + return img; + } + + public BufferedImage MoveObject(Direction direction) + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + if (_map != null) + { + _map.MoveObject(direction); + _map.DrawMapWithObject(); + } + return img; + } + + private void Shaking() + { + int j = _setPlanes.Count() - 1; + for (int i = 0; i < _setPlanes.Count(); i++) + { + if (_setPlanes.Get(i) == null) + { + for (; j > i; j--) + { + var plane = _setPlanes.Get(j); + if (plane != null) + { + _setPlanes.Insert(plane, i); + _setPlanes.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics g) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.GRAY); + g.fillRect(0, 0, _pictureWidth, _pictureHeight); + g2d.setColor(Color.WHITE); + for (int y = 0; y < _pictureHeight; y+=80) + { + g.fillRect(_pictureWidth / 2 - 10, y, 20, 70); + } + int lastLine = 0; + for (int y = 50; y < _pictureHeight; y += _pictureHeight/3 - 50) + { + g.fillRect(_pictureWidth / 3 - 10, y, 20, 70); + g.fillRect(_pictureWidth - _pictureWidth / 3 - 10, y, 20, 70); + lastLine = y; + } + g.fillRect(_pictureWidth / 3 - 40, lastLine, 20, 70); + g.fillRect(_pictureWidth - _pictureWidth / 3 + 20, lastLine, 20, 70); + + g2d.setColor(Color.BLACK); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + + private void DrawPlanes(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _setPlanes.Count(); i++) + { + if(_setPlanes.Get(i) != null) + { + _setPlanes.Get(i).SetObject((width - i % width - 1) * _placeSizeWidth,(height - i / width - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setPlanes.Get(i).DrawningObject(g); + } + } + } +} diff --git a/SetPlanesGeneric.java b/SetPlanesGeneric.java new file mode 100644 index 0000000..7eb7932 --- /dev/null +++ b/SetPlanesGeneric.java @@ -0,0 +1,44 @@ +public class SetPlanesGeneric { + private T[] _places; + + public int Count() { + return _places.length; + } + + public SetPlanesGeneric(int count) { + _places = (T[]) (new Object[count]); + } + + public int Insert(T plane) { + for (int i = 0; i < _places.length; i++) { + if (_places[i] == null) { + _places[i] = plane; + return i; + } + } + return -1; + } + + public int Insert(T plane, int position) { + int index = position; + + while (_places[index] != null && index < _places.length) index++; + + if (index == _places.length) return -1; + for (int i = index; i > position; --i) _places[i] = _places[i - 1]; + + _places[position] = plane; + return position; + } + + public T Remove(int position) { + if (position >= _places.length) return null; + T res = _places[position]; + _places[position] = null; + return res; + } + + public T Get(int position) { + return _places[position]; + } +} \ No newline at end of file -- 2.25.1 From 2c9b5d5c13c2d6552bc784c486d22cb5be56b956 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Wed, 30 Nov 2022 19:25:43 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D1=81=D0=BE=D0=B1=D1=80=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormMap.java | 25 ++--- FormMapWithSetPlanes.form | 156 ++++++++++++++++++++++++++++ FormMapWithSetPlanes.java | 192 +++++++++++++++++++++++++++++++++++ FormPlane.form | 24 +++-- FormPlane.java | 21 +++- Main.java | 2 +- MapWithSetPlanesGeneric.java | 15 ++- 7 files changed, 400 insertions(+), 35 deletions(-) create mode 100644 FormMapWithSetPlanes.form create mode 100644 FormMapWithSetPlanes.java diff --git a/FormMap.java b/FormMap.java index f25dbdf..9bcb45e 100644 --- a/FormMap.java +++ b/FormMap.java @@ -106,20 +106,17 @@ public class FormMap extends JFrame{ ButtonRight.addActionListener(e -> { ButtonMove_Click("buttonRight"); }); - comboBoxSelector.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - comboBoxSelector = (JComboBox)e.getSource(); - String item = (String)comboBoxSelector.getSelectedItem(); - switch (item) { - case "Простая карта" -> { - _abstractMap = new SimpleMap(); - break; - } - case "Вторая карта" -> { - _abstractMap = new MyMap(); - break; - } + comboBoxSelector.addActionListener(e -> { + comboBoxSelector = (JComboBox)e.getSource(); + String item = (String)comboBoxSelector.getSelectedItem(); + switch (item) { + case "Простая карта" -> { + _abstractMap = new SimpleMap(); + break; + } + case "Вторая карта" -> { + _abstractMap = new MyMap(); + break; } } }); diff --git a/FormMapWithSetPlanes.form b/FormMapWithSetPlanes.form new file mode 100644 index 0000000..5173ec5 --- /dev/null +++ b/FormMapWithSetPlanes.form @@ -0,0 +1,156 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormMapWithSetPlanes.java b/FormMapWithSetPlanes.java new file mode 100644 index 0000000..1e26a4f --- /dev/null +++ b/FormMapWithSetPlanes.java @@ -0,0 +1,192 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; + +public class FormMapWithSetPlanes extends JFrame { + + private MapWithSetPlanesGeneric _mapPlanesCollectionGeneric; + + public JPanel Mainpanel; + private JButton buttonAdd; + private JComboBox comboBoxSelectorMap; + private JTextField textBoxPosition; + private JButton buttonRemove; + private JButton buttonShowStorage; + private JButton buttonShowOnMap; + private JButton buttonUp; + private JButton buttonLeft; + private JButton buttonRight; + private JButton buttonDown; + private JPanel PictureBox; + private JPanel groupBox; + AbstractMap _abstractMap; + + private JFrame getFrame() { + JFrame frame = new JFrame(); + frame.setVisible(false); + frame.setBounds(300, 100, 800, 600); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + return frame; + } + + JFrame jFrame = getFrame(); + + private void ButtonMove_Click(String name) + { + if (_mapPlanesCollectionGeneric == null) return; + 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; + } + PictureBox.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject(direction))); + PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + } + + public FormMapWithSetPlanes() { + comboBoxSelectorMap.addItem("Простая карта"); + comboBoxSelectorMap.addItem("Вторая карта"); + try { + Image img = ImageIO.read(FormMap.class.getResource("/Resource/arrowUp.jpg")); + buttonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormMap.class.getResource("/Resource/arrowDown.jpg")); + buttonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormMap.class.getResource("/Resource/arrowLeft.jpg")); + buttonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormMap.class.getResource("/Resource/arrowRight.jpg")); + buttonRight.setIcon(new ImageIcon(img)); + } catch (Exception ex) { + System.out.println(ex); + } + + comboBoxSelectorMap.addActionListener(e -> { + AbstractMap map = null; + switch (comboBoxSelectorMap.getSelectedItem().toString()) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Вторая карта": + map = new MyMap(); + break; + } + if (map != null) + { + _mapPlanesCollectionGeneric = new MapWithSetPlanesGeneric(PictureBox.getWidth(), PictureBox.getHeight(), map); + } + else + { + _mapPlanesCollectionGeneric = null; + } + }); + buttonAdd.addActionListener(e -> { + if (_mapPlanesCollectionGeneric == null) + { + 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 (_mapPlanesCollectionGeneric.addPlane(plane) != -1) + { + JOptionPane.showMessageDialog(jFrame, "Объект добавлен"); + PictureBox.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(PictureBox.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowSet())); + PictureBox.add(imageOfShip,BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + } + else + { + JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект"); + } + }); + buttonRemove.addActionListener(e -> { + if(_mapPlanesCollectionGeneric == null) return; + + String text = textBoxPosition.getText(); + if(text.isEmpty()) return; + + if(JOptionPane.showConfirmDialog(jFrame, "Вы действительно хотите удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return; + + try { + Integer.parseInt(text); + } + catch (Exception ex){ + return; + } + + int pos = Integer.parseInt(text); + if (_mapPlanesCollectionGeneric.removePlane(pos) != null) + { + JOptionPane.showMessageDialog(jFrame, "Объект удален"); + PictureBox.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(PictureBox.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowSet())); + PictureBox.add(imageOfShip,BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + } + else + { + JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект"); + } + }); + buttonShowStorage.addActionListener(e -> { + if (_mapPlanesCollectionGeneric == null) return; + PictureBox.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(PictureBox.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowSet())); + PictureBox.add(imageOfShip,BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + }); + buttonShowOnMap.addActionListener(e -> { + if (_mapPlanesCollectionGeneric == null) return; + PictureBox.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(PictureBox.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowOnMap())); + PictureBox.add(imageOfShip,BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + }); + buttonUp.addActionListener(e -> ButtonMove_Click("buttonUp")); + buttonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft")); + buttonDown.addActionListener(e -> ButtonMove_Click("buttonDown")); + buttonRight.addActionListener(e -> ButtonMove_Click("buttonRight")); + } +} diff --git a/FormPlane.form b/FormPlane.form index 2426924..b0deb1b 100644 --- a/FormPlane.form +++ b/FormPlane.form @@ -1,6 +1,6 @@
- + @@ -13,7 +13,7 @@ - + @@ -25,7 +25,7 @@ - + @@ -37,7 +37,7 @@ - + @@ -50,7 +50,7 @@ - + @@ -72,7 +72,7 @@ - + @@ -80,7 +80,7 @@ - + @@ -98,7 +98,15 @@ - + + + + + + + + + diff --git a/FormPlane.java b/FormPlane.java index ac34cbb..8ef35b8 100644 --- a/FormPlane.java +++ b/FormPlane.java @@ -8,7 +8,7 @@ import java.util.Random; import static java.lang.Boolean.parseBoolean; -public class FormPlane extends JFrame{ +public class FormPlane extends JDialog{ public JPanel Mainpanel; private JButton ButtonCreate; private JButton ButtonLeft; @@ -19,9 +19,14 @@ public class FormPlane extends JFrame{ private JPanel PictureBox; private JToolBar StatusStrip; private JButton ButtonModif; + private JButton ButtonSelectPlane; private final JLabel JLabelSpeed = new JLabel(); private final JLabel JLabelWeight = new JLabel(); private final JLabel JLabelColor = new JLabel(); + private DrawningPlane SelectedPlane; + public DrawningPlane GetSelectedPlane() { + return SelectedPlane; + } public void Draw() { PictureBox.removeAll(); BufferedImage bmp = new BufferedImage(PictureBox.getWidth(), PictureBox.getHeight(),BufferedImage.TYPE_INT_RGB); @@ -46,6 +51,7 @@ public class FormPlane extends JFrame{ JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " ")); } public FormPlane() { + add(Mainpanel); Box LabelBox = Box.createHorizontalBox(); LabelBox.setMinimumSize(new Dimension(1, 20)); LabelBox.add(JLabelSpeed); @@ -66,13 +72,16 @@ public class FormPlane extends JFrame{ } ButtonCreate.addActionListener(e -> { Random random = new Random(); - _plane = new DrawningPlane(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + Color color = JColorChooser.showDialog(null, "Цвет", null); + _plane = new DrawningPlane(random.nextInt(100, 300),random.nextInt(1000, 2000),color); SetData(); Draw(); }); ButtonModif.addActionListener(e -> { Random random = new Random(); - _plane = new DrawningAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); + Color first = JColorChooser.showDialog(null, "Цвет", null); + Color second = JColorChooser.showDialog(null, "Цвет", null); + _plane = new DrawningAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), first, second, random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); SetData(); Draw(); }); @@ -106,5 +115,11 @@ public class FormPlane extends JFrame{ PictureBox.revalidate(); Draw(); }); + ButtonSelectPlane.addActionListener(e -> { + SelectedPlane = _plane; + setVisible(false); + dispose(); + }); } + } diff --git a/Main.java b/Main.java index f7c77d5..f38f398 100644 --- a/Main.java +++ b/Main.java @@ -4,7 +4,7 @@ public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Самолёт"); - frame.setContentPane(new FormMap().Mainpanel); + frame.setContentPane(new FormMapWithSetPlanes().Mainpanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(500, 200); frame.pack(); diff --git a/MapWithSetPlanesGeneric.java b/MapWithSetPlanesGeneric.java index d49924b..09c9602 100644 --- a/MapWithSetPlanesGeneric.java +++ b/MapWithSetPlanesGeneric.java @@ -41,20 +41,18 @@ public class MapWithSetPlanesGeneric Date: Wed, 30 Nov 2022 19:29:28 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=B5=D1=89=D0=B5=20=D1=87=D1=83=D1=82?= =?UTF-8?q?=D1=8C=20=D1=87=D1=83=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormMap.form | 121 ------------------------------------ FormMap.java | 125 -------------------------------------- FormMapWithSetPlanes.form | 4 +- FormMapWithSetPlanes.java | 8 +-- 4 files changed, 6 insertions(+), 252 deletions(-) delete mode 100644 FormMap.form delete mode 100644 FormMap.java diff --git a/FormMap.form b/FormMap.form deleted file mode 100644 index ff5bafc..0000000 --- a/FormMap.form +++ /dev/null @@ -1,121 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/FormMap.java b/FormMap.java deleted file mode 100644 index 9bcb45e..0000000 --- a/FormMap.java +++ /dev/null @@ -1,125 +0,0 @@ -import javax.imageio.ImageIO; -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.image.BufferedImage; -import java.util.Random; - -public class FormMap extends JFrame{ - public JPanel Mainpanel; - private JButton ButtonCreate; - private JButton ButtonLeft; - private JButton ButtonUp; - private JButton ButtonDown; - private JButton ButtonRight; - private JPanel PictureBox; - private JToolBar StatusStrip; - private JButton ButtonModif; - private JComboBox comboBoxSelector; - private final JLabel JLabelSpeed = new JLabel(); - private final JLabel JLabelWeight = new JLabel(); - private final JLabel JLabelColor = new JLabel(); - private AbstractMap _abstractMap; - - private void ButtonMove_Click(String name) - { - Direction dir = Direction.None; - switch (name) - { - case "buttonLeft": - dir = Direction.Left; - break; - case "buttonUp": - dir = Direction.Up; - break; - case "buttonRight": - dir = Direction.Right; - break; - case "buttonDown": - dir = Direction.Down; - break; - } - PictureBox.removeAll(); - JLabel imageWithMapAndObject = new JLabel(); - imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); - imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(dir))); - PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); - PictureBox.revalidate(); - PictureBox.repaint(); - } - - private void SetData(DrawningPlane _plane){ - PictureBox.removeAll(); - JLabelSpeed.setText("Cкорость: " + _plane.GetPlane().GetSpeed() + " "); - JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " "); - JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " ")); - JLabel imageWithMapAndObject = new JLabel(); - imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); - imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.CreateMap(PictureBox.getWidth(),PictureBox.getHeight(), new DrawningObjectPlane(_plane)))); - PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); - PictureBox.revalidate(); - } - public FormMap() { - comboBoxSelector.addItem("Простая карта"); - comboBoxSelector.addItem("Вторая карта"); - _abstractMap = new SimpleMap(); - Box LabelBox = Box.createHorizontalBox(); - LabelBox.setMinimumSize(new Dimension(1, 20)); - LabelBox.add(JLabelSpeed); - LabelBox.add(JLabelWeight); - LabelBox.add(JLabelColor); - StatusStrip.add(LabelBox); - try { - Image img = ImageIO.read(FormMap.class.getResource("/Resource/arrowUp.jpg")); - ButtonUp.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMap.class.getResource("/Resource/arrowDown.jpg")); - ButtonDown.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMap.class.getResource("/Resource/arrowLeft.jpg")); - ButtonLeft.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMap.class.getResource("/Resource/arrowRight.jpg")); - ButtonRight.setIcon(new ImageIcon(img)); - } catch (Exception ex) { - System.out.println(ex); - } - ButtonCreate.addActionListener(e -> { - Random random = new Random(); - var _plane = new DrawningPlane(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); - SetData(_plane); - }); - ButtonModif.addActionListener(e -> { - Random random = new Random(); - var _plane = new DrawningAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); - SetData(_plane); - }); - ButtonUp.addActionListener(e -> { - ButtonMove_Click("buttonUp"); - }); - ButtonDown.addActionListener(e -> { - ButtonMove_Click("buttonDown"); - }); - ButtonLeft.addActionListener(e -> { - ButtonMove_Click("buttonLeft"); - }); - ButtonRight.addActionListener(e -> { - ButtonMove_Click("buttonRight"); - }); - comboBoxSelector.addActionListener(e -> { - comboBoxSelector = (JComboBox)e.getSource(); - String item = (String)comboBoxSelector.getSelectedItem(); - switch (item) { - case "Простая карта" -> { - _abstractMap = new SimpleMap(); - break; - } - case "Вторая карта" -> { - _abstractMap = new MyMap(); - break; - } - } - }); - - } -} diff --git a/FormMapWithSetPlanes.form b/FormMapWithSetPlanes.form index 5173ec5..f556201 100644 --- a/FormMapWithSetPlanes.form +++ b/FormMapWithSetPlanes.form @@ -31,7 +31,7 @@ - + @@ -47,7 +47,7 @@ - + diff --git a/FormMapWithSetPlanes.java b/FormMapWithSetPlanes.java index 1e26a4f..aa37fe1 100644 --- a/FormMapWithSetPlanes.java +++ b/FormMapWithSetPlanes.java @@ -64,13 +64,13 @@ public class FormMapWithSetPlanes extends JFrame { comboBoxSelectorMap.addItem("Простая карта"); comboBoxSelectorMap.addItem("Вторая карта"); try { - Image img = ImageIO.read(FormMap.class.getResource("/Resource/arrowUp.jpg")); + Image img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowUp.jpg")); buttonUp.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMap.class.getResource("/Resource/arrowDown.jpg")); + img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowDown.jpg")); buttonDown.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMap.class.getResource("/Resource/arrowLeft.jpg")); + img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowLeft.jpg")); buttonLeft.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMap.class.getResource("/Resource/arrowRight.jpg")); + img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowRight.jpg")); buttonRight.setIcon(new ImageIcon(img)); } catch (Exception ex) { System.out.println(ex); -- 2.25.1 From 176c2b9601dd4df17e98bb0b29a120bf4847d2a3 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Tue, 13 Dec 2022 17:02:16 +0400 Subject: [PATCH 4/5] third complete --- DrawningAirbus.java | 6 +++ DrawningEntities.java | 57 ++++++++++++++++++++ DrawningPlane.java | 5 ++ FormMapWithSetPlanes.form | 26 ++++++--- FormParam.form | 62 +++++++++++++++++++++ FormParam.java | 111 ++++++++++++++++++++++++++++++++++++++ Main.java | 3 +- 7 files changed, 261 insertions(+), 9 deletions(-) create mode 100644 DrawningEntities.java create mode 100644 FormParam.form create mode 100644 FormParam.java diff --git a/DrawningAirbus.java b/DrawningAirbus.java index 98c9a28..0f878ac 100644 --- a/DrawningAirbus.java +++ b/DrawningAirbus.java @@ -7,6 +7,12 @@ public class DrawningAirbus extends DrawningPlane{ super(speed, weight, bodyColor, 140, 70); Plane = new EntityAirbus(speed, weight, bodyColor, dopColor, bodyKit, wing, sportLine); } + + protected DrawningAirbus(EntityPlane plane, IDrawningIlluminator illum){ + super(plane,illum); + Plane = plane; + } + @Override public void DrawTransport(Graphics g) { diff --git a/DrawningEntities.java b/DrawningEntities.java new file mode 100644 index 0000000..2603137 --- /dev/null +++ b/DrawningEntities.java @@ -0,0 +1,57 @@ +import java.util.Random; + +public class DrawningEntities +{ + public T[] _entities; + public U[] _illuminator; + int entitiesCount = 0; + int illumCount = 0; + String indx; + String indy; + + public DrawningEntities(int countE,int countI){ + _entities = (T[]) new EntityPlane[countE]; + _illuminator = (U[]) new IDrawningIlluminator[countI]; + } + + public int Insert(T plane){ + if(entitiesCount < _entities.length){ + _entities[entitiesCount] = plane; + entitiesCount++; + return entitiesCount - 1; + } + return -1; + } + + public int Insert(U illuminator){ + if(illumCount < _illuminator.length){ + _illuminator[illumCount] = illuminator; + illumCount++; + return illumCount - 1; + } + return -1; + } + + public void SetIndexs(int ind1, int ind2) + { + indx=Integer.toString(ind1); + indy=Integer.toString(ind2); + } + + public DrawningPlane CreatePlane(){ + Random random = new Random(); + int indEnt = 0; + int indIllum = 0; + if(entitiesCount - 1 != 0 & illumCount - 1 != 0){ + indEnt = random.nextInt(0,entitiesCount - 1); + indIllum = random.nextInt(0, illumCount - 1); + } + T plane = (T)_entities[indEnt]; + U illum = (U)_illuminator[indIllum]; + SetIndexs(indEnt,indIllum); + if(plane instanceof EntityAirbus){ + return new DrawningAirbus(plane,illum); + } + return new DrawningPlane(plane,illum); + } +} diff --git a/DrawningPlane.java b/DrawningPlane.java index fe462e7..7fa305a 100644 --- a/DrawningPlane.java +++ b/DrawningPlane.java @@ -43,6 +43,11 @@ public class DrawningPlane extends JPanel { SetIlluminator(); } + protected DrawningPlane(EntityPlane plane, IDrawningIlluminator illum){ + Plane = plane; + IlluminatorDraw = illum; + } + public void SetPosition(int x, int y, int width, int height) { if (x >= 0 && x + _PlaneWidth <= width && y >= 0 && y + _PlaneHeight <= height) { diff --git a/FormMapWithSetPlanes.form b/FormMapWithSetPlanes.form index f556201..6e0d047 100644 --- a/FormMapWithSetPlanes.form +++ b/FormMapWithSetPlanes.form @@ -16,7 +16,7 @@ - + @@ -28,7 +28,7 @@ - + @@ -44,7 +44,7 @@ - + @@ -52,7 +52,7 @@ - + @@ -60,7 +60,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -138,17 +138,27 @@ - + - + + + + + + + + + + + diff --git a/FormParam.form b/FormParam.form new file mode 100644 index 0000000..e209b0d --- /dev/null +++ b/FormParam.form @@ -0,0 +1,62 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormParam.java b/FormParam.java new file mode 100644 index 0000000..2ce4e03 --- /dev/null +++ b/FormParam.java @@ -0,0 +1,111 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class FormParam extends JFrame{ + public JPanel MainPanel; + private JPanel pictureBoxPlane; + private JButton ButtonCreate; + private JButton ButtonCreateModif; + private JToolBar StatusStrip; + private JLabel LabelInfo; + private JLabel JLabelSpeed = new JLabel(); + private JLabel JLabelWeight = new JLabel(); + private JLabel JLabelColor = new JLabel(); + private DrawningEntities _drawningEntities; + private IDrawningIlluminator SetData() + { + Random random=new Random(); + int r = random.nextInt(3); + if(r==0) + { + return new DrawningIlluminator(); + } + if(r==1) + { + return new DrawningSqareIlluminator(); + } + else + { + return new DrawningTriangleIlluminator(); + } + } + private void Draw(DrawningPlane _plane) { + pictureBoxPlane.removeAll(); + Random random = new Random(); + 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 != null) { + _plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), + pictureBoxPlane.getWidth(), pictureBoxPlane.getHeight()); + _plane.DrawTransport(gr); + JLabelSpeed.setText("Cкорость: " + _plane.GetPlane().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " ")); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(pictureBoxPlane.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(bmp)); + pictureBoxPlane.add(imageOfShip,BorderLayout.CENTER); + } + validate(); + } + public FormParam() + { + Box LabelBox = Box.createHorizontalBox(); + LabelBox.setMinimumSize(new Dimension(1, 20)); + LabelBox.add(JLabelSpeed); + LabelBox.add(JLabelWeight); + LabelBox.add(JLabelColor); + StatusStrip.add(LabelBox); + _drawningEntities = new DrawningEntities<>(10,10); + ButtonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e){ + Random random = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); + EntityPlane ship=new EntityPlane(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst); + IDrawningIlluminator deck = SetData(); + int DecksCount=random.nextInt(1,4) * 10; + deck.SetIlluminatorCount(DecksCount); + if((_drawningEntities.Insert(ship)!=-1) & (_drawningEntities.Insert(deck)!=-1)) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + Draw(_drawningEntities.CreatePlane()); + LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + }); + ButtonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); + Color colorSecond = JColorChooser.showDialog(null, "Цвет", null); + EntityAirbus _ship=new EntityAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); + IDrawningIlluminator deck = SetData(); + int DecksCount=random.nextInt(1,4) * 10; + deck.SetIlluminatorCount(DecksCount); + if((_drawningEntities.Insert(_ship)!=-1) & (_drawningEntities.Insert(deck)!=-1)) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + Draw(_drawningEntities.CreatePlane()); + LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + }); + } +} diff --git a/Main.java b/Main.java index f38f398..f163727 100644 --- a/Main.java +++ b/Main.java @@ -4,7 +4,8 @@ public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Самолёт"); - frame.setContentPane(new FormMapWithSetPlanes().Mainpanel); +// frame.setContentPane(new FormMapWithSetPlanes().Mainpanel); + frame.setContentPane(new FormParam().MainPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(500, 200); frame.pack(); -- 2.25.1 From 41c6423777fc2d3b3a84abdbab68113963d8ee42 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Wed, 14 Dec 2022 13:13:38 +0400 Subject: [PATCH 5/5] third complete --- FormParam.java | 85 +++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/FormParam.java b/FormParam.java index 2ce4e03..daceb69 100644 --- a/FormParam.java +++ b/FormParam.java @@ -41,17 +41,16 @@ public class FormParam extends JFrame{ gr.setColor(new Color(238, 238, 238)); gr.fillRect(0, 0, pictureBoxPlane.getWidth(), pictureBoxPlane.getHeight()); if (_plane != null) { - _plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), - pictureBoxPlane.getWidth(), pictureBoxPlane.getHeight()); + _plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxPlane.getWidth(), pictureBoxPlane.getHeight()); _plane.DrawTransport(gr); JLabelSpeed.setText("Cкорость: " + _plane.GetPlane().GetSpeed() + " "); JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " "); JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " ")); - JLabel imageOfShip = new JLabel(); - imageOfShip.setPreferredSize(pictureBoxPlane.getSize()); - imageOfShip.setMinimumSize(new Dimension(1, 1)); - imageOfShip.setIcon(new ImageIcon(bmp)); - pictureBoxPlane.add(imageOfShip,BorderLayout.CENTER); + JLabel imageOfPlane = new JLabel(); + imageOfPlane.setPreferredSize(pictureBoxPlane.getSize()); + imageOfPlane.setMinimumSize(new Dimension(1, 1)); + imageOfPlane.setIcon(new ImageIcon(bmp)); + pictureBoxPlane.add(imageOfPlane,BorderLayout.CENTER); } validate(); } @@ -64,47 +63,41 @@ public class FormParam extends JFrame{ LabelBox.add(JLabelColor); StatusStrip.add(LabelBox); _drawningEntities = new DrawningEntities<>(10,10); - ButtonCreate.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e){ - Random random = new Random(); - Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); - EntityPlane ship=new EntityPlane(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst); - IDrawningIlluminator deck = SetData(); - int DecksCount=random.nextInt(1,4) * 10; - deck.SetIlluminatorCount(DecksCount); - if((_drawningEntities.Insert(ship)!=-1) & (_drawningEntities.Insert(deck)!=-1)) - { - JOptionPane.showMessageDialog(null,"Объект добавлен"); - Draw(_drawningEntities.CreatePlane()); - LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); - } - else - { - JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); - } + ButtonCreate.addActionListener(e -> { + Random random = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); + EntityPlane _plane = new EntityPlane(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst); + IDrawningIlluminator illum = SetData(); + int IllumCount = random.nextInt(1,4) * 10; + illum.SetIlluminatorCount(IllumCount); + if((_drawningEntities.Insert(_plane)!=-1) & (_drawningEntities.Insert(illum)!=-1)) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + Draw(_drawningEntities.CreatePlane()); + LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); } }); - ButtonCreateModif.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - Random random = new Random(); - Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); - Color colorSecond = JColorChooser.showDialog(null, "Цвет", null); - EntityAirbus _ship=new EntityAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); - IDrawningIlluminator deck = SetData(); - int DecksCount=random.nextInt(1,4) * 10; - deck.SetIlluminatorCount(DecksCount); - if((_drawningEntities.Insert(_ship)!=-1) & (_drawningEntities.Insert(deck)!=-1)) - { - JOptionPane.showMessageDialog(null,"Объект добавлен"); - Draw(_drawningEntities.CreatePlane()); - LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); - } - else - { - JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); - } + ButtonCreateModif.addActionListener(e -> { + Random random = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); + Color colorSecond = JColorChooser.showDialog(null, "Цвет", null); + EntityAirbus _plane = new EntityAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); + IDrawningIlluminator illum = SetData(); + int IllumCount = random.nextInt(1,4) * 10; + illum.SetIlluminatorCount(IllumCount); + if((_drawningEntities.Insert(_plane)!=-1) & (_drawningEntities.Insert(illum)!=-1)) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + Draw(_drawningEntities.CreatePlane()); + LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); } }); } -- 2.25.1