From 30e3d67aeb9887a27e94f6ec237f82ac67acac72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Thu, 15 Dec 2022 22:28:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractMap.java | 2 +- DrawingShip.java | 6 + DrawningMotorShip.java | 5 + FormMapWithSetShip.form | 156 ++++++++++++++++++++++++ FormMapWithSetShip.java | 238 +++++++++++++++++++++++++++++++++++++ FormShip.form | 13 +- FormShip.java | 21 +++- FormShipMixer.form | 70 +++++++++++ FormShipMixer.java | 118 ++++++++++++++++++ Main.java | 4 +- MapWithSetShipGeneric.java | 122 +++++++++++++++++++ SetShipGeneric.java | 54 +++++++++ ShipsMix.java | 56 +++++++++ 13 files changed, 853 insertions(+), 12 deletions(-) create mode 100644 FormMapWithSetShip.form create mode 100644 FormMapWithSetShip.java create mode 100644 FormShipMixer.form create mode 100644 FormShipMixer.java create mode 100644 MapWithSetShipGeneric.java create mode 100644 SetShipGeneric.java create mode 100644 ShipsMix.java diff --git a/AbstractMap.java b/AbstractMap.java index c5ccc9c..25b710a 100644 --- a/AbstractMap.java +++ b/AbstractMap.java @@ -84,7 +84,7 @@ public abstract class AbstractMap { } return true; } - private BufferedImage DrawMapWithObject() + public BufferedImage DrawMapWithObject() { BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); if (_drawningObject == null || _map == null) diff --git a/DrawingShip.java b/DrawingShip.java index 8e3766c..f2c6f37 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -34,6 +34,12 @@ public class DrawingShip extends JPanel { idd.SetDeckCount(random.nextInt(1, 4)); } + public DrawingShip(EntityWarmlyShip ship, IDrawningDeck deck) + { + warmlyShip = ship; + idd = deck; + } + protected DrawingShip(int speed, float weight, Color bodyColor, int warmlyWidth, int warmlyHeight) { this(speed, weight, bodyColor); diff --git a/DrawningMotorShip.java b/DrawningMotorShip.java index f77ed25..c91088a 100644 --- a/DrawningMotorShip.java +++ b/DrawningMotorShip.java @@ -7,6 +7,11 @@ public class DrawningMotorShip extends DrawingShip { warmlyShip = new EntityMotorShip(speed, weight, bodyColor, dopColor, tubes , cistern); } + public DrawningMotorShip(EntityWarmlyShip ship, IDrawningDeck deck){ + super(ship, deck); + warmlyShip = ship; + } + @Override public void DrawTransport(Graphics g) { diff --git a/FormMapWithSetShip.form b/FormMapWithSetShip.form new file mode 100644 index 0000000..d326f3f --- /dev/null +++ b/FormMapWithSetShip.form @@ -0,0 +1,156 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormMapWithSetShip.java b/FormMapWithSetShip.java new file mode 100644 index 0000000..59b26f8 --- /dev/null +++ b/FormMapWithSetShip.java @@ -0,0 +1,238 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class FormMapWithSetShip extends JFrame { + + private MapWithSetShipGeneric _mapShipCollectionGeneric; + public boolean ShipOnMap = false; + + public JPanel Mainpanel; + private JButton buttonAddShip; + private JComboBox comboBoxSelectorMap; + private JTextField maskedTextBoxPosition; + private JButton buttonRemoveShip; + private JButton buttonShowStorage; + private JButton buttonShowOnMap; + private JButton buttonUp; + private JButton buttonLeft; + private JButton buttonRight; + private JButton buttonDown; + private JPanel GraphicsOutput; + private JPanel groupBox; + + private JFrame getFrame() { + JFrame frame = new JFrame(); + frame.setVisible(false); + frame.setBounds(300, 100, 800, 600); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + return frame; + } + + JFrame jFrame = getFrame(); + + private void ButtonMove_Click(String name) + { + if (_mapShipCollectionGeneric == null || !ShipOnMap) return; + Direction direction = Direction.None; + switch (name) + { + case "buttonLeft": + direction = Direction.Left; + break; + case "buttonUp": + direction = Direction.Up; + break; + case "buttonRight": + direction = Direction.Right; + break; + case "buttonDown": + direction = Direction.Down; + break; + } + GraphicsOutput.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(GraphicsOutput.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.MoveObject(direction))); + GraphicsOutput.add(imageOfShip,BorderLayout.CENTER); + GraphicsOutput.revalidate(); + GraphicsOutput.repaint(); + } + + public FormMapWithSetShip() { + comboBoxSelectorMap.addItem("Простая карта"); + comboBoxSelectorMap.addItem("Вторая карта"); + comboBoxSelectorMap.addItem("Последняя карта"); + + 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); + } + + comboBoxSelectorMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + AbstractMap map = null; + switch (comboBoxSelectorMap.getSelectedItem().toString()) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Вторая карта": + map = new SecondMap(); + break; + case "Последняя карта": + map = new LastMap(); + break; + } + if (map != null) + { + _mapShipCollectionGeneric = new MapWithSetShipGeneric(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(), map); + } + else + { + _mapShipCollectionGeneric = null; + } + } + }); + buttonAddShip.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_mapShipCollectionGeneric == null) + { + 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 (_mapShipCollectionGeneric.Add(ship) > -1) + { + JOptionPane.showMessageDialog(jFrame, "Объект добавлен"); + GraphicsOutput.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(GraphicsOutput.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowSet())); + GraphicsOutput.add(imageOfShip,BorderLayout.CENTER); + GraphicsOutput.revalidate(); + GraphicsOutput.repaint(); + } + else + { + JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект"); + } + } + }); + buttonRemoveShip.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_mapShipCollectionGeneric == null) return; + + String text = maskedTextBoxPosition.getText(); + if(text.isEmpty()) return; + + if(JOptionPane.showConfirmDialog(jFrame, "Вы действительно хотите удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return; + + try { + Integer.parseInt(text); + } + catch (Exception ex){ + return; + } + + int pos = Integer.parseInt(text); + if (_mapShipCollectionGeneric.Delete(pos) != null) + { + JOptionPane.showMessageDialog(jFrame, "Объект удален"); + GraphicsOutput.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(GraphicsOutput.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowSet())); + GraphicsOutput.add(imageOfShip,BorderLayout.CENTER); + GraphicsOutput.revalidate(); + GraphicsOutput.repaint(); + } + else + { + JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект"); + } + } + }); + buttonShowStorage.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_mapShipCollectionGeneric == null) return; + + GraphicsOutput.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(GraphicsOutput.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowSet())); + GraphicsOutput.add(imageOfShip,BorderLayout.CENTER); + GraphicsOutput.revalidate(); + GraphicsOutput.repaint(); + ShipOnMap = false; + } + }); + buttonShowOnMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_mapShipCollectionGeneric == null) return; + + GraphicsOutput.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(GraphicsOutput.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowOnMap())); + GraphicsOutput.add(imageOfShip,BorderLayout.CENTER); + GraphicsOutput.revalidate(); + GraphicsOutput.repaint(); + ShipOnMap = true; + } + }); + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonUp"); + } + }); + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonLeft"); + } + }); + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonDown"); + } + }); + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonRight"); + } + }); + } +} diff --git a/FormShip.form b/FormShip.form index d356abf..c377de0 100644 --- a/FormShip.form +++ b/FormShip.form @@ -102,16 +102,19 @@ - - - - - + + + + + + + + diff --git a/FormShip.java b/FormShip.java index 707cf9c..8b9d20c 100644 --- a/FormShip.java +++ b/FormShip.java @@ -5,10 +5,12 @@ import java.awt.event.*; import java.awt.image.*; import java.util.*; -public class FormShip extends JFrame{ +public class FormShip extends JDialog{ private JToolBar statusStrip; public JPanel Mainpanel; private DrawingShip ship; + private DrawingShip selectedShip; + public DrawingShip GetSelectedShip(){return selectedShip;} //Выбранный объект private JButton buttonRight; private JButton buttonCreate; private JButton buttonLeft; @@ -16,6 +18,7 @@ public class FormShip extends JFrame{ private JButton buttonDown; private JPanel GraphicsOutput; private JButton buttonCreateModif; + private JButton buttonSelectShip; private JLabel JLabelSpeed = new JLabel(); private JLabel JLabelWeight = new JLabel(); private JLabel JLabelColor = new JLabel(); @@ -76,6 +79,8 @@ public class FormShip extends JFrame{ LabelBox.add(JLabelColor); statusStrip.add(LabelBox); + add(Mainpanel); + try { Image img = ImageIO.read(FormShip.class.getResource("/Images/totop.png")); buttonUp.setIcon(new ImageIcon(img)); @@ -93,7 +98,7 @@ public class FormShip extends JFrame{ @Override public void actionPerformed(ActionEvent e) { Random random = new Random(); - ship = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); + ship = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), JColorChooser.showDialog(null, "Цвет", null)); SetData(); Draw(); } @@ -137,12 +142,20 @@ public class FormShip extends JFrame{ public void actionPerformed(ActionEvent e) { Random random = new Random(); ship = new DrawningMotorShip(random.nextInt(100, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + JColorChooser.showDialog(null, "Цвет", null), + JColorChooser.showDialog(null, "Цвет", null), random.nextBoolean(), random.nextBoolean()); SetData(); Draw(); } }); + buttonSelectShip.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + selectedShip = ship; + setVisible(false); + dispose(); + } + }); } } diff --git a/FormShipMixer.form b/FormShipMixer.form new file mode 100644 index 0000000..9419a2e --- /dev/null +++ b/FormShipMixer.form @@ -0,0 +1,70 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormShipMixer.java b/FormShipMixer.java new file mode 100644 index 0000000..29b7edd --- /dev/null +++ b/FormShipMixer.java @@ -0,0 +1,118 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class FormShipMixer extends JFrame{ + public JPanel Mainpanel; + private JPanel pictureBox; + private JButton ButtonCreate; + private JButton ButtonCreateModif; + private JToolBar StatusStrip; + private JLabel LabelInfo; + private JButton ButtonMix; + private JLabel JLabelSpeed = new JLabel(); + private JLabel JLabelWeight = new JLabel(); + private JLabel JLabelColor = new JLabel(); + private ShipsMix _drawningEntities; + + private IDrawningDeck SetData() + { + Random random = new Random(); + switch (random.nextInt(3)) + { + case 0: + return new DrawDeck(); + case 1: + return new DrawGuns(); + } + return new DrawOvalDeck(); + } + + private void Draw(DrawingShip _plane) { + pictureBox.removeAll(); + Random random = new Random(); + BufferedImage bmp = new BufferedImage(pictureBox.getWidth(), pictureBox.getHeight(),BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + gr.setColor(new Color(238, 238, 238)); + gr.fillRect(0, 0, pictureBox.getWidth(), pictureBox.getHeight()); + if (_plane != null) { + _plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), + pictureBox.getWidth(), pictureBox.getHeight()); + _plane.DrawTransport(gr); + JLabelSpeed.setText("Cкорость: " + _plane.GetWarmlyShip().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + _plane.GetWarmlyShip().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + _plane.GetWarmlyShip().GetBodyColor() + " ")); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(pictureBox.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(bmp)); + pictureBox.add(imageOfShip,BorderLayout.CENTER); + } + validate(); + } + public FormShipMixer() + { + Box LabelBox = Box.createHorizontalBox(); + LabelBox.setMinimumSize(new Dimension(1, 20)); + LabelBox.add(JLabelSpeed); + LabelBox.add(JLabelWeight); + LabelBox.add(JLabelColor); + StatusStrip.add(LabelBox); + _drawningEntities = new ShipsMix<>(10,10); + + ButtonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e){ + Random random = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); + EntityWarmlyShip ship = new EntityWarmlyShip(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst); + IDrawningDeck deck = SetData(); + int DecksCount=random.nextInt(1,4); + deck.SetDeckCount(DecksCount); + if(_drawningEntities.Insert(ship) != -1 && _drawningEntities.Insert(deck) != -1) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + Draw(_drawningEntities.MakeShipMix()); + LabelInfo.setText(_drawningEntities.indexX + " " + _drawningEntities.indexY); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + }); + ButtonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); + Color colorSecond = JColorChooser.showDialog(null, "Цвет", null); + EntityMotorShip _ship = new EntityMotorShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean()); + IDrawningDeck deck = SetData(); + int DecksCount=random.nextInt(1,4); + deck.SetDeckCount(DecksCount); + if(_drawningEntities.Insert(_ship) != -1 && _drawningEntities.Insert(deck) != -1) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + Draw(_drawningEntities.MakeShipMix()); + LabelInfo.setText(_drawningEntities.indexX + " " + _drawningEntities.indexY); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + }); + ButtonMix.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_drawningEntities.shipsCount == 0 || _drawningEntities.deckCount == 0) return; + Draw(_drawningEntities.MakeShipMix()); + LabelInfo.setText(_drawningEntities.indexX + " " + _drawningEntities.indexY); + } + }); + } +} diff --git a/Main.java b/Main.java index 5a80f25..bb8b139 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 №2"); - frame.setContentPane(new FormMap().Mainpanel); + JFrame frame = new JFrame("Hard №3"); + frame.setContentPane(new FormShipMixer().Mainpanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(500, 200); frame.pack(); diff --git a/MapWithSetShipGeneric.java b/MapWithSetShipGeneric.java new file mode 100644 index 0000000..8991399 --- /dev/null +++ b/MapWithSetShipGeneric.java @@ -0,0 +1,122 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetShipGeneric { + private int _pictureWidth; + private int _pictureHeight; + private int _placeSizeWidth = 210; + private int _placeSizeHeight = 100; + private SetShipGeneric _setShips; + private U _map; + + public MapWithSetShipGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setShips = new SetShipGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int Add(T ship) + { + return _setShips.Insert(ship); + } + public T Delete(int position) + { + return _setShips.Remove(position); + } + + public BufferedImage ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + Graphics g = bmp.getGraphics(); + DrawBackground((Graphics2D) g); + DrawShips(g); + return bmp; + } + + public BufferedImage ShowOnMap() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + + Shaking(); + for (int i = 0; i < _setShips.getCount(); i++) + { + var ship = _setShips.Get(i); + if (ship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); + } + } + return bmp; + } + + public BufferedImage MoveObject(Direction direction) + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + + if (_map != null) + { + return _map.MoveObject(direction); + } + return bmp; + } + + private void Shaking() + { + int j = _setShips.getCount() - 1; + for (int i = 0; i < _setShips.getCount(); ++i) + { + if (_setShips.Get(i) == null) + { + for (; j > i; --j) + { + var car = _setShips.Get(j); + if (car != null) + { + _setShips.Insert(car, i); + _setShips.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics2D g2d) + { + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, _pictureWidth, _pictureHeight); + g2d.setColor(new Color(150, 75, 0)); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i) + { + for (int j = 1; j < _pictureHeight / _placeSizeHeight + 1; ++j) //линия рамзетки места + { + g2d.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + for (int k = 0; k < _placeSizeWidth / (30 * 2); ++k) + g2d.drawLine(i * _placeSizeWidth + (k + 1) * 30, j * _placeSizeHeight, i * _placeSizeWidth + (k + 1) * 30, j * _placeSizeHeight + 30); + } + g2d.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + + private void DrawShips(Graphics g) + { + for (int i = 0; i < _setShips.getCount(); ++i) + { + if (_setShips.Get(i) != null ) + { + int temp = 0; + if (_setShips.Get(i).GetCurrentPosition()[3] - _setShips.Get(i).GetCurrentPosition()[2] < 75) temp = (int)(_setShips.Get(i).GetCurrentPosition()[3] - _setShips.Get(i).GetCurrentPosition()[2]); + _setShips.Get(i).SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight); + _setShips.Get(i).DrawningObject(g); + } + } + } +} diff --git a/SetShipGeneric.java b/SetShipGeneric.java new file mode 100644 index 0000000..ec5347c --- /dev/null +++ b/SetShipGeneric.java @@ -0,0 +1,54 @@ +public class SetShipGeneric { + private T[] _places; + public int getCount() { + return _places.length; + } + + 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; + } + + public int Insert(T ship) + { + return Insert(ship, 0); + } + + 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; + return position; + } + + public T Remove(int position) + { + T DelElement = _places[position]; + _places[position] = null; + return DelElement; + } + + public T Get(int position) + { + if (position < 0 || position > _places.length) return null; + return _places[position]; + } +} diff --git a/ShipsMix.java b/ShipsMix.java new file mode 100644 index 0000000..5c9f6b3 --- /dev/null +++ b/ShipsMix.java @@ -0,0 +1,56 @@ +import java.util.Random; + +public class ShipsMix{ + public T[] _ships; + public U[] _decks; + int shipsCount = 0; + int deckCount = 0; + String indexX; + String indexY; + + public ShipsMix(int countE,int countI){ + _ships = (T[]) new EntityWarmlyShip[countE]; + _decks = (U[]) new IDrawningDeck[countI]; + } + + public int Insert(T ship){ + if(shipsCount < _ships.length){ + _ships[shipsCount] = ship; + ++shipsCount; + return shipsCount - 1; + } + return -1; + } + + public int Insert(U deck){ + if(deckCount < _decks.length){ + _decks[deckCount] = deck; + ++deckCount; + return deckCount - 1; + } + return -1; + } + + public void SetIndexs(int ind1, int ind2) + { + indexX = Integer.toString(ind1); + indexY = Integer.toString(ind2); + } + + public DrawingShip MakeShipMix(){ + Random random = new Random(); + int indEnt = 0; + int indIllum = 0; + if(shipsCount - 1 != 0 && deckCount - 1 != 0){ + indEnt = random.nextInt(0,shipsCount - 1); + indIllum = random.nextInt(0, deckCount - 1); + } + T plane = (T)_ships[indEnt]; + U illum = (U)_decks[indIllum]; + SetIndexs(indEnt,indIllum); + if(plane instanceof EntityMotorShip){ + return new DrawningMotorShip(plane,illum); + } + return new DrawingShip(plane,illum); + } +} -- 2.25.1