This commit is contained in:
the 2022-12-06 11:02:26 +04:00
parent 41866d9c5f
commit 2accdec263
7 changed files with 188 additions and 20 deletions

View File

@ -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)) {

View File

@ -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;

43
EntityWithDecks.java Normal file
View File

@ -0,0 +1,43 @@
import java.util.Random;
public class EntityWithDecks<T extends EntityShip, U extends IDrawingDecks> {
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));
}
}

View File

@ -148,6 +148,19 @@
<text value="Модификация"/>
</properties>
</component>
<component id="d034f" class="javax.swing.JButton" binding="buttonSelect">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Выбрать"/>
</properties>
</component>
<hspacer id="f89d0">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="f89d0">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>

View File

@ -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;
}
}

30
FormShipDisplay.form Normal file
View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormShipDisplay">
<grid id="27dc6" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="323" height="234"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="632d3" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="7e5f8" class="javax.swing.JButton" binding="buttonRefresh">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Обновить"/>
</properties>
</component>
</children>
</grid>
</form>

66
FormShipDisplay.java Normal file
View File

@ -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<EntityShip, IDrawingDecks> 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);
}
}
}