diff --git a/src/DrawingField.java b/src/DrawingField.java deleted file mode 100644 index 35c9228..0000000 --- a/src/DrawingField.java +++ /dev/null @@ -1,60 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.util.Random; - -public class DrawingField extends JPanel{ - private final FormWarship Field; - private DrawingWarship _warship=null; - - public DrawingWarship SelectedWarship; - public DrawingField(FormWarship field) { - Field = field; - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - Graphics2D g2 =(Graphics2D)g; - if (_warship!=null) - _warship.DrawTransport(g2); - else return; - } - public void DirectionButtonAction(Direction side){ - if(_warship == null) - return; - _warship.MoveTransport(side); - } - private void SetData() { - Random rand=new Random(); - _warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); - Field.SpeedLabel.setText("Скорость: "+_warship.GetWarship().GetSpeed()); - Field.WeightLabel.setText("Вес: "+_warship.GetWarship().GetWeight()); - Field.BodyColorLabel.setText("Цвет: "+Integer.toHexString(_warship.GetWarship().GetBodyColor().getRGB()).substring(2)); - } - public void CreateButtonAction(){ - Random rand=new Random(); - _warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),rand.nextInt(3)); - SetData(); - } - public void CreateModifButtonAction(){ - Random rand=new Random(); - _warship = new DrawingAdvancedWarship(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), - new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3)); - SetData(); - } - public void ResizeField(){ - if (_warship!=null) - _warship.ChangeBorders(getWidth(),getHeight()); - else return; - } - - public void Draw(Graphics2D graphics) { - if (_warship!=null) - _warship.DrawTransport(graphics); - else return; - } - public DrawingWarship GetDrawingWarship() { - return _warship; - } - -} diff --git a/src/DrawingObjectWarship.java b/src/DrawingObjectWarship.java index 920ae47..4b0d4cb 100644 --- a/src/DrawingObjectWarship.java +++ b/src/DrawingObjectWarship.java @@ -2,7 +2,7 @@ import java.awt.*; public class DrawingObjectWarship implements IDrawingObject { - private DrawingWarship _warship=null; + private DrawingWarship _warship; public DrawingObjectWarship(DrawingWarship warship) { @@ -36,4 +36,7 @@ public class DrawingObjectWarship implements IDrawingObject { return _warship.GetCurrentPosition(); return null; } + public DrawingWarship GetWarship() { + return _warship; + } } diff --git a/src/FormMapWithSetWarships.form b/src/FormMapWithSetWarships.form index 4e1319a..0904dc6 100644 --- a/src/FormMapWithSetWarships.form +++ b/src/FormMapWithSetWarships.form @@ -3,7 +3,7 @@ - + @@ -23,7 +23,7 @@ - + @@ -33,20 +33,9 @@ - - - - - - - - - - - - + @@ -67,7 +56,7 @@ - + @@ -75,7 +64,7 @@ - + @@ -83,7 +72,7 @@ - + @@ -93,7 +82,7 @@ - + @@ -103,7 +92,7 @@ - + @@ -113,7 +102,7 @@ - + @@ -131,7 +120,7 @@ - + @@ -139,6 +128,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/FormMapWithSetWarships.java b/src/FormMapWithSetWarships.java index d8da024..90ef3e5 100644 --- a/src/FormMapWithSetWarships.java +++ b/src/FormMapWithSetWarships.java @@ -1,6 +1,10 @@ import javax.swing.*; import java.awt.*; -import java.awt.image.BufferedImage; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Optional; public class FormMapWithSetWarships extends JFrame{ @@ -17,8 +21,20 @@ public class FormMapWithSetWarships extends JFrame{ private JPanel PictureBox; private JPanel MainPanel; private JPanel GroupBoxTools; - private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric; + private JTextField TextFieldMap; + private JButton CreateMapButton; + private JButton DeleteMapButton; + private JList ListBoxMaps; + private JPanel MapPanel; + private JButton ButtonShowDeleted; private Image bufferedImage; + private MapsCollection _mapsCollection; + private final HashMap _mapsDict=new HashMap(){ + { + put("Первая карта",new SimpleMap()); + put("Вторая карта",new SecondMap()); + } + }; public FormMapWithSetWarships(){ setTitle("Военный корабль"); @@ -28,6 +44,11 @@ public class FormMapWithSetWarships extends JFrame{ ShowWindow(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); + _mapsCollection=new MapsCollection(getWidth(),getHeight()); + СomboBoxSelectorMap.removeAllItems(); + for(String elem:_mapsDict.keySet()){ + СomboBoxSelectorMap.addItem(elem); + } } @Override @@ -40,28 +61,79 @@ public class FormMapWithSetWarships extends JFrame{ } } + private void ReloadMaps() + { + int index = ListBoxMaps.getSelectedIndex(); + ListBoxMaps.setListData(_mapsCollection.Keys().toArray(new String[0])); + + 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); + } + repaint(); + } + private void ShowWindow(){ - ButtonShowOnMap.addActionListener(e -> { - if (_mapWarshipsCollectionGeneric == null) + ListBoxMaps.addListSelectionListener(e -> { + if(ListBoxMaps.getSelectedIndex()==-1) + return; + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); + repaint(); + }); + + DeleteMapButton.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) { return; } - bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap(); + if(JOptionPane.showConfirmDialog(this,"Удалить карту "+ListBoxMaps.getSelectedValue().toString()+"?", + "Удаление",JOptionPane.YES_NO_OPTION)==0) + { + _mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString()); + ReloadMaps(); + } + }); + + CreateMapButton.addActionListener(e -> { + if (СomboBoxSelectorMap.getSelectedIndex() == -1 || TextFieldMap.getText()==null || TextFieldMap.getText().equals("")) + { + JOptionPane.showMessageDialog(this,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE); + return; + } + if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem())) + { + JOptionPane.showMessageDialog(this,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE); + return; + } + _mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(СomboBoxSelectorMap.getSelectedItem().toString())); + ReloadMaps(); + }); + + ButtonShowOnMap.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() ==-1) + { + return; + } + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap(); repaint(); }); ButtonShowStorage.addActionListener(e -> { - if (_mapWarshipsCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() ==-1) { return; } - bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); repaint(); }); ButtonAddWarship.addActionListener(e -> { - if (_mapWarshipsCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() ==-1) { return; } @@ -73,9 +145,9 @@ public class FormMapWithSetWarships extends JFrame{ if (dialog.getSelectedWarship()!=null) { DrawingObjectWarship warship = new DrawingObjectWarship(dialog.getSelectedWarship()); - if (_mapWarshipsCollectionGeneric.plus(warship) >= 0) { + if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).plus(warship)>=0) { JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); repaint(); } else { JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE); @@ -85,7 +157,7 @@ public class FormMapWithSetWarships extends JFrame{ ButtonRemoveWarship.addActionListener(e -> { String txt=TextBoxPosition.getText(); - if (txt==null||_mapWarshipsCollectionGeneric==null||txt.isEmpty()) + if (txt==null||ListBoxMaps.getSelectedIndex() ==-1) { return; } @@ -94,15 +166,12 @@ public class FormMapWithSetWarships extends JFrame{ "Удалить объект?", "Удаление", JOptionPane.YES_NO_CANCEL_OPTION); - if (result == JOptionPane.NO_OPTION) - { + if (result!=0) return; - } int pos = Integer.parseInt(txt); - if (_mapWarshipsCollectionGeneric.minus(pos) !=null) - { - JOptionPane.showMessageDialog(this,"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).minus(pos)!=null){ + JOptionPane.showMessageDialog(this, "Объект удален", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); repaint(); } else @@ -111,53 +180,44 @@ public class FormMapWithSetWarships extends JFrame{ } }); - СomboBoxSelectorMap.addActionListener(e -> { - AbstractMap map = null; - - switch (СomboBoxSelectorMap.getSelectedItem().toString()) - { - case "Первая карта": - map = new SimpleMap(); - break; - case "Вторая карта": - map = new SecondMap(); - break; - } - if (map != null) - { - _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric<>( - PictureBox.getWidth(), PictureBox.getHeight(), map); - } - else - { - _mapWarshipsCollectionGeneric = null; + ButtonShowDeleted.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex()==-1) + return; + DrawingObjectWarship warship=(DrawingObjectWarship)_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetWarshipInDeleted(); + if(warship!=null){ + FormWarship dialog=new FormWarship(); + dialog.SetDrawingWarship(warship.GetWarship()); + dialog.setSize(1200,700); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); } }); ButtonUp.addActionListener(e -> { - if (_mapWarshipsCollectionGeneric != null) { - bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Up); + if (ListBoxMaps.getSelectedIndex() !=-1) { + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up); repaint(); } }); ButtonDown.addActionListener(e -> { - if (_mapWarshipsCollectionGeneric != null) { - bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Down); + if (ListBoxMaps.getSelectedIndex() !=-1) { + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down); repaint(); } }); ButtonRight.addActionListener(e -> { - if (_mapWarshipsCollectionGeneric != null) { - bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Right); + if (ListBoxMaps.getSelectedIndex() !=-1) { + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right); repaint(); } }); ButtonLeft.addActionListener(e -> { - if (_mapWarshipsCollectionGeneric != null) { - bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Left); + if (ListBoxMaps.getSelectedIndex() !=-1) { + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left); repaint(); } }); diff --git a/src/FormWarship.java b/src/FormWarship.java index 3cf0105..074d5c9 100644 --- a/src/FormWarship.java +++ b/src/FormWarship.java @@ -2,11 +2,11 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.util.Random; public class FormWarship extends JDialog{ private int Width; private int Height; - DrawingField field = new DrawingField(this); DrawingWarship SelectedWarship; private JButton ButtonDown; private JButton ButtonRight; @@ -19,47 +19,76 @@ public class FormWarship extends JDialog{ public JLabel BodyColorLabel; private JPanel PictureBox; private JButton ButtonSelect; + private DrawingWarship _warship=null; - public FormWarship(JFrame frame){ - super(frame,"Военный корабль"); + public FormWarship(){ + setTitle("Военный корабль"); setContentPane(PictureBox); - setSize(1000,700); - Width = getWidth(); - Height = getHeight(); + setSize(1000,500); + setResizable(false); ShowWindow(); - field.setBounds(0,0,Width,Height); setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); - setVisible(true); + } + + private void SetData() { + Random rand=new Random(); + _warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); + SpeedLabel.setText("Скорость: "+_warship.GetWarship().GetSpeed()); + WeightLabel.setText("Вес: "+_warship.GetWarship().GetWeight()); + BodyColorLabel.setText("Цвет: "+Integer.toHexString(_warship.GetWarship().GetBodyColor().getRGB()).substring(2)); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + + if (_warship != null) { + PictureBox.paintComponents(PictureBox.getGraphics()); + _warship.DrawTransport(g); + } } private void ShowWindow(){ ButtonCreate.addActionListener(e -> { - field.CreateButtonAction(); - ReDraw(); + Random rand=new Random(); + _warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),rand.nextInt(3)); + SetData(); + repaint(); }); ButtonCreateModif.addActionListener(e -> { - field.CreateModifButtonAction(); - ReDraw(); + Random rand=new Random(); + _warship = new DrawingAdvancedWarship(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), + new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3)); + SetData(); + repaint(); }); ButtonUp.addActionListener(e -> { - field.DirectionButtonAction(Direction.Up); - ReDraw(); + if(_warship == null) + return; + _warship.MoveTransport(Direction.Up); + repaint(); }); ButtonLeft.addActionListener(e -> { - field.DirectionButtonAction(Direction.Left); - ReDraw(); + if(_warship == null) + return; + _warship.MoveTransport(Direction.Left); + repaint(); }); ButtonRight.addActionListener(e -> { - field.DirectionButtonAction(Direction.Right); - ReDraw(); + if(_warship == null) + return; + _warship.MoveTransport(Direction.Right); + repaint(); }); ButtonDown.addActionListener(e -> { - field.DirectionButtonAction(Direction.Down); - ReDraw(); + if(_warship == null) + return; + _warship.MoveTransport(Direction.Down); + repaint(); }); ButtonSelect.addActionListener(e -> { - SelectedWarship=field.GetDrawingWarship(); + SelectedWarship=GetDrawingWarship(); JOptionPane.showMessageDialog(PictureBox, "Корабль добавлен."); }); @@ -69,17 +98,20 @@ public class FormWarship extends JDialog{ super.componentResized(e); Width=getWidth(); Height=getHeight(); - - field.ResizeField(); - ReDraw(); - field.setBounds(0,0,Width,Height); + if (_warship!=null) + _warship.ChangeBorders(getWidth(),getHeight()); + else return; + repaint(); + setBounds(0,0,Width,Height); } }); } - private void ReDraw() { - Graphics2D graphics = (Graphics2D) PictureBox.getGraphics(); - graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); - PictureBox.paintComponents(graphics); - field.Draw(graphics); + public DrawingWarship GetDrawingWarship() { + return _warship; + } + public void SetDrawingWarship(DrawingWarship warship) { + _warship=warship; + SetData(); + repaint(); } } diff --git a/src/MapWithSetWarshipsGeneric.java b/src/MapWithSetWarshipsGeneric.java index dee5f53..6eca910 100644 --- a/src/MapWithSetWarshipsGeneric.java +++ b/src/MapWithSetWarshipsGeneric.java @@ -1,5 +1,6 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.util.LinkedList; public class MapWithSetWarshipsGeneric { private final int _pictureWidth; @@ -7,6 +8,7 @@ public class MapWithSetWarshipsGeneric _setWarship; + private final LinkedList DeletedWarships; private final U _map; public MapWithSetWarshipsGeneric(int picWidth,int picHeight, U map) @@ -17,6 +19,7 @@ public class MapWithSetWarshipsGeneric(); } public int plus(T warship) @@ -26,7 +29,9 @@ public class MapWithSetWarshipsGeneric> _mapStorages; + private final int _pictureWidth; + private final int _pictureHeight; + + public MapsCollection(int pictureWidth,int pictureHeight){ + _mapStorages=new HashMap<>(); + _pictureWidth=pictureWidth; + _pictureHeight=pictureHeight; + } + + public ArrayList Keys(){ + return new ArrayList<>(_mapStorages.keySet()); + } + + public void AddMap(String name, AbstractMap map){ + if (_mapStorages.containsKey(name)){ + return; + } + else{ + _mapStorages.put(name,new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map)); + } + } + + public void DelMap(String name){ + _mapStorages.remove(name); + } + + public MapWithSetWarshipsGeneric get(String name){ + if (_mapStorages.containsKey(name)){ + return _mapStorages.get(name); + } + return null; + } + + public DrawingObjectWarship get(String name,int i) { + if (_mapStorages.containsKey(name)) + { + return _mapStorages.get(name).GetWarshipInList(i); + } + return null; + } +} diff --git a/src/SetWarshipsGeneric.java b/src/SetWarshipsGeneric.java index ab5ca68..e72f0cf 100644 --- a/src/SetWarshipsGeneric.java +++ b/src/SetWarshipsGeneric.java @@ -1,68 +1,63 @@ -public class SetWarshipsGeneric { - private final Object[] _places; +import java.util.ArrayList; +import java.util.Iterator; + +public class SetWarshipsGeneric implements Iterable{ + private final ArrayList _places; public int Count() { - return _places.length; + return _places.size(); } + private final int _maxCount; + public SetWarshipsGeneric(int count) { - _places = new Object[count]; + _maxCount=count; + _places = new ArrayList<>(); } public int Insert(T warship) { - return Insert(warship,0); + if (_places.size()+1>=_maxCount) + return -1; + _places.add(0,warship); + return 0; } public int Insert(T warship, int position) { - int EmptyEl=-1; - - if (position>=Count() || position < 0) + if (position>=_maxCount||position<0) return -1; - - if (_places[position] == null) - { - _places[position] = warship; - return 1; - } - - else if (_places[position] != null) - { - for (int i = position + 1; i < Count(); i++) - if (_places[i] == null) - { - EmptyEl = i; - break; - } - - if (EmptyEl == -1) - return -1; - - for (int i = EmptyEl; i > position; i--) - _places[i] = _places[i - 1]; - } - - _places[position] = warship; - return 1; + if (_places.size()+1>=_maxCount) + return -1; + _places.add(position,warship); + return position; } public T Remove(int position) { - if (position >= Count() || position < 0 || _places[position]==null) + if (position>=_maxCount||position<0) return null; - - T deleted =(T)_places[position]; - _places[position] = null; + T deleted=_places.get(position); + _places.remove(position); return deleted; } public T Get(int position) { - if (position >= Count() || position < 0) + if (position>=_maxCount||position<0) return null; + return _places.get(position); + } - return (T)_places[position]; + public void Set(int position,T warship){ + if (position >= _maxCount||position<0) + return; + Insert(warship,position); + } + + @Override + public Iterator iterator(){ + return _places.iterator(); } }