diff --git a/DrawningObjectShip.java b/DrawningObjectShip.java index 7f22022..d2e471e 100644 --- a/DrawningObjectShip.java +++ b/DrawningObjectShip.java @@ -9,6 +9,8 @@ public class DrawningObjectShip implements IDrawningObject { _warmlyShip = warmlyShip; } + public DrawingShip getDrawingShip() {return _warmlyShip;} + @Override public float getStep() { if (_warmlyShip == null || _warmlyShip.warmlyShip == null) return 0; diff --git a/FormMapWithSetShipsGeneric.form b/FormMapWithSetShipsGeneric.form new file mode 100644 index 0000000..93f336a --- /dev/null +++ b/FormMapWithSetShipsGeneric.form @@ -0,0 +1,196 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormMapWithSetShipsGeneric.java b/FormMapWithSetShipsGeneric.java new file mode 100644 index 0000000..41302bc --- /dev/null +++ b/FormMapWithSetShipsGeneric.java @@ -0,0 +1,309 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.image.BufferedImage; +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; + private JPanel GroupBoxTools; + private JComboBox ComboBoxSelectorMap; + private JButton ButtonAddShip; + private JButton ButtonDeleteShip; + 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=400; + private JButton ButtonAddMap; + private JList ListBoxMaps; + private JButton ButtonDeleteMap; + private JPanel GroupBoxMaps; + private JButton ButtonCheckDel; + private MapWithSetShipGeneric _mapShipsCollectionGeneric; + private final HashMap _mapsDict = new HashMap<>(){{ + put("Простая карта",new SimpleMap()); + put("Вторая карта",new SecondMap()); + put("Последняя карта",new LastMap()); + }}; + private final MapsCollection _mapsCollection; + public void UpdateWindow(BufferedImage bmp) + { + pictureBoxShip.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(bmp)); + 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() + { + //picWidth = pictureBoxShip.getWidth(); + //picHeight = pictureBoxShip.getHeight(); + _mapsCollection = new MapsCollection(picWidth, picHeight); + ComboBoxSelectorMap.removeAllItems(); + for (String elem : _mapsDict.keySet()) { + ComboBoxSelectorMap.addItem(elem); + } + + try { + Image img = ImageIO.read(FormShip.class.getResource("/Images/totop.png")); + buttonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormShip.class.getResource("/Images/toleft.png")); + buttonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormShip.class.getResource("/Images/todown.png")); + buttonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormShip.class.getResource("/Images/toright.png")); + buttonRight.setIcon(new ImageIcon(img)); + } catch (Exception ex) { + System.out.println(ex); + } + + ButtonAddShip.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + + FormShip dialog = new FormShip(); + 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.GetSelectedShip() == null) return; + + DrawningObjectShip ship = new DrawningObjectShip(dialog.GetSelectedShip()); + + if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship) != -1) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + }); + 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) { + if (maskedTextBoxPosition.getText().isEmpty()) + { + return; + } + int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); + if(result==JOptionPane.NO_OPTION) + { + return; + } + int pos=Integer.parseInt(maskedTextBoxPosition.getText()); + if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Delete(pos)!=null) + { + JOptionPane.showMessageDialog(null, "Объект удален"); + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); + } + } + }); + ButtonShowStorage.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); + } + }); + ButtonShowMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap()); + } + }); + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + pictureBoxShip.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up))); + pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxShip.revalidate(); + pictureBoxShip.repaint(); + } + }); + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + pictureBoxShip.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down))); + pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxShip.revalidate(); + pictureBoxShip.repaint(); + } + }); + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + pictureBoxShip.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right))); + pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxShip.revalidate(); + pictureBoxShip.repaint(); + } + }); + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + pictureBoxShip.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left))); + pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxShip.revalidate(); + pictureBoxShip.repaint(); + + } + }); + 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(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; + } + DrawningObjectShip ship = _mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).GetShipsDeleted(); + if(ship == null) + { + JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE); + return; + } + FormShip dialog = new FormShip(); + dialog.setShipIn(ship); + dialog.setSize(800, 600); + dialog.setLocation(500, 200); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + } + }); + } + + private void createUIComponents() { + DefaultListModel dlm = new DefaultListModel(); + ListBoxMaps = new JList(dlm); + } +} diff --git a/FormShip.java b/FormShip.java index 8b9d20c..743e873 100644 --- a/FormShip.java +++ b/FormShip.java @@ -71,6 +71,11 @@ public class FormShip extends JDialog{ Draw(); } + public void setShipIn(DrawningObjectShip ship) + { + this.ship = ship.getDrawingShip(); + } + public FormShip() { Box LabelBox = Box.createHorizontalBox(); LabelBox.setMinimumSize(new Dimension(1, 20)); diff --git a/Main.java b/Main.java index bb8b139..426046d 100644 --- a/Main.java +++ b/Main.java @@ -2,8 +2,8 @@ import javax.swing.*; public class Main { public static void main(String[] args) { - JFrame frame = new JFrame("Hard №3"); - frame.setContentPane(new FormShipMixer().Mainpanel); + JFrame frame = new JFrame("Hard №4"); + frame.setContentPane(new FormMapWithSetShipsGeneric().Mainpanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(500, 200); frame.pack(); diff --git a/MapWithSetShipGeneric.java b/MapWithSetShipGeneric.java index 8991399..55658c9 100644 --- a/MapWithSetShipGeneric.java +++ b/MapWithSetShipGeneric.java @@ -1,5 +1,7 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.util.LinkedList; +import java.util.Queue; public class MapWithSetShipGeneric { private int _pictureWidth; @@ -8,9 +10,11 @@ public class MapWithSetShipGeneric _setShips; private U _map; + private Queue _deletedShips; public MapWithSetShipGeneric(int picWidth, int picHeight, U map) { + _deletedShips = new LinkedList<>(); int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _setShips = new SetShipGeneric(width * height); @@ -25,7 +29,9 @@ public class MapWithSetShipGeneric> _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<>(); + this._pictureWidth = pictureWidth; + this._pictureHeight = pictureHeight; + } + + public void AddMap(String Name, AbstractMap Map) + { + if(!_mapStorages.containsKey(Name)) + { + _mapStorages.put(Name,new MapWithSetShipGeneric<>(_pictureWidth,_pictureHeight,Map)); + } + } + + public void DelMap(String name) + { + _mapStorages.remove(name); + } + + public MapWithSetShipGeneric Get(String ind) + { + if(_mapStorages.containsKey(ind)) + { + return _mapStorages.get(ind); + } + return null; + } + + public DrawningObjectShip 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 ec5347c..48b493a 100644 --- a/SetShipGeneric.java +++ b/SetShipGeneric.java @@ -1,19 +1,16 @@ -public class SetShipGeneric { - private T[] _places; +import java.util.*; + +public class SetShipGeneric implements Iterable { + private ArrayList _places; + private final int _maxCount; public int getCount() { - return _places.length; + return _places.size(); } public SetShipGeneric(int count) { - _places = (T[])(new Object[count]); - } - - private boolean CanInsert(int position) - { - for (int i = position; i < _places.length; ++i) - if (_places[i] == null) return true; - return false; + _maxCount = count; + _places = new ArrayList<>(); } public int Insert(T ship) @@ -23,32 +20,35 @@ public class SetShipGeneric { public int Insert(T ship, int position) { - if (position < 0 || position > _places.length) return -1; - if (_places[position] != null && CanInsert(position)) - { - for (int i = _places.length - 1; i > position; --i) - { - if (_places[i] == null) - { - _places[i] = _places[i - 1]; - _places[i - 1] = null; - } - } - } - _places[position] = ship; + if (position < 0 || position > getCount() || _maxCount == getCount()) return -1; + _places.add(position,ship); return position; } public T Remove(int position) { - T DelElement = _places[position]; - _places[position] = null; + if (position < 0 || position >= getCount()) return null; + T DelElement = (T) _places.get(position); + _places.remove(position); return DelElement; } public T Get(int position) { - if (position < 0 || position > _places.length) return null; - return _places[position]; + if (position < 0 || position >= getCount()) 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(); } }