From 557d46293b111323729ac32d68b924f1703a8636 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Thu, 8 Dec 2022 21:15:20 +0400 Subject: [PATCH] lab#4 --- DrawingObjectShip.java | 3 + FormMapWithSetShipsGeneric.form | 108 +++++++++++++++----- FormMapWithSetShipsGeneric.java | 168 ++++++++++++++++++++++---------- FormShip.java | 15 ++- MapWithSetShipsGeneric.java | 26 +++-- MapsCollection.java | 45 +++++++++ SetShipGeneric.java | 63 +++++------- 7 files changed, 312 insertions(+), 116 deletions(-) create mode 100644 MapsCollection.java diff --git a/DrawingObjectShip.java b/DrawingObjectShip.java index 3437abd..dc419c7 100644 --- a/DrawingObjectShip.java +++ b/DrawingObjectShip.java @@ -13,6 +13,9 @@ public class DrawingObjectShip implements IDrawingObject { } return 0; } + public DrawingShip GetDrawingObjectShip() { + return _ship; + } @Override public void SetObject(int x, int y, int width, int height) { _ship.SetPosition(x,y,width,height); diff --git a/FormMapWithSetShipsGeneric.form b/FormMapWithSetShipsGeneric.form index d0a132c..ae9f62e 100644 --- a/FormMapWithSetShipsGeneric.form +++ b/FormMapWithSetShipsGeneric.form @@ -1,16 +1,20 @@
- + - + - + + + + + @@ -18,32 +22,20 @@ - + - + - - - - - - - - - - - - - + @@ -51,7 +43,7 @@ - + @@ -59,7 +51,7 @@ - + @@ -67,7 +59,7 @@ - + @@ -75,7 +67,7 @@ - + @@ -129,6 +121,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FormMapWithSetShipsGeneric.java b/FormMapWithSetShipsGeneric.java index 76c1c82..d04c3fe 100644 --- a/FormMapWithSetShipsGeneric.java +++ b/FormMapWithSetShipsGeneric.java @@ -6,7 +6,9 @@ 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 FormMapWithSetShipsGeneric extends JFrame{ public JPanel Mainpanel; private JPanel pictureBoxShip; @@ -21,7 +23,22 @@ public class FormMapWithSetShipsGeneric extends JFrame{ private JButton ButtonUp; private JButton ButtonRight; private JTextField maskedTextBoxPosition; + private JTextField textBoxNewMapName; + private int picWidth=600; + + private int picHeight=400; + private JButton ButtonAddMap; + private JList ListBoxMaps; + private JButton ButtonDeleteMap; + private JPanel GroupBoxMaps; + private JButton ButtonCheckDel; private MapWithSetShipsGeneric _mapShipsCollectionGeneric; + private final HashMap _mapsDict = new HashMap<>(){{ + put("Простая карта",new SimpleMap()); + put("Карта острова",new IslandsMap()); + put("Карта скалы",new RocksMap()); + }}; + private final MapsCollection _mapsCollection; public void UpdateWindow(BufferedImage bmp) { pictureBoxShip.removeAll(); @@ -32,8 +49,30 @@ public class FormMapWithSetShipsGeneric extends JFrame{ pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); pictureBoxShip.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); + } + } public FormMapWithSetShipsGeneric() { + _mapsCollection = new MapsCollection(picWidth, picHeight); + ComboBoxSelectorMap.removeAllItems(); + for (String elem : _mapsDict.keySet()) { + ComboBoxSelectorMap.addItem(elem); + } try { Image img = ImageIO.read(FormShip.class.getResource("Images/4.png")); ButtonUp.setIcon(new ImageIcon(img)); @@ -47,51 +86,22 @@ public class FormMapWithSetShipsGeneric extends JFrame{ System.out.println(ex); } - 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 IslandsMap(); - break; - } - case "Карта скалы": { - map = new RocksMap(); - break; - } - } - if(map!=null) - { - _mapShipsCollectionGeneric = new MapWithSetShipsGeneric(pictureBoxShip.getWidth(),pictureBoxShip.getHeight(),map); - } - else - { - _mapShipsCollectionGeneric=null; - } - } - }); ButtonAddShip.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_mapShipsCollectionGeneric==null) + if (ListBoxMaps.getSelectedIndex() == -1) { return; } FormShip form = new FormShip(); form.setSize(1000,700); form.setVisible(true); + form.setModal(true); DrawingObjectShip ship = new DrawingObjectShip(form.GetSelectedShip()); - if(_mapShipsCollectionGeneric.Add(ship)!=-1) + if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship)!=-1) { JOptionPane.showMessageDialog(null,"Объект добавлен"); - UpdateWindow(_mapShipsCollectionGeneric.ShowSet()); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } else { @@ -99,7 +109,14 @@ public class FormMapWithSetShipsGeneric extends JFrame{ } } }); - + ListBoxMaps.addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + if (ListBoxMaps.getSelectedIndex() == -1) + return; + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); + } + }); ButtonDeleteShip.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -113,10 +130,10 @@ public class FormMapWithSetShipsGeneric extends JFrame{ return; } int pos=Integer.parseInt(maskedTextBoxPosition.getText()); - if(_mapShipsCollectionGeneric.Delete(pos)!=null) + if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Delete(pos)!=null) { JOptionPane.showMessageDialog(null, "Объект удален"); - UpdateWindow(_mapShipsCollectionGeneric.ShowSet()); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } else { @@ -127,27 +144,27 @@ public class FormMapWithSetShipsGeneric extends JFrame{ ButtonShowStorage.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() == -1) { return; } - UpdateWindow(_mapShipsCollectionGeneric.ShowSet()); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); } }); ButtonShowMap.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() == -1) { return; } - UpdateWindow(_mapShipsCollectionGeneric.ShowOnMap()); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap()); } }); ButtonUp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() == -1) { return; } @@ -155,7 +172,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{ JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Up))); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up))); pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); pictureBoxShip.revalidate(); pictureBoxShip.repaint(); @@ -164,7 +181,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{ ButtonDown.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() == -1) { return; } @@ -172,7 +189,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{ JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Down))); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down))); pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); pictureBoxShip.revalidate(); pictureBoxShip.repaint(); @@ -181,7 +198,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{ ButtonRight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() == -1) { return; } @@ -189,7 +206,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{ JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Right))); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right))); pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); pictureBoxShip.revalidate(); pictureBoxShip.repaint(); @@ -198,7 +215,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{ ButtonLeft.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() == -1) { return; } @@ -206,7 +223,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{ JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Left))); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left))); pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); pictureBoxShip.revalidate(); pictureBoxShip.repaint(); @@ -222,5 +239,58 @@ public class FormMapWithSetShipsGeneric extends JFrame{ } } }); + 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(!_mapsDict.containsKey(ComboBoxSelectorMap.getSelectedItem())) + { + JOptionPane.showMessageDialog(null,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE); + } + _mapsCollection.AddMap(textBoxNewMapName.getText(),_mapsDict.get(ComboBoxSelectorMap.getSelectedItem().toString())); + ReloadMaps(); + } + }); + 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(); + } + } + }); + ButtonCheckDel.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + DrawingObjectShip ship=_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).GetShipsDeleted(); + if(ship==null) + { + JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE); + return; + } + FormShip formShip=new FormShip(ship); + formShip.setSize(1000,800); + formShip.setVisible(true); + } + }); + } + + private void createUIComponents() { + DefaultListModel dlm = new DefaultListModel(); + ListBoxMaps = new JList(dlm); } } diff --git a/FormShip.java b/FormShip.java index ba2cb82..eed562f 100644 --- a/FormShip.java +++ b/FormShip.java @@ -44,8 +44,21 @@ public class FormShip extends JDialog{ } validate(); } + public FormShip(DrawingObjectShip ship) + { + super(new Frame("Корабль")); + CreateWindow(); + setModal(true); + _ship=ship.GetDrawingObjectShip(); + getContentPane().add(Mainpanel); + } public FormShip() { super(new Frame("Корабль")); + CreateWindow(); + setModal(true); + getContentPane().add(Mainpanel); + } + public void CreateWindow(){ Box LabelBox = Box.createHorizontalBox(); LabelBox.setMinimumSize(new Dimension(1, 20)); LabelBox.add(JLabelSpeed); @@ -143,7 +156,5 @@ public class FormShip extends JDialog{ dispose(); } }); - setModal(true); - getContentPane().add(Mainpanel); } } diff --git a/MapWithSetShipsGeneric.java b/MapWithSetShipsGeneric.java index a42addf..6ba7a30 100644 --- a/MapWithSetShipsGeneric.java +++ b/MapWithSetShipsGeneric.java @@ -1,5 +1,6 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.util.Stack; public class MapWithSetShipsGeneric { private final int _pictureWidth; @@ -8,8 +9,10 @@ public class MapWithSetShipsGeneric _setShips; private final U _map; + private Stack _deletedShips; public MapWithSetShipsGeneric(int picWidth, int picHeight, U map) { + _deletedShips=new Stack<>(); int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _setShips = new SetShipGeneric(width * height); @@ -23,7 +26,9 @@ public class MapWithSetShipsGeneric> _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 MapWithSetShipsGeneric<>(_pictureWidth,_pictureHeight,Map)); + } + } + public void DelMap(String name) + { + _mapStorages.remove(name); + } + public MapWithSetShipsGeneric Get(String ind) + { + if(_mapStorages.containsKey(ind)) + { + return _mapStorages.get(ind); + } + return null; + } + public DrawingObjectShip Get(String name, int ind) { + if (_mapStorages.containsKey(name)) + { + return _mapStorages.get(name).GetSelectedShip(ind); + } + return null; + } +} diff --git a/SetShipGeneric.java b/SetShipGeneric.java index 30f662b..badcf07 100644 --- a/SetShipGeneric.java +++ b/SetShipGeneric.java @@ -1,12 +1,17 @@ -public class SetShipGeneric { - private final Object[] _places; +import java.util.ArrayList; +import java.util.Iterator; +public class SetShipGeneric implements Iterable{ + private ArrayList _places; + private final int _maxCount; + //private final Object[] _places; public int Count() { - return _places.length; + return _places.size(); } public SetShipGeneric(int count) { - _places = new Object[count]; + _maxCount=count; + _places = new ArrayList<>(); } public int Insert(T ship) { @@ -14,52 +19,38 @@ public class SetShipGeneric { } public int Insert(T ship, int position) { - if (position >= _places.length || position < 0) - { - return -1; - } - if (_places[position] == null) - { - _places[position] = ship; - return position; - } - int emptyIndex = -1; - for (int i = position + 1; i < Count(); i++) - { - if (_places[i] == null) - { - emptyIndex = i; - break; - } - } - if (emptyIndex < 0) - { - return -1; - } - for (int i = emptyIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = ship; + if (position < 0 || position > Count() || _maxCount == Count()) return -1; + _places.add(position,ship); return position; } public T Remove(int position) { - if (position >= _places.length || position < 0) + if (position >= Count() || position < 0) { return null; } - T ship = (T) _places[position]; - _places[position] = null; + T ship = (T) _places.get(position); + _places.remove(ship); return ship; } public T Get(int position) { - if (position >= _places.length || position < 0) + if (position >= Count() || position<0) { return null; } - return (T)_places[position]; + 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(); } }