From 405904860a66150eb7c60a7a2554f059fe50c4d3 Mon Sep 17 00:00:00 2001 From: dimazhelovanov Date: Tue, 20 Dec 2022 20:50:09 +0300 Subject: [PATCH 01/10] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Direction.java | 3 +- DrawningBattleship.java | 4 + DrawningLinkor.java | 5 +- DrawningTriangleBlocks.java | 2 - FormBattleship.java | 135 ++++++++++++++++++++ FormMap.java | 5 +- FormMapWithSetBattleship.java | 197 ++++++++++++++++++++++++++++++ FormParameterClass.java | 96 +++++++++++++++ MapWithSetBattleshipsGeneric.java | 139 +++++++++++++++++++++ MyParametrClass.java | 39 ++++++ SetBattleshipGeneric.java | 59 +++++++++ 11 files changed, 676 insertions(+), 8 deletions(-) create mode 100644 FormBattleship.java create mode 100644 FormMapWithSetBattleship.java create mode 100644 FormParameterClass.java create mode 100644 MapWithSetBattleshipsGeneric.java create mode 100644 MyParametrClass.java create mode 100644 SetBattleshipGeneric.java diff --git a/Direction.java b/Direction.java index a641b80..3b7253a 100644 --- a/Direction.java +++ b/Direction.java @@ -2,5 +2,6 @@ public enum Direction { Up, Down, Left, - Right + Right, + None } diff --git a/DrawningBattleship.java b/DrawningBattleship.java index cc6d3e2..ccb8631 100644 --- a/DrawningBattleship.java +++ b/DrawningBattleship.java @@ -59,6 +59,10 @@ public class DrawningBattleship { Battleship = new EntityBattleship(speed, weight, bodyColor); } + public DrawningBattleship(EntityBattleship entity, IDrawningBlocks blocks){ + Battleship = entity; + iDrawingBlocks = blocks; + } protected DrawningBattleship(int speed, float weight, Color bodyColor, int battleshipWidth, int battleshipHeight) { this(speed, weight, bodyColor); diff --git a/DrawningLinkor.java b/DrawningLinkor.java index 565fd10..f4d44f0 100644 --- a/DrawningLinkor.java +++ b/DrawningLinkor.java @@ -6,7 +6,10 @@ public class DrawningLinkor extends DrawningBattleship { super(speed, weight, bodyColor, 120, 50); Battleship = new EntityLinkor(speed, weight, bodyColor, dopColor, turret, missileBay); } - + public DrawningLinkor(EntityBattleship battleship, IDrawningBlocks blocks){ + super(battleship, blocks); + Battleship = battleship; + } @Override public void DrawTransport(Graphics2D g) { diff --git a/DrawningTriangleBlocks.java b/DrawningTriangleBlocks.java index 4557e57..2b2485a 100644 --- a/DrawningTriangleBlocks.java +++ b/DrawningTriangleBlocks.java @@ -15,8 +15,6 @@ public class DrawningTriangleBlocks implements IDrawningBlocks { g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3); g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3); - - } case Six -> { g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 7, y, y + 7}, 3); diff --git a/FormBattleship.java b/FormBattleship.java new file mode 100644 index 0000000..9c46e6a --- /dev/null +++ b/FormBattleship.java @@ -0,0 +1,135 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentEvent; +import java.awt.event.ComponentListener; +import java.awt.image.BufferedImage; +import java.util.Random; +public class FormBattleship extends JComponent { + private AbstractMap _abstractMap; + private DrawningBattleship _battleship; + private DrawningBattleship SelectedBattleship; + private EntityBattleship _entityBattleship; + private BufferedImage bufferedImage = null; + + public DrawningBattleship GetSelectedBattleship() { + 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()); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.SOUTH); + Label speedLabel = new Label("Скорость: "); + Label weightLabel = new Label("Вес: "); + Label colorLabel = new Label("Цвет: "); + + + JButton createButton = new JButton("Создать"); + createButton.addActionListener(e -> { + + Random rnd = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Color", 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); + speedLabel.setText("Скорость: " + _battleship.Battleship.GetSpeed()); + weightLabel.setText("Вес: " + (int)_battleship.Battleship.GetWeight()); + colorLabel.setText("Цвет: " + _battleship.Battleship.GetBodyColor().getRed() + " " + + _battleship.Battleship.GetBodyColor().getGreen() + " " + _battleship.Battleship.GetBodyColor().getBlue() ); + repaint(); + + }); + 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))); + _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); + speedLabel.setText("Скорость: " + _battleship.Battleship.GetSpeed()); + weightLabel.setText("Вес: " + (int)_battleship.Battleship.GetWeight()); + colorLabel.setText("Цвет:: " + _battleship.Battleship.GetBodyColor().getRed() + " " + _battleship.Battleship.GetBodyColor().getGreen() + " " + _battleship.Battleship.GetBodyColor().getBlue() ); + if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(_battleship)); + repaint(); + }); + JButton selectButton = new JButton("Выбрать"); + selectButton.addActionListener(e -> { + SelectedBattleship = _battleship; + caller.dispose(); + }); + statusPanel.add(createButton); + statusPanel.add(modifiedButton); + statusPanel.add(selectButton); + statusPanel.add(speedLabel); + statusPanel.add(weightLabel); + statusPanel.add(colorLabel); + JButton upButton = new JButton("↑"); + JButton rightButton = new JButton("→"); + JButton leftButton = new JButton("←"); + JButton downButton = new JButton("↓"); + upButton.addActionListener(e -> { + if (_battleship != null) _battleship.MoveTransport(Direction.Up); + repaint(); + }); + + + rightButton.addActionListener(e -> { + if (_battleship != null) _battleship.MoveTransport(Direction.Right); + repaint(); + }); + + + leftButton.addActionListener(e -> { + if (_battleship != null) _battleship.MoveTransport(Direction.Left); + repaint(); + }); + + + downButton.addActionListener(e -> { + if (_battleship != null) _battleship.MoveTransport(Direction.Down); + repaint(); + }); + + statusPanel.add(leftButton); + statusPanel.add(upButton); + statusPanel.add(rightButton); + statusPanel.add(downButton); + + //getContentPane().add(this); + } + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (_battleship != null) _battleship.DrawTransport(g2); + super.repaint(); + } + +} \ No newline at end of file diff --git a/FormMap.java b/FormMap.java index 286fce5..9b4f5a2 100644 --- a/FormMap.java +++ b/FormMap.java @@ -10,9 +10,7 @@ public class FormMap extends JComponent { private AbstractMap _abstractMap; private DrawningBattleship _battleship; private BufferedImage bufferedImage; - public static void main(String[] args) { - FormMap formMap = new FormMap(); - } + public FormMap() { JFrame form = new JFrame("Карта"); form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -55,7 +53,6 @@ public class FormMap extends JComponent { createButton.addActionListener(e -> { Random rnd = new Random(); - var battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); diff --git a/FormMapWithSetBattleship.java b/FormMapWithSetBattleship.java new file mode 100644 index 0000000..f9ecbbc --- /dev/null +++ b/FormMapWithSetBattleship.java @@ -0,0 +1,197 @@ +import javax.swing.*; +import javax.swing.text.MaskFormatter; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.text.ParseException; + +public class FormMapWithSetBattleship extends JComponent { + private BufferedImage bufferedImage = null; + + private MapWithSetBattleshipsGeneric _mapBattleshipsCollectionGeneric; + public static void main(String[] args) { + FormMapWithSetBattleship formMap = new FormMapWithSetBattleship(); + } + public FormMapWithSetBattleship(){ + JFrame form = new JFrame("FormMapWithSetBattleship"); + form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + form.setSize(750, 500); + form.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new GridLayout(0, 1, 20, 20)); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.EAST); + String[] maps = { + "Простая карта", + "Морская карта", + }; + JComboBox 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); + + + JButton addAirbusButton = new JButton("Добавить корабль"); + addAirbusButton.addActionListener(e -> { + + if (_mapBattleshipsCollectionGeneric == null) + { + 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) { + JOptionPane.showMessageDialog(form, "Объект добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION); + bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet(); + repaint(); + } + else { + JOptionPane.showMessageDialog(form, "Объект не добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION); + } + }); + statusPanel.add(addAirbusButton); + + JFormattedTextField maskedTextFieldPosition = null; + try { + MaskFormatter positionMask = new MaskFormatter("##"); + maskedTextFieldPosition = new JFormattedTextField(positionMask); + statusPanel.add(maskedTextFieldPosition); + } + catch (ParseException e) { + e.printStackTrace(); + } + + JButton deleteAirbusButton = new JButton("Удалить корабль"); + JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition; + deleteAirbusButton.addActionListener(e -> { + if ((String)mapSelectComboBox.getSelectedItem() == null) { + return; + } + if (finalMaskedTextFieldPosition == null) { + return; + } + int position = Integer.parseInt(finalMaskedTextFieldPosition.getText()); + if (_mapBattleshipsCollectionGeneric.Subtraction(position) != null) { + JOptionPane.showMessageDialog(form, "Объект удален", "Удаление", JOptionPane.OK_CANCEL_OPTION); + bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet(); + repaint(); + } + else{ + JOptionPane.showMessageDialog(form, "Объект не удален", "Удаление", JOptionPane.OK_CANCEL_OPTION); + } + }); + statusPanel.add(deleteAirbusButton); + JButton showStorageButton = new JButton("Хранилище"); + showStorageButton.addActionListener(e -> { + + if (_mapBattleshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet(); + repaint(); + }); + statusPanel.add(showStorageButton); + + JButton showOnMapButton = new JButton("Карта"); + showOnMapButton.addActionListener(e -> { + + if (_mapBattleshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapBattleshipsCollectionGeneric.ShowOnMap(); + }); + statusPanel.add(showOnMapButton); + + ActionListener moveButtonListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_mapBattleshipsCollectionGeneric == null) + { + return; + } + String name = e.getActionCommand(); + Direction dir = Direction.None; + switch (name) + { + case "↑": + dir = Direction.Up; + break; + case "↓": + dir = Direction.Down; + break; + case "←": + dir = Direction.Left; + break; + case "→": + dir = Direction.Right; + break; + } + bufferedImage = _mapBattleshipsCollectionGeneric.MoveObject(dir); + } + }; + + //Кнопки управления + JButton moveDownButton = new JButton("↓"); + moveDownButton.addActionListener(moveButtonListener); + + JButton moveUpButton = new JButton("↑"); + moveUpButton.addActionListener(moveButtonListener); + + JButton moveLeftButton = new JButton("←"); + moveLeftButton.addActionListener(moveButtonListener); + + JButton moveRightButton = new JButton("→"); + moveRightButton.addActionListener(moveButtonListener); + + JButton showGalleryButton = new JButton("Галерея"); + showGalleryButton.addActionListener(e -> { + new FormParameterClass(); + }); + statusPanel.add(showGalleryButton); + statusPanel.add(moveUpButton); + statusPanel.add(moveDownButton); + statusPanel.add(moveLeftButton); + statusPanel.add(moveRightButton); + form.getContentPane().add(this); + form.setVisible(true); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,600,500,null); + super.repaint(); + } + } + diff --git a/FormParameterClass.java b/FormParameterClass.java new file mode 100644 index 0000000..c4c3d06 --- /dev/null +++ b/FormParameterClass.java @@ -0,0 +1,96 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormParameterClass extends JComponent { + private DrawningBattleship battleship1; + private DrawningBattleship battleship2; + private DrawningBattleship battleship3; + private DrawningBattleship battleship4; + private DrawningBattleship battleship5; + MyParametrClass parameterClass; + public FormParameterClass(){ + JFrame form = new JFrame("Экземпляры от параметризованного класса"); + form.setSize(900, 500); + form.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + form.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new FlowLayout()); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.SOUTH); + + + JButton showRandomEntity = new JButton("Создать"); + showRandomEntity.addActionListener(e -> { + Random random = new Random(); + int[] arrayBlocks = {2, 4, 6}; + if (parameterClass == null) { + parameterClass = new MyParametrClass(5, 5); + for (int i = 0; i < 5; 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)))); + } + else { + parameterClass.Insert(new EntityLinkor(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + random.nextBoolean(), random.nextBoolean())); + } + } + for (int i = 0; i < 5; i ++) { + int rnd = random.nextInt(3); + + switch (rnd) { + case 0: + parameterClass.Insert(new DrawningBlocks(arrayBlocks[random.nextInt(3)], + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 1: + parameterClass.Insert(new DrawningRoundBlocks(arrayBlocks[random.nextInt(3)])); + break; + + case 2: + parameterClass.Insert(new DrawningTriangleBlocks(arrayBlocks[random.nextInt(3)])); + break; + } + } + } + battleship1 = parameterClass.GetDrawningBattleship(); + battleship1.SetPosition(200, 200, form.getWidth(), form.getHeight() - 75); + + battleship2 = parameterClass.GetDrawningBattleship(); + battleship2.SetPosition(400, 200, form.getWidth(), form.getHeight() - 75); + + battleship3 = parameterClass.GetDrawningBattleship(); + battleship3.SetPosition(600, 200, form.getWidth(), form.getHeight() - 75); + + battleship4 = parameterClass.GetDrawningBattleship(); + battleship4.SetPosition(300, 300, form.getWidth(), form.getHeight() - 75); + + battleship5 = parameterClass.GetDrawningBattleship(); + battleship5.SetPosition(500, 300, form.getWidth(), form.getHeight() - 75); + repaint(); + }); + statusPanel.add(showRandomEntity); + + form.getContentPane().add(this); + + form.setVisible(true); +} + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (battleship1 != null) battleship1.DrawTransport(g2); + if (battleship2 != null) battleship2.DrawTransport(g2); + if (battleship3 != null) battleship3.DrawTransport(g2); + if (battleship4 != null) battleship4.DrawTransport(g2); + if (battleship5 != null) battleship5.DrawTransport(g2); + super.repaint(); + } +} + diff --git a/MapWithSetBattleshipsGeneric.java b/MapWithSetBattleshipsGeneric.java new file mode 100644 index 0000000..9ff08d0 --- /dev/null +++ b/MapWithSetBattleshipsGeneric.java @@ -0,0 +1,139 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetBattleshipsGeneric { + + /// Ширина окна отрисовки + private final int _pictureWidth; + /// Высота окна отрисовки + private final int _pictureHeight; + /// Размер занимаемого объектом места (ширина) + private final int _placeSizeWidth = 250; + /// Размер занимаемого объектом места (высота) + private final int _placeSizeHeight = 100; + private final SetBattleshipGeneric _setBattleship; + /// Карта + private final U _map; + /// Конструктор + public MapWithSetBattleshipsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setBattleship = new SetBattleshipGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + public int Add(T battleship) + { + return this._setBattleship.Insert(battleship); + } + public T Subtraction(int position) + { + return this._setBattleship.Remove(position); + } + public BufferedImage ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + DrawBackground((Graphics2D)gr); + DrawBattleship((Graphics2D)gr); + return bmp; + } + public BufferedImage ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setBattleship.Count(); i++) + { + var battleship = _setBattleship.Get(i); + if (battleship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, battleship); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + /// Перемещение объекта по крате + public BufferedImage MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + private void Shaking() + { + int j = _setBattleship.Count() - 1; + for (int i = 0; i < _setBattleship.Count(); i++) + { + if (_setBattleship.Get(i) == null) + { + for (; j > i; j--) + { + var battleship = _setBattleship.Get(j); + if (battleship != null) + { + _setBattleship.Insert(battleship, i); + _setBattleship.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + private void DrawBackground(Graphics2D g) + { + g.setColor(Color.blue); + //Brush seaBrush = new SolidBrush(Color.Aqua); + g.fillRect(0, 0, _pictureWidth, _pictureHeight); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + + for (int j = 0; j <= _pictureHeight / _placeSizeHeight; ++j) + { + g.setStroke(new BasicStroke(7)); + g.setColor(Color.black); + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * + _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + g.setColor(Color.RED); + g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight-5, i * _placeSizeWidth + 20, j * _placeSizeHeight - 23); + g.drawLine( i * _placeSizeWidth + 70, j * _placeSizeHeight - 5, i * _placeSizeWidth + 70, j * _placeSizeHeight - 23); + } + + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, + (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + + + } + } + private void DrawBattleship(Graphics2D g) + { + int countColumns = _pictureWidth / _placeSizeWidth - 1; + int countRows = _pictureHeight / _placeSizeHeight - 1; + int currentColumn = 0; + for (int i = 0; i < _setBattleship.Count(); i++) + { + if((_setBattleship.Get(i) != null)) + { + _setBattleship.Get(i).SetObject((currentColumn) * _placeSizeWidth + 10, (countRows) * _placeSizeHeight + 15, _pictureWidth, _pictureHeight); + _setBattleship.Get(i).DrawningObject(g); + } + if (currentColumn >= countColumns) + { + currentColumn = 0; + countRows--; + } + else + { + currentColumn++; + + } + } + } +} diff --git a/MyParametrClass.java b/MyParametrClass.java new file mode 100644 index 0000000..59abc76 --- /dev/null +++ b/MyParametrClass.java @@ -0,0 +1,39 @@ +import java.util.ArrayList; +import java.util.Random; + +public class MyParametrClass { + private final Object[] EntityArray; + private final Object[] BlocksArray; + int entitiesCount = 0; + int blocksCount = 0; + public MyParametrClass(int entitiesCount, int blocksCount) { + EntityArray = new Object[entitiesCount]; + BlocksArray = new Object[blocksCount]; + } + public void Insert(T battleship){ + if(entitiesCount < EntityArray.length) { + EntityArray[entitiesCount] = battleship; + entitiesCount++; + } + } + public void Insert(U drawningBlocks){ + if(blocksCount < BlocksArray.length) { + BlocksArray[blocksCount] = drawningBlocks; + blocksCount++; + } + } + public DrawningBattleship GetDrawningBattleship() { + + Random random = new Random(); + int entityIndex = random.nextInt(EntityArray.length); + int blockIndex = random.nextInt(BlocksArray.length); + + EntityBattleship battleship = (T)EntityArray[entityIndex]; + IDrawningBlocks blocks = (U)BlocksArray[blockIndex]; + + if (battleship instanceof EntityLinkor) { + return new DrawningLinkor(battleship, blocks); + } + return new DrawningLinkor(battleship, blocks); + } +} diff --git a/SetBattleshipGeneric.java b/SetBattleshipGeneric.java new file mode 100644 index 0000000..57416ab --- /dev/null +++ b/SetBattleshipGeneric.java @@ -0,0 +1,59 @@ +public class SetBattleshipGeneric +{ + private final T[] _places; + + public SetBattleshipGeneric(int count) { + _places = (T[]) new Object[count]; + } + + public int Count() { + return _places.length; + } + 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; + } + + 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; + return result; + } + + public T Get(int position) + { + if (position >= Count() || position < 0) + { + return null; + } + return _places[position]; + } +} -- 2.25.1 From 1d7e6e80a0762eded4b4f8006e030801ba399635 Mon Sep 17 00:00:00 2001 From: Zhelovanov_Dmitrii Date: Thu, 22 Dec 2022 21:55:10 +0400 Subject: [PATCH 02/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D0=BD=D0=B0=20'FormMapWithSetBattleship.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormMapWithSetBattleship.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FormMapWithSetBattleship.java b/FormMapWithSetBattleship.java index f9ecbbc..63d827f 100644 --- a/FormMapWithSetBattleship.java +++ b/FormMapWithSetBattleship.java @@ -89,9 +89,9 @@ 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; } @@ -108,7 +108,7 @@ public class FormMapWithSetBattleship extends JComponent { JOptionPane.showMessageDialog(form, "Объект не удален", "Удаление", JOptionPane.OK_CANCEL_OPTION); } }); - statusPanel.add(deleteAirbusButton); + statusPanel.add(deleteBattleshipButton); JButton showStorageButton = new JButton("Хранилище"); showStorageButton.addActionListener(e -> { -- 2.25.1 From 9a93dd2af5cecf378a01db63cbaf6cbdc76540d2 Mon Sep 17 00:00:00 2001 From: Zhelovanov_Dmitrii Date: Thu, 22 Dec 2022 21:57:30 +0400 Subject: [PATCH 03/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D0=BD=D0=B0=20'MyParametrClass.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MyParametrClass.java | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/MyParametrClass.java b/MyParametrClass.java index 59abc76..876b05f 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); -- 2.25.1 From 7384aa95142da374df89846562e4179aac9e0e2a Mon Sep 17 00:00:00 2001 From: Zhelovanov_Dmitrii Date: Thu, 22 Dec 2022 22:01:16 +0400 Subject: [PATCH 04/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D0=BD=D0=B0=20'FormParameterClass.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormParameterClass.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FormParameterClass.java b/FormParameterClass.java index c4c3d06..4b07de1 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) { -- 2.25.1 From 79c31bbb8bef0b3e31d34c8a3d56716b652c4ae8 Mon Sep 17 00:00:00 2001 From: Zhelovanov_Dmitrii Date: Thu, 22 Dec 2022 22:07:19 +0400 Subject: [PATCH 05/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D0=BD=D0=B0=20'FormMapWithSetBattleship.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormMapWithSetBattleship.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FormMapWithSetBattleship.java b/FormMapWithSetBattleship.java index 63d827f..bb9db36 100644 --- a/FormMapWithSetBattleship.java +++ b/FormMapWithSetBattleship.java @@ -54,8 +54,8 @@ public class FormMapWithSetBattleship extends JComponent { statusPanel.add(mapSelectComboBox); - JButton addAirbusButton = new JButton("Добавить корабль"); - addAirbusButton.addActionListener(e -> { + JButton addBattleshipButton = new JButton("Добавить корабль"); + addBattleshipButton.addActionListener(e -> { if (_mapBattleshipsCollectionGeneric == null) { @@ -77,7 +77,7 @@ public class FormMapWithSetBattleship extends JComponent { JOptionPane.showMessageDialog(form, "Объект не добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION); } }); - statusPanel.add(addAirbusButton); + statusPanel.add(addBattleshipButton); JFormattedTextField maskedTextFieldPosition = null; try { -- 2.25.1 From c56ee336efa2ea5ce613281ced978808080eddec Mon Sep 17 00:00:00 2001 From: Zhelovanov_Dmitrii Date: Thu, 22 Dec 2022 22:11:23 +0400 Subject: [PATCH 06/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D0=BD=D0=B0=20'FormBattleship.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormBattleship.java | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/FormBattleship.java b/FormBattleship.java index 9c46e6a..87f4583 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()); -- 2.25.1 From 8573ebc9cd0dcb4d92d051d7549f3a9541f77632 Mon Sep 17 00:00:00 2001 From: Zhelovanov_Dmitrii Date: Thu, 22 Dec 2022 22:11:40 +0400 Subject: [PATCH 07/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D0=BD=D0=B0=20'FormBattleship.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormBattleship.java | 1 - 1 file changed, 1 deletion(-) diff --git a/FormBattleship.java b/FormBattleship.java index 87f4583..1bf3eac 100644 --- a/FormBattleship.java +++ b/FormBattleship.java @@ -99,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); -- 2.25.1 From 9cce5599bf123c51e7ce8809ccfca36395d3d15f Mon Sep 17 00:00:00 2001 From: Zhelovanov_Dmitrii Date: Thu, 22 Dec 2022 22:16:36 +0400 Subject: [PATCH 08/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D0=BD=D0=B0=20'MapWithSetBattleshipsGeneric.j?= =?UTF-8?q?ava'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MapWithSetBattleshipsGeneric.java | 1 - 1 file changed, 1 deletion(-) diff --git a/MapWithSetBattleshipsGeneric.java b/MapWithSetBattleshipsGeneric.java index 9ff08d0..ef45509 100644 --- a/MapWithSetBattleshipsGeneric.java +++ b/MapWithSetBattleshipsGeneric.java @@ -90,7 +90,6 @@ public class MapWithSetBattleshipsGeneric Date: Thu, 22 Dec 2022 22:25:29 +0400 Subject: [PATCH 09/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D0=BD=D0=B0=20'FormBattleship.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormBattleship.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FormBattleship.java b/FormBattleship.java index 1bf3eac..23f6ab0 100644 --- a/FormBattleship.java +++ b/FormBattleship.java @@ -31,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); @@ -45,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); -- 2.25.1 From de0ba340c3e4cd3e952cd83c3a9120b413459647 Mon Sep 17 00:00:00 2001 From: Zhelovanov_Dmitrii Date: Thu, 22 Dec 2022 23:08:38 +0400 Subject: [PATCH 10/10] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D0=BD=D0=B0=20'FormParameterClass.java'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormParameterClass.java | 1 - 1 file changed, 1 deletion(-) diff --git a/FormParameterClass.java b/FormParameterClass.java index 4b07de1..a402da5 100644 --- a/FormParameterClass.java +++ b/FormParameterClass.java @@ -93,4 +93,3 @@ public class FormParameterClass extends JComponent { super.repaint(); } } - -- 2.25.1