From 612001e1dbf788f52f9355088a61529a8ac92b09 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Mon, 7 Nov 2022 10:19:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D0=B2?= =?UTF-8?q?=D1=81=D0=BF=D0=BB=D1=8B=D0=B2=D0=B0=D1=8E=D1=89=D0=B5=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=BE=D0=BA=D0=BD=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project/src/DrawningObjectPlane.java | 5 + Project/src/FormMapWithSetPlanesGeneric.form | 6 +- Project/src/FormMapWithSetPlanesGeneric.java | 139 ++++++++++--------- Project/src/FormPlane.java | 122 +++++++++++----- Project/src/MapWithSetPlanesGeneric.java | 8 +- Project/src/MapsCollection.java | 6 +- 6 files changed, 181 insertions(+), 105 deletions(-) diff --git a/Project/src/DrawningObjectPlane.java b/Project/src/DrawningObjectPlane.java index 89d6b0d..b988862 100644 --- a/Project/src/DrawningObjectPlane.java +++ b/Project/src/DrawningObjectPlane.java @@ -4,6 +4,11 @@ public class DrawningObjectPlane implements IDrawningObject { private DrawingPlane _plane = null; + public DrawingPlane GetDrawingObjectPlane() + { + return _plane; + } + public DrawningObjectPlane(DrawingPlane plane){ _plane = plane; } @Override diff --git a/Project/src/FormMapWithSetPlanesGeneric.form b/Project/src/FormMapWithSetPlanesGeneric.form index 34a3eb3..c845aca 100644 --- a/Project/src/FormMapWithSetPlanesGeneric.form +++ b/Project/src/FormMapWithSetPlanesGeneric.form @@ -207,7 +207,7 @@ - + @@ -215,7 +215,9 @@ - + + + diff --git a/Project/src/FormMapWithSetPlanesGeneric.java b/Project/src/FormMapWithSetPlanesGeneric.java index 4330891..41b7a11 100644 --- a/Project/src/FormMapWithSetPlanesGeneric.java +++ b/Project/src/FormMapWithSetPlanesGeneric.java @@ -31,9 +31,6 @@ public class FormMapWithSetPlanesGeneric { private JPanel GroupBoxMap; private JButton ButtonShowDelete; - //объект от класса карты с набором объектов - private MapWithSetPlanesGeneric _mapPlanesCollectionGeneric; - //объект от коллекции карт private final MapsCollection _mapsCollection; @@ -49,11 +46,11 @@ public class FormMapWithSetPlanesGeneric { { /*super("Хранилище");*/ CreateWindow(); - _mapsCollection = new MapsCollection(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + _mapsCollection = new MapsCollection(730, 650); ComboBoxSelectorMap.removeAllItems(); - for (String element : _mapsHashMap.keySet()) - { - ComboBoxSelectorMap.addItem(element); + + for (String elem : _mapsHashMap.keySet()) { + ComboBoxSelectorMap.addItem(elem); } } @@ -62,12 +59,12 @@ public class FormMapWithSetPlanesGeneric { { int index = ListBoxMaps.getSelectedIndex(); - DefaultListModel model = (DefaultListModel) ListBoxMaps.getModel(); + DefaultListModel model = (DefaultListModel)ListBoxMaps.getModel(); model.removeAllElements(); - for (int i = 0; i < _mapsCollection.Keys.size(); i++) + for (int i = 0; i < _mapsCollection.Keys().size(); i++) { - model.addElement(_mapsCollection.Keys.get(i)); + model.addElement(_mapsCollection.Keys().get(i)); } if (ListBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= ListBoxMaps.getModel().getSize())) @@ -115,7 +112,22 @@ public class FormMapWithSetPlanesGeneric { ButtonAddMap.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + if(ComboBoxSelectorMap.getSelectedIndex() == -1 || TextBoxNewMapName.getText().isEmpty()) + { + JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE); + return; + } + + if(!_mapsHashMap.containsKey(ComboBoxSelectorMap.getSelectedItem())) + { + JOptionPane.showMessageDialog(null, "Данная карта отсутсвует", "Ошибка", JOptionPane.ERROR_MESSAGE); + + return; + } + + _mapsCollection.AddMap(TextBoxNewMapName.getText(), _mapsHashMap.get(ComboBoxSelectorMap.getSelectedItem().toString())); + ReloadMaps(); } }); @@ -123,7 +135,17 @@ public class FormMapWithSetPlanesGeneric { ButtonDeleteMap.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + if(ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + if(JOptionPane.showConfirmDialog(null, "Удалить карту" + ListBoxMaps.getSelectedValue().toString() + "?", + "Удаление", JOptionPane.YES_NO_OPTION) == 0) + { + _mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString()); + ReloadMaps(); + } } }); @@ -131,7 +153,12 @@ public class FormMapWithSetPlanesGeneric { ListBoxMaps.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { + if(ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } }); @@ -139,7 +166,23 @@ public class FormMapWithSetPlanesGeneric { ButtonShowDelete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + if(ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + DrawningObjectPlane plane = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetPlaneInDelete(); + + if(plane == null) + { + JOptionPane.showMessageDialog(null, "Список пуст", "Ошибка", JOptionPane.ERROR_MESSAGE); + + return; + } + + FormPlane form = new FormPlane(plane); + form.setSize(500, 300); + form.setVisible(true); } }); @@ -147,7 +190,7 @@ public class FormMapWithSetPlanesGeneric { ButtonAddPlane.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_mapPlanesCollectionGeneric == null) + if(_mapsCollection == null) { return; } @@ -158,10 +201,10 @@ public class FormMapWithSetPlanesGeneric { form.setModal(true); DrawningObjectPlane plane = new DrawningObjectPlane(form.GetSelectedShip()); - if(_mapPlanesCollectionGeneric.Add(plane) != -1) + if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Add(plane) != -1) { JOptionPane.showMessageDialog(null,"Объект добавлен"); - UpdateWindow(_mapPlanesCollectionGeneric.ShowSet()); + UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } else { @@ -181,17 +224,18 @@ public class FormMapWithSetPlanesGeneric { int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); - if(result==JOptionPane.NO_OPTION) + + if(result == JOptionPane.NO_OPTION) { return; } int pos = Integer.parseInt(MaskedTextBoxPosition.getText()); - if(_mapPlanesCollectionGeneric.Delete(pos) != null) + if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Delete(pos) != null) { JOptionPane.showMessageDialog(null,"Объект удалён"); - UpdateWindow(_mapPlanesCollectionGeneric.ShowSet()); + UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } else { @@ -204,12 +248,12 @@ public class FormMapWithSetPlanesGeneric { ButtonShowStorage.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_mapPlanesCollectionGeneric == null) + if(_mapsCollection == null) { return; } - UpdateWindow(_mapPlanesCollectionGeneric.ShowSet()); + UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } }); @@ -217,19 +261,19 @@ public class FormMapWithSetPlanesGeneric { ButtonShowOnMap.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapPlanesCollectionGeneric == null) + if (_mapsCollection == null) { return; } - UpdateWindow(_mapPlanesCollectionGeneric.ShowOnMap()); + UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap()); } }); ButtonUp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_mapPlanesCollectionGeneric == null) + if(_mapsCollection== null) { return; } @@ -239,7 +283,7 @@ public class FormMapWithSetPlanesGeneric { JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Up)))); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Up)))); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.revalidate(); @@ -250,7 +294,7 @@ public class FormMapWithSetPlanesGeneric { ButtonLeft.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_mapPlanesCollectionGeneric == null) + if(_mapsCollection == null) { return; } @@ -260,7 +304,7 @@ public class FormMapWithSetPlanesGeneric { JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Left)))); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Left)))); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.revalidate(); @@ -271,7 +315,7 @@ public class FormMapWithSetPlanesGeneric { ButtonDown.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_mapPlanesCollectionGeneric == null) + if(_mapsCollection == null) { return; } @@ -281,7 +325,7 @@ public class FormMapWithSetPlanesGeneric { JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Down)))); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Down)))); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.revalidate(); @@ -292,7 +336,7 @@ public class FormMapWithSetPlanesGeneric { ButtonRight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_mapPlanesCollectionGeneric == null) + if(_mapsCollection == null) { return; } @@ -302,7 +346,7 @@ public class FormMapWithSetPlanesGeneric { JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Right)))); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Right)))); PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBoxPlane.revalidate(); @@ -310,40 +354,6 @@ public class FormMapWithSetPlanesGeneric { } }); - //выпадающий список с вариантами карт - 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) { @@ -355,4 +365,9 @@ public class FormMapWithSetPlanesGeneric { } }); } + + private void createUIComponents() { + DefaultListModel defListMod = new DefaultListModel(); + ListBoxMaps = new JList(defListMod); + } } diff --git a/Project/src/FormPlane.java b/Project/src/FormPlane.java index de4e378..e89a799 100644 --- a/Project/src/FormPlane.java +++ b/Project/src/FormPlane.java @@ -21,8 +21,10 @@ public class FormPlane extends JDialog private JLabel LabelWeight = new JLabel(); private JLabel LabelColor = new JLabel(); - protected DrawingPlane plane; + protected DrawingPlane _plane; protected DrawingPlane _selectedPlane; + private int PictureBoxPlaneWidth; + private int PictureBoxPlaneHeight; public DrawingPlane GetSelectedShip() { @@ -38,7 +40,7 @@ public class FormPlane extends JDialog gr.setColor(new Color(238, 238, 238)); gr.fillRect(0, 0, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); - if (plane.GetPlane() != null) { + if (_plane.GetPlane() != null) { _plane.DrawTransport(gr); JLabel imageOfLogo = new JLabel(); imageOfLogo.setPreferredSize(PictureBoxPlane.getSize()); @@ -50,18 +52,46 @@ public class FormPlane extends JDialog validate(); } - //создание всплывающего окна + //создание всплывающего окна (первый конструктор) public FormPlane() { - super(new Frame("Airbus")); + super(new Frame("Аэробус")); CreateWindow(); setModal(true); getContentPane().add(MainPanel); } + //второй конструктор + protected FormPlane(DrawningObjectPlane plane) + { + super(new Frame("Аэробус")); + CreateWindow(); + setModal(true); + _plane = plane.GetDrawingObjectPlane(); + SetData(); + } + + public DrawingPlane GetSelectedPlane() + { + return _selectedPlane; + } + + public void SetData() + { + Random rnd = new Random(); + _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()); + PictureBoxPlane.add(_plane, BorderLayout.CENTER); + } + public void CreateWindow() { - setModal(true); + /*setModal(true);*/ //создание строки отображения скорости, веса и цвета объекта Box LableBox = Box.createHorizontalBox(); @@ -71,6 +101,10 @@ public class FormPlane extends JDialog LableBox.add(LabelColor); StatusStrip.add(LableBox); + PictureBoxPlane.setBackground(Color.WHITE); + setPreferredSize(new Dimension(1000, 700)); + getContentPane().add(MainPanel); + try { Image img = ImageIO.read(getClass().getResource("resourses/Up.png")); @@ -87,46 +121,56 @@ public class FormPlane extends JDialog System.out.println(ex.getMessage()); } - _selectedPlane = plane; + _selectedPlane = _plane; ButtonCreate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + try + { + PictureBoxPlane.remove(_plane); + } + catch (Exception c){ } + Random rnd = new Random(); Color colorSimple = JColorChooser.showDialog(null, "Выберите цвет", null); - plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + _plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), 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()); + SetData(); - Draw(plane); + Draw(_plane); } }); ButtonCreateModif.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + try + { + PictureBoxPlane.remove(_plane); + } + catch (Exception c){ } + 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), + _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()); + SetData(); - Draw(plane); + Draw(_plane); + } + }); + + ButtonSelectPlane.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _selectedPlane = _plane; + dispose(); } }); @@ -135,48 +179,56 @@ public class FormPlane extends JDialog public void componentResized(ComponentEvent e) { super.componentResized(e); - if(plane != null) + if(_plane == null) { - plane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); - PictureBoxPlane.revalidate(); - Draw(plane); + return; } + + _plane.ChangeBorders(PictureBoxPlaneWidth, PictureBoxPlaneHeight); + + try + { + PictureBoxPlane.remove(_plane); + } + catch (Exception c){ } + + PictureBoxPlane.add(_plane, BorderLayout.CENTER); } }); ButtonUp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - plane.MoveTransport(Direction.Up); + _plane.MoveTransport(Direction.Up); PictureBoxPlane.revalidate(); - Draw(plane); + Draw(_plane); } }); ButtonLeft.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - plane.MoveTransport(Direction.Left); + _plane.MoveTransport(Direction.Left); PictureBoxPlane.revalidate(); - Draw(plane); + Draw(_plane); } }); ButtonDown.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - plane.MoveTransport(Direction.Down); + _plane.MoveTransport(Direction.Down); PictureBoxPlane.revalidate(); - Draw(plane); + Draw(_plane); } }); ButtonRight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - plane.MoveTransport(Direction.Right); + _plane.MoveTransport(Direction.Right); PictureBoxPlane.revalidate(); - Draw(plane); + Draw(_plane); } }); @@ -184,7 +236,7 @@ public class FormPlane extends JDialog ButtonSelectPlane.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - _selectedPlane = plane; + _selectedPlane = _plane; dispose(); } }); diff --git a/Project/src/MapWithSetPlanesGeneric.java b/Project/src/MapWithSetPlanesGeneric.java index 000a0d2..1d2d354 100644 --- a/Project/src/MapWithSetPlanesGeneric.java +++ b/Project/src/MapWithSetPlanesGeneric.java @@ -44,9 +44,9 @@ public class MapWithSetPlanesGeneric > _mapStorage; //возвращение списка названий карт - public List Keys; + public ArrayList Keys() + { + return new ArrayList<>(_mapStorage.keySet()); + } //ширина окна отрисовки private final int _pictureWidth; @@ -18,7 +21,6 @@ public class MapsCollection //конструктор public MapsCollection(int pictureWidth, int pictureHeight) { - Keys = new ArrayList(_mapStorage.keySet()); _mapStorage = new HashMap<>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight;