From 2accdec263a5f0bdb1b8e7f2e50eb50f67615ac8 Mon Sep 17 00:00:00 2001 From: the Date: Tue, 6 Dec 2022 11:02:26 +0400 Subject: [PATCH] Start --- DrawingMotorShip.java | 4 +++ DrawingShip.java | 5 ++++ EntityWithDecks.java | 43 ++++++++++++++++++++++++++++ FormShip.form | 13 +++++++++ FormShip.java | 47 +++++++++++++++++------------- FormShipDisplay.form | 30 ++++++++++++++++++++ FormShipDisplay.java | 66 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 188 insertions(+), 20 deletions(-) create mode 100644 EntityWithDecks.java create mode 100644 FormShipDisplay.form create mode 100644 FormShipDisplay.java diff --git a/DrawingMotorShip.java b/DrawingMotorShip.java index 3a9b201..869d257 100644 --- a/DrawingMotorShip.java +++ b/DrawingMotorShip.java @@ -6,6 +6,10 @@ public class DrawingMotorShip extends DrawingShip { ship = new EntityMotorShip(speed, weight, bodyColor, dopColor, pipes, fueltank); } + public DrawingMotorShip(EntityMotorShip entity, IDrawingDecks decks) { + super(entity, decks); + } + @Override public void drawTransport(Graphics2D g) { if (!(ship instanceof EntityMotorShip motorShip)) { diff --git a/DrawingShip.java b/DrawingShip.java index 884f5d8..059daf9 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -19,6 +19,11 @@ public class DrawingShip { drawingDecks = DecksType.random(decksCount, bodyColor); } + public DrawingShip(EntityShip entity, IDrawingDecks decks) { + ship = entity; + drawingDecks = decks; + } + protected DrawingShip(int speed, float weight, Color bodyColor, int decksCount, int shipWidth, int shipHeight) { this(speed, weight, bodyColor, decksCount); _shipWidth = shipWidth; diff --git a/EntityWithDecks.java b/EntityWithDecks.java new file mode 100644 index 0000000..edb041f --- /dev/null +++ b/EntityWithDecks.java @@ -0,0 +1,43 @@ +import java.util.Random; + +public class EntityWithDecks { + static Random rnd = new Random(); + private final Object[] entities; + public int entitiesCount = 0; + private final Object[] decks; + public int decksCount = 0; + + public EntityWithDecks(int count) { + entities = new Object[count]; + decks = new Object[count]; + } + + public boolean add(EntityShip entity) { + if (entitiesCount >= entities.length) { + return false; + } + entities[entitiesCount++] = entity; + return true; + } + + public boolean add(IDrawingDecks deck) { + if (decksCount >= decks.length) { + return false; + } + decks[decksCount++] = deck; + return true; + } + + public IDrawingObject constructShip() { + if (entitiesCount == 0 || decksCount == 0) { + return null; + } + EntityShip entity = (EntityShip) entities[rnd.nextInt(0, entitiesCount)]; + IDrawingDecks deck = (IDrawingDecks) decks[rnd.nextInt(0, decksCount)]; + + if (entity instanceof EntityMotorShip advancedEntity) { + return new DrawingObjectShip(new DrawingMotorShip(advancedEntity, deck)); + } + return new DrawingObjectShip(new DrawingShip(entity, deck)); + } +} diff --git a/FormShip.form b/FormShip.form index 675ccc4..d26b349 100644 --- a/FormShip.form +++ b/FormShip.form @@ -148,6 +148,19 @@ + + + + + + + + + + + + + diff --git a/FormShip.java b/FormShip.java index 9c340f9..065e9b6 100644 --- a/FormShip.java +++ b/FormShip.java @@ -1,10 +1,9 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; +import java.awt.event.*; import java.util.Random; -public class FormShip extends JFrame { +public class FormShip extends JDialog { private JPanel shipPane; private JLabel speedLabel; private JLabel weightLabel; @@ -16,23 +15,21 @@ public class FormShip extends JFrame { private JButton buttonLeft; private JButton buttonRight; private JButton createAdvancedButton; + public JButton buttonSelect; private DrawingShip _ship; + private DrawingShip selectedShip; public FormShip() { this.setTitle("Ship"); this.setContentPane(shipPane); createButton.addActionListener(e -> { Random rnd = new Random(); - _ship = new DrawingShip( - rnd.nextInt(100, 300), - rnd.nextInt(1000, 2000), - new Color( - rnd.nextInt(0, 256), - rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), - rnd.nextInt(1, 4) - ); + Color color = JColorChooser.showDialog(this, "Цвет", new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); + if (color == null) { + color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + } + _ship = new DrawingShip(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color, rnd.nextInt(1, 4)); _ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); speedLabel.setText(String.format("Speed: %s", _ship.getShip().getSpeed())); weightLabel.setText(String.format("Weight: %s", _ship.getShip().getWeight())); @@ -64,18 +61,20 @@ public class FormShip extends JFrame { }); createAdvancedButton.addActionListener(e -> { Random rnd = new Random(); + Color color = JColorChooser.showDialog(this, "Основной цвет", Color.white); + if (color == null) { + color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + } + Color dopColor = JColorChooser.showDialog(this, "Дополнительный цвет", Color.white); + if (dopColor == null) { + dopColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + } _ship = new DrawingMotorShip( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - new Color( - rnd.nextInt(0, 256), - rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), + color, rnd.nextInt(1, 4), - new Color( - rnd.nextInt(0, 256), - rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), + dopColor, rnd.nextBoolean(), rnd.nextBoolean() ); @@ -85,6 +84,10 @@ public class FormShip extends JFrame { colorLabel.setText(String.format("Цвет: %x", _ship.getShip().getBodyColor().getRGB())); repaint(); }); + buttonSelect.addActionListener(e -> { + selectedShip = _ship; + dispose(); + }); } @Override @@ -95,4 +98,8 @@ public class FormShip extends JFrame { _ship.drawTransport(g2d); } } + + public DrawingShip getSelectedShip() { + return selectedShip; + } } diff --git a/FormShipDisplay.form b/FormShipDisplay.form new file mode 100644 index 0000000..f468515 --- /dev/null +++ b/FormShipDisplay.form @@ -0,0 +1,30 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/FormShipDisplay.java b/FormShipDisplay.java new file mode 100644 index 0000000..9a733a5 --- /dev/null +++ b/FormShipDisplay.java @@ -0,0 +1,66 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormShipDisplay extends JFrame { + private static final Random rnd = new Random(); + private JPanel contentPane; + private JButton buttonRefresh; + private JPanel pictureBox; + private IDrawingObject first; + private IDrawingObject second; + private IDrawingObject third; + + private final EntityWithDecks storage; + + public FormShipDisplay() { + setTitle("Корабли"); + setContentPane(contentPane); + + storage = new EntityWithDecks<>(20); + + for(int i = 0; i < 20; i++) { + if (rnd.nextBoolean()) { + storage.add(new EntityMotorShip( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextBoolean(), + rnd.nextBoolean() + )); + } else { + storage.add(new EntityShip( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)) + )); + } + storage.add(DecksType.random(rnd.nextInt(0, 3), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)))); + } + + buttonRefresh.addActionListener(e -> { + first = storage.constructShip(); + second = storage.constructShip(); + third = storage.constructShip(); + + first.setObject(0, 10, pictureBox.getWidth(), pictureBox.getHeight()); + second.setObject(90, 10, pictureBox.getWidth(), pictureBox.getHeight()); + third.setObject(190, 10, pictureBox.getWidth(), pictureBox.getHeight()); + + repaint(); + }); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + Graphics2D g2g = (Graphics2D) pictureBox.getGraphics(); + + if (first != null && second != null && third != null) { + first.drawingObject(g2g); + second.drawingObject(g2g); + third.drawingObject(g2g); + } + } +} \ No newline at end of file