diff --git a/DrawningBattleship.java b/DrawningBattleship.java index ccb8631..9ac721b 100644 --- a/DrawningBattleship.java +++ b/DrawningBattleship.java @@ -3,7 +3,6 @@ import java.util.Random; public class DrawningBattleship { EntityBattleship Battleship; - private DrawningBlocks drawingBlocks; private IDrawningBlocks iDrawingBlocks; public EntityBattleship Battleship() {return Battleship; } @@ -40,7 +39,6 @@ public class DrawningBattleship { public DrawningBattleship(int speed, float weight, Color bodyColor) { - //Battleship.Init(speed, weight, bodyColor); Random random = new Random(); int[] ArrayBlocks = new int[]{2, 4, 6}; @@ -193,6 +191,6 @@ public class DrawningBattleship { } public float[] GetCurrentPosition() { - return new float[] {_startPosY, _startPosX + _battleshipWidth, /*DOWN*/ _startPosY + _battleshipHeight, /*LEFT*/ _startPosX}; + return new float[] {_startPosY, _startPosX + _battleshipWidth, _startPosY + _battleshipHeight, _startPosX}; } } diff --git a/DrawningLinkor.java b/DrawningLinkor.java index f4d44f0..9fc7c36 100644 --- a/DrawningLinkor.java +++ b/DrawningLinkor.java @@ -22,7 +22,7 @@ public class DrawningLinkor extends DrawningBattleship { if (entityLinkor.turret) { - //Brush dopBrushRed = new SolidBrush(Color.Red); + g.setColor(entityLinkor.dopColor); g.fillOval((int)_startPosX + 15, (int)_startPosY, 20, 20); g.fillOval((int) _startPosX + 15, (int)_startPosY + 30, 20, 20); diff --git a/DrawningObjectBattleship.java b/DrawningObjectBattleship.java index 16a01a8..24bf694 100644 --- a/DrawningObjectBattleship.java +++ b/DrawningObjectBattleship.java @@ -6,6 +6,10 @@ public class DrawningObjectBattleship implements IDrawningObject { public DrawningObjectBattleship(DrawningBattleship battleship){ _battleship = battleship; } + public DrawningBattleship GetDrawningBattleship() { + return _battleship; + } + @Override public float getStep() { if (_battleship.Battleship != null) { diff --git a/FormBattleship.java b/FormBattleship.java index 9c46e6a..90e2b35 100644 --- a/FormBattleship.java +++ b/FormBattleship.java @@ -17,30 +17,6 @@ public class FormBattleship extends JComponent { return SelectedBattleship; } public FormBattleship(JDialog caller) { -// JFrame form = new JFrame("Военный корабль"); -// form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); -// form.setSize(800, 500); -// form.setVisible(true); -// form.setLocationRelativeTo(null); -// form.addComponentListener(new ComponentListener() { -// @Override -// public void componentResized(ComponentEvent e) { -// if(_battleship != null) _battleship.ChangeBorders(getWidth(), getHeight()); -// repaint(); -// } -// -// @Override -// public void componentMoved(ComponentEvent e) { -// } -// -// @Override -// public void componentShown(ComponentEvent e) { -// } -// -// @Override -// public void componentHidden(ComponentEvent e) { -// } -// }); Panel statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); statusPanel.setLayout(new FlowLayout()); @@ -55,7 +31,7 @@ public class FormBattleship extends JComponent { createButton.addActionListener(e -> { Random rnd = new Random(); - Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); _battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), colorFirst); _battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), getWidth(), getHeight() - 75); @@ -69,8 +45,8 @@ public class FormBattleship extends JComponent { JButton modifiedButton = new JButton("Модификация"); modifiedButton.addActionListener(e -> { Random rnd = new Random(); - Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); - Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + Color colorSecond = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); _battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst, colorSecond, rnd.nextBoolean(), rnd.nextBoolean()); _battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), getWidth(), getHeight() - 75); @@ -123,7 +99,6 @@ public class FormBattleship extends JComponent { statusPanel.add(rightButton); statusPanel.add(downButton); - //getContentPane().add(this); } protected void paintComponent(Graphics g) { super.paintComponent(g); @@ -131,5 +106,9 @@ public class FormBattleship extends JComponent { if (_battleship != null) _battleship.DrawTransport(g2); super.repaint(); } + public void SetBattleship(DrawningObjectBattleship battleship){ + _battleship = battleship.GetDrawningBattleship(); + repaint(); + } } \ No newline at end of file diff --git a/FormMapWithSetBattleship.java b/FormMapWithSetBattleship.java index f9ecbbc..e2ebfb7 100644 --- a/FormMapWithSetBattleship.java +++ b/FormMapWithSetBattleship.java @@ -5,10 +5,23 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.text.ParseException; +import java.util.HashMap; public class FormMapWithSetBattleship extends JComponent { private BufferedImage bufferedImage = null; + JList listBoxMaps; + Panel statusPanel; + DefaultListModel mapsListModel; + JComboBox mapSelectComboBox; + private final HashMap _mapsDict = new HashMap<>() { + { + put("Простая карта", new SimpleMap()); + put("Морская карта", new SeaMap()); + + } + }; + private final MapsCollection _mapsCollection; private MapWithSetBattleshipsGeneric _mapBattleshipsCollectionGeneric; public static void main(String[] args) { FormMapWithSetBattleship formMap = new FormMapWithSetBattleship(); @@ -19,7 +32,7 @@ public class FormMapWithSetBattleship extends JComponent { form.setSize(750, 500); form.setLocationRelativeTo(null); - Panel statusPanel = new Panel(); + statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); statusPanel.setLayout(new GridLayout(0, 1, 20, 20)); setLayout(new BorderLayout()); @@ -28,56 +41,88 @@ public class FormMapWithSetBattleship extends JComponent { "Простая карта", "Морская карта", }; - JComboBox mapSelectComboBox = new JComboBox(maps); + + mapSelectComboBox = new JComboBox(maps); mapSelectComboBox.setEditable(true); - mapSelectComboBox.addActionListener(e -> { - AbstractMap map = null; - String item = (String)mapSelectComboBox.getSelectedItem(); - switch (item) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Морская карта": - map = new SeaMap(); - break; - } - if (map != null) - { - _mapBattleshipsCollectionGeneric = new MapWithSetBattleshipsGeneric - (1000, 700, map); - } - else - { - _mapBattleshipsCollectionGeneric = null; - } - }); statusPanel.add(mapSelectComboBox); + mapsListModel = new DefaultListModel<>(); - JButton addAirbusButton = new JButton("Добавить корабль"); - addAirbusButton.addActionListener(e -> { + _mapsCollection = new MapsCollection(800, 500); + mapSelectComboBox.removeAllItems(); + for (var elem : _mapsDict.keySet()) + { + mapSelectComboBox.addItem(elem); + } + JScrollPane listMapPane = new JScrollPane(); + listBoxMaps = new JList(mapsListModel); + listBoxMaps.addListSelectionListener(e -> { + if(listBoxMaps.getSelectedValue() == null) return; + bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet(); + repaint(); + }); + statusPanel.add(listBoxMaps); + listMapPane.setViewportView(listBoxMaps); + listBoxMaps.setLayoutOrientation(JList.VERTICAL); + statusPanel.add(listMapPane); + TextField textFieldMapName = new TextField(); + statusPanel.add(textFieldMapName); + statusPanel.add(mapSelectComboBox); + JButton addMapButton = new JButton("Добавить карту"); + addMapButton.addActionListener(e -> { - if (_mapBattleshipsCollectionGeneric == null) + if (mapSelectComboBox.getSelectedIndex() == -1 || textFieldMapName.getText() == null) { return; } - JDialog dialog = new JDialog(form, "Диалого", true); + if (!_mapsDict.containsKey(mapSelectComboBox.getSelectedItem().toString())) + { + return; + } + _mapsCollection.AddMap(textFieldMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString())); + ReloadMaps(); + }); + + statusPanel.add(addMapButton); + JButton deleteMapButton = new JButton("Удалить карту"); + deleteMapButton.addActionListener(e -> { + // логика удаления + if (listBoxMaps.getSelectedIndex() == -1) + { + return; + } + if(listBoxMaps.getSelectedValue().toString() == null) return; + _mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString()); + ReloadMaps(); + repaint(); + }); + statusPanel.add(deleteMapButton); + + + JButton addBattleshipButton = new JButton("Добавить корабль"); + addBattleshipButton.addActionListener(e -> { + + if (listBoxMaps.getSelectedIndex() == -1) + { + return; + } + JDialog dialog = new JDialog(form, "Диалог", true); FormBattleship formShip = new FormBattleship(dialog); dialog.setSize(800, 500); dialog.setContentPane(formShip); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); DrawningObjectBattleship battleship = new DrawningObjectBattleship(formShip.GetSelectedBattleship()); - if (_mapBattleshipsCollectionGeneric.Add(battleship) != -1) { + if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Add(battleship)!= -1) { JOptionPane.showMessageDialog(form, "Объект добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION); - bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet(); + bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet(); repaint(); } else { JOptionPane.showMessageDialog(form, "Объект не добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION); } }); - statusPanel.add(addAirbusButton); + statusPanel.add(addBattleshipButton); JFormattedTextField maskedTextFieldPosition = null; try { @@ -89,9 +134,10 @@ public class FormMapWithSetBattleship extends JComponent { e.printStackTrace(); } - JButton deleteAirbusButton = new JButton("Удалить корабль"); + + JButton deleteBattleshipButton = new JButton("Удалить корабль"); JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition; - deleteAirbusButton.addActionListener(e -> { + deleteBattleshipButton.addActionListener(e -> { if ((String)mapSelectComboBox.getSelectedItem() == null) { return; } @@ -99,16 +145,19 @@ public class FormMapWithSetBattleship extends JComponent { return; } int position = Integer.parseInt(finalMaskedTextFieldPosition.getText()); - if (_mapBattleshipsCollectionGeneric.Subtraction(position) != null) { - JOptionPane.showMessageDialog(form, "Объект удален", "Удаление", JOptionPane.OK_CANCEL_OPTION); - bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet(); + if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Subtraction(position) != null) { + JOptionPane.showMessageDialog(form, "Объект удален", "Успех", JOptionPane.OK_CANCEL_OPTION); + bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet(); repaint(); } else{ JOptionPane.showMessageDialog(form, "Объект не удален", "Удаление", JOptionPane.OK_CANCEL_OPTION); } }); - statusPanel.add(deleteAirbusButton); + + + + statusPanel.add(deleteBattleshipButton); JButton showStorageButton = new JButton("Хранилище"); showStorageButton.addActionListener(e -> { @@ -123,19 +172,19 @@ public class FormMapWithSetBattleship extends JComponent { JButton showOnMapButton = new JButton("Карта"); showOnMapButton.addActionListener(e -> { - - if (_mapBattleshipsCollectionGeneric == null) + if (listBoxMaps.getSelectedIndex() == -1) { return; } - bufferedImage = _mapBattleshipsCollectionGeneric.ShowOnMap(); + bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap(); + repaint(); }); statusPanel.add(showOnMapButton); ActionListener moveButtonListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapBattleshipsCollectionGeneric == null) + if (listBoxMaps.getSelectedIndex() == -1) { return; } @@ -156,7 +205,7 @@ public class FormMapWithSetBattleship extends JComponent { dir = Direction.Right; break; } - bufferedImage = _mapBattleshipsCollectionGeneric.MoveObject(dir); + bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).MoveObject(dir); } }; @@ -177,7 +226,27 @@ public class FormMapWithSetBattleship extends JComponent { showGalleryButton.addActionListener(e -> { new FormParameterClass(); }); + JDialog dialog = new JDialog(form, "Dialog", true); statusPanel.add(showGalleryButton); + JButton showDeleteObjButton = new JButton("Удаленные корабли"); + showDeleteObjButton.addActionListener(e -> { + + DrawningObjectBattleship battleship = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).GetDeleted(); + if (battleship == null) { + JOptionPane.showMessageDialog(null, "Больше нет"); + } + if (battleship != null) { + + FormBattleship formBattleship = new FormBattleship(dialog); + formBattleship.SetBattleship(battleship); + dialog.setSize(800, 500); + dialog.setContentPane(formBattleship); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + } + }); + statusPanel.add(showDeleteObjButton); + statusPanel.add(moveUpButton); statusPanel.add(moveDownButton); statusPanel.add(moveLeftButton); @@ -185,7 +254,23 @@ public class FormMapWithSetBattleship extends JComponent { form.getContentPane().add(this); form.setVisible(true); } - + private void ReloadMaps(){ + int index = listBoxMaps.getSelectedIndex(); + listBoxMaps.removeAll(); + mapsListModel.removeAllElements(); + for (int i = 0; i < _mapsCollection.Keys().size(); i++){ + mapsListModel.addElement(_mapsCollection.Keys().get(i)); + } + if (mapsListModel.size() > 0 && (index == -1 || index >= mapsListModel.size())){ + listBoxMaps.setSelectedIndex(0); + } + else if (mapsListModel.size() > 0 && index > -1 && index < mapsListModel.size()) + { + listBoxMaps.setSelectedIndex(index); + } + listBoxMaps.setModel(mapsListModel); + statusPanel.repaint(); + } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); diff --git a/FormParameterClass.java b/FormParameterClass.java index c4c3d06..a402da5 100644 --- a/FormParameterClass.java +++ b/FormParameterClass.java @@ -27,8 +27,8 @@ public class FormParameterClass extends JComponent { Random random = new Random(); int[] arrayBlocks = {2, 4, 6}; if (parameterClass == null) { - parameterClass = new MyParametrClass(5, 5); - for (int i = 0; i < 5; i ++) { + parameterClass = new MyParametrClass(20, 20); + for (int i = 0; i < 20; i ++) { if (random.nextBoolean()) { parameterClass.Insert(new EntityBattleship(random.nextInt(100), random.nextInt(100), new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); @@ -40,7 +40,7 @@ public class FormParameterClass extends JComponent { random.nextBoolean(), random.nextBoolean())); } } - for (int i = 0; i < 5; i ++) { + for (int i = 0; i < 20; i ++) { int rnd = random.nextInt(3); switch (rnd) { @@ -93,4 +93,3 @@ public class FormParameterClass extends JComponent { super.repaint(); } } - diff --git a/MapWithSetBattleshipsGeneric.java b/MapWithSetBattleshipsGeneric.java index 9ff08d0..2d6cf74 100644 --- a/MapWithSetBattleshipsGeneric.java +++ b/MapWithSetBattleshipsGeneric.java @@ -1,5 +1,6 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.util.LinkedList; public class MapWithSetBattleshipsGeneric { @@ -8,12 +9,13 @@ public class MapWithSetBattleshipsGeneric _setBattleship; + private final int _placeSizeHeight = 90; + public final SetBattleshipGeneric _setBattleship; /// Карта private final U _map; + LinkedList deletedBattleships = new LinkedList<>(); /// Конструктор public MapWithSetBattleshipsGeneric(int picWidth, int picHeight, U map) { @@ -30,7 +32,11 @@ public class MapWithSetBattleshipsGeneric > _mapStorages; + private final int _pictureWidth; + private final int _pictureHeight; + + public ArrayList Keys() { + return new ArrayList(_mapStorages.keySet()); + } + 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 MapWithSetBattleshipsGeneric(_pictureWidth, _pictureHeight, map)); + + } + // TODO Прописать логику для добавления + } + public void DelMap(String name) + { + if (_mapStorages.containsKey(name)) { _mapStorages.remove(name); } + } + public MapWithSetBattleshipsGeneric Get(String ind) + { + if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind); + return null; + } + public DrawningObjectBattleship Get(String name, int pos) + { + if (_mapStorages.containsKey(name)) return _mapStorages.get(name)._setBattleship.Get(pos); + return null; + } + +} diff --git a/MyParametrClass.java b/MyParametrClass.java index 59abc76..497580d 100644 --- a/MyParametrClass.java +++ b/MyParametrClass.java @@ -2,34 +2,35 @@ import java.util.ArrayList; import java.util.Random; public class MyParametrClass { - private final Object[] EntityArray; - private final Object[] BlocksArray; + private final ArrayList EntityArray; + private final ArrayList BlocksArray; int entitiesCount = 0; int blocksCount = 0; public MyParametrClass(int entitiesCount, int blocksCount) { - EntityArray = new Object[entitiesCount]; - BlocksArray = new Object[blocksCount]; + EntityArray = new ArrayList(); + BlocksArray = new ArrayList(); } public void Insert(T battleship){ - if(entitiesCount < EntityArray.length) { - EntityArray[entitiesCount] = battleship; + + EntityArray.add(battleship); entitiesCount++; - } + } public void Insert(U drawningBlocks){ - if(blocksCount < BlocksArray.length) { - BlocksArray[blocksCount] = drawningBlocks; + + BlocksArray.add(drawningBlocks); blocksCount++; - } + } public DrawningBattleship GetDrawningBattleship() { Random random = new Random(); - int entityIndex = random.nextInt(EntityArray.length); - int blockIndex = random.nextInt(BlocksArray.length); + int entityIndex = random.nextInt(EntityArray.size()); + int blockIndex = random.nextInt( BlocksArray.size()); - EntityBattleship battleship = (T)EntityArray[entityIndex]; - IDrawningBlocks blocks = (U)BlocksArray[blockIndex]; + + EntityBattleship battleship = EntityArray.get(entityIndex); + IDrawningBlocks blocks = BlocksArray.get(blockIndex); if (battleship instanceof EntityLinkor) { return new DrawningLinkor(battleship, blocks); diff --git a/SetBattleshipGeneric.java b/SetBattleshipGeneric.java index 57416ab..cda3ec7 100644 --- a/SetBattleshipGeneric.java +++ b/SetBattleshipGeneric.java @@ -1,50 +1,34 @@ +import java.util.ArrayList; + public class SetBattleshipGeneric { - private final T[] _places; - - public SetBattleshipGeneric(int count) { - _places = (T[]) new Object[count]; - } - + private final ArrayList _places; + private final int _maxCount; public int Count() { - return _places.length; + return _places.size(); } + public SetBattleshipGeneric(int count) { + + _maxCount = count; + _places = new ArrayList<>();; + } + public int Insert(T battleship) { return Insert(battleship, 0); } public int Insert(T battleship, int position) { - if (position >= _places.length || position < 0) return -1; - if (_places[position] == null) { - _places[position] = battleship; - return position; + if (position >= _maxCount|| position < 0) return -1; + _places.add(position, battleship); + return position; } - int emptyEl = -1; - 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] = battleship; - return position; - } + public T Remove (int position) { - if (position >= _places.length || position < 0) return null; - T result = _places[position]; - _places[position] = null; + if (position >= _places.size() || position < 0) return null; + T result = _places.get(position); + _places.remove(position); return result; } @@ -54,6 +38,10 @@ public class SetBattleshipGeneric { return null; } - return _places[position]; + return _places.get(position); + } + public Iterable GetBattleships() + { + return _places; } }