From 12d639e3dfa3889060be1f4324f654f8ee81268f Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Wed, 14 Dec 2022 15:30:36 +0400 Subject: [PATCH 1/2] complete --- DrawningObjectPlane.java | 3 + FormMapWithSetPlanes.form | 284 ++++++++++++++++++++--------------- FormPlane.java | 16 +- Main.java | 3 +- MapWithSetPlanesGeneric.java | 19 ++- MapsCollection.java | 44 ++++++ SetPlanesGeneric.java | 58 ++++--- 7 files changed, 275 insertions(+), 152 deletions(-) create mode 100644 MapsCollection.java diff --git a/DrawningObjectPlane.java b/DrawningObjectPlane.java index 1353dd5..86c7ca0 100644 --- a/DrawningObjectPlane.java +++ b/DrawningObjectPlane.java @@ -35,4 +35,7 @@ public class DrawningObjectPlane implements IDrawningObject { return _plane.GetCurrentPosition(); return null; } + public DrawningPlane GetDrawningObjectPlane() { + return _plane; + } } diff --git a/FormMapWithSetPlanes.form b/FormMapWithSetPlanes.form index 6e0d047..749be9b 100644 --- a/FormMapWithSetPlanes.form +++ b/FormMapWithSetPlanes.form @@ -1,164 +1,200 @@
- + - + - + - + + + + + - + + + - + + + + + + - - - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FormPlane.java b/FormPlane.java index 8ef35b8..1435036 100644 --- a/FormPlane.java +++ b/FormPlane.java @@ -50,7 +50,8 @@ public class FormPlane extends JDialog{ JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " "); JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " ")); } - public FormPlane() { + + private void CreateWindow(){ add(Mainpanel); Box LabelBox = Box.createHorizontalBox(); LabelBox.setMinimumSize(new Dimension(1, 20)); @@ -121,5 +122,18 @@ public class FormPlane extends JDialog{ dispose(); }); } + public FormPlane() { + super(new Frame("Самолёт")); + CreateWindow(); + setModal(true); + getContentPane().add(Mainpanel); + } + public FormPlane(DrawningObjectPlane plane){ + super(new Frame("Корабль")); + CreateWindow(); + setModal(true); + _plane = plane.GetDrawningObjectPlane(); + getContentPane().add(Mainpanel); + } } diff --git a/Main.java b/Main.java index f163727..f38f398 100644 --- a/Main.java +++ b/Main.java @@ -4,8 +4,7 @@ public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Самолёт"); -// frame.setContentPane(new FormMapWithSetPlanes().Mainpanel); - frame.setContentPane(new FormParam().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 09c9602..556df29 100644 --- a/MapWithSetPlanesGeneric.java +++ b/MapWithSetPlanesGeneric.java @@ -1,5 +1,6 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.util.LinkedList; public class MapWithSetPlanesGeneric { @@ -9,9 +10,11 @@ public class MapWithSetPlanesGeneric _setPlanes; private U _map; + private LinkedList _deletedPlanes; public MapWithSetPlanesGeneric(int picWidth, int picHeight, U map) { + _deletedPlanes = new LinkedList<>(); int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _setPlanes = new SetPlanesGeneric(width * height); @@ -27,7 +30,9 @@ public class MapWithSetPlanesGeneric > _mapStorages; + public ArrayList Keys() + { + return new ArrayList<>(_mapStorages.keySet()); + } + private final int _pictureWidth; + private final int _pictureHeight; + public MapsCollection(int pictureWidth,int pictureHeight) + { + _mapStorages=new HashMap<>(); + _pictureWidth=pictureWidth; + _pictureHeight=pictureHeight; + } + public void AddMap(String Name, AbstractMap Map) + { + if(!_mapStorages.containsKey(Name)) + { + _mapStorages.put(Name,new MapWithSetPlanesGeneric<>(_pictureWidth,_pictureHeight,Map)); + } + } + public void DelMap(String name) + { + _mapStorages.remove(name); + } + public MapWithSetPlanesGeneric Get(String ind) + { + if(_mapStorages.containsKey(ind)) + { + return _mapStorages.get(ind); + } + return null; + } + public DrawningObjectPlane Get(String name, int ind) { + if (_mapStorages.containsKey(name)) + { + return _mapStorages.get(name).GetSelectedPlane(ind); + } + return null; + } +} diff --git a/SetPlanesGeneric.java b/SetPlanesGeneric.java index 7eb7932..896a50c 100644 --- a/SetPlanesGeneric.java +++ b/SetPlanesGeneric.java @@ -1,44 +1,54 @@ -public class SetPlanesGeneric { - private T[] _places; +import java.util.ArrayList; +import java.util.Iterator; +public class SetPlanesGeneric implements Iterable { + private ArrayList _places; + private final int _maxCount; public int Count() { - return _places.length; + return _places.size(); } public SetPlanesGeneric(int count) { - _places = (T[]) (new Object[count]); + _maxCount=count; + _places = new ArrayList<>(); } public int Insert(T plane) { - for (int i = 0; i < _places.length; i++) { - if (_places[i] == null) { - _places[i] = plane; - return i; - } - } - return -1; + return Insert(plane, 0); } 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; + if (position < 0 || position > Count() || _maxCount == Count()) return -1; + _places.add(position,plane); return position; } public T Remove(int position) { - if (position >= _places.length) return null; - T res = _places[position]; - _places[position] = null; - return res; + if (position >= Count() || position < 0) + { + return null; + } + T ship = (T) _places.get(position); + _places.remove(ship); + return ship; } public T Get(int position) { - return _places[position]; + if (position >= Count() || position<0) + { + return null; + } + return _places.get(position); + } + public void Set(int position,T value) + { + if (position < _maxCount || position >= 0) + { + Insert(value,position); + } + } + @Override + public Iterator iterator() { + return _places.iterator(); } } \ No newline at end of file -- 2.25.1 From 1a7b11939cb4729a0408f031d2a3b83274c5b79c Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Wed, 14 Dec 2022 15:30:52 +0400 Subject: [PATCH 2/2] complete --- FormMapWithSetPlanes.java | 305 +++++++++++++++++++++----------------- 1 file changed, 171 insertions(+), 134 deletions(-) diff --git a/FormMapWithSetPlanes.java b/FormMapWithSetPlanes.java index aa37fe1..d9d4a6b 100644 --- a/FormMapWithSetPlanes.java +++ b/FormMapWithSetPlanes.java @@ -1,39 +1,71 @@ import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; - -public class FormMapWithSetPlanes extends JFrame { - - private MapWithSetPlanesGeneric _mapPlanesCollectionGeneric; - +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 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; + 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); + } } - - JFrame jFrame = getFrame(); - private void ButtonMove_Click(String name) { - if (_mapPlanesCollectionGeneric == null) return; Direction direction = Direction.None; switch (name) { @@ -50,54 +82,30 @@ public class FormMapWithSetPlanes extends JFrame { 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(); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(direction)); } - - public FormMapWithSetPlanes() { - comboBoxSelectorMap.addItem("Простая карта"); - comboBoxSelectorMap.addItem("Вторая карта"); + public FormMapWithSetPlanes() + { + _mapsCollection = new MapsCollection(picWidth, picHeight); + ComboBoxSelectorMap.removeAllItems(); + for (String elem : _mapsDict.keySet()) { + ComboBoxSelectorMap.addItem(elem); + } try { - Image img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowUp.jpg")); - buttonUp.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowDown.jpg")); - buttonDown.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowLeft.jpg")); - buttonLeft.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowRight.jpg")); - buttonRight.setIcon(new ImageIcon(img)); + 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); } - 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) + ButtonAddPlane.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) { return; } @@ -109,84 +117,113 @@ public class FormMapWithSetPlanes extends JFrame { dialog.setVisible(true); if (dialog.GetSelectedPlane() == null) return; - - DrawningObjectPlane plane = new DrawningObjectPlane(dialog.GetSelectedPlane()); - - if (_mapPlanesCollectionGeneric.addPlane(plane) != -1) + DrawningObjectPlane Plane = new DrawningObjectPlane(dialog.GetSelectedPlane()); + if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).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(); + JOptionPane.showMessageDialog(null,"Объект добавлен"); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } else { - JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект"); + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); } }); - 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){ + ListBoxMaps.addListSelectionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) + return; + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); + }); + ButtonDeletePlane.addActionListener(e -> { + if (maskedTextBoxPosition.getText().isEmpty()) + { return; } - - int pos = Integer.parseInt(text); - if (_mapPlanesCollectionGeneric.removePlane(pos) != null) + int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); + if(result==JOptionPane.NO_OPTION) { - 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(); + 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(jFrame, "Не удалось удалить объект"); + JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); } }); - 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(); + ButtonShowStorage.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); }); - 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(); + 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")); + 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); } } -- 2.25.1