Work is done 3
This commit is contained in:
parent
cd2b9474d4
commit
385eedd0b9
@ -6,6 +6,10 @@ public class DrawingAdvancedArtillery extends DrawingArtillery {
|
||||
artillery = new EntityAdvancedArtillery(speed, weight, bodyColor, dopColor, weapon, salvoBattery);
|
||||
}
|
||||
|
||||
public DrawingAdvancedArtillery(EntityAdvancedArtillery entity, IDrawingRollers rollers) {
|
||||
super(entity, rollers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawTransport(Graphics2D g) {
|
||||
if (!(artillery instanceof EntityAdvancedArtillery advancedArtillery)) {
|
||||
|
@ -25,6 +25,11 @@ public class DrawingArtillery {
|
||||
_artilleryHeight = artilleryHeight;
|
||||
}
|
||||
|
||||
public DrawingArtillery(EntityArtillery entity, IDrawingRollers rollers) {
|
||||
artillery = entity;
|
||||
drawingRollers = rollers;
|
||||
}
|
||||
|
||||
public void setPosition(int x, int y, int width, int height) {
|
||||
if (x < 0 || x + _artilleryWidth >= width)
|
||||
{
|
||||
|
43
EntityWithRollers.java
Normal file
43
EntityWithRollers.java
Normal file
@ -0,0 +1,43 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityWithRollers<T extends EntityArtillery, U extends IDrawingRollers> {
|
||||
static Random rnd = new Random();
|
||||
private final Object[] entities;
|
||||
public int entitiesCount = 0;
|
||||
private final Object[] rollers;
|
||||
public int rollersCount = 0;
|
||||
|
||||
public EntityWithRollers(int count) {
|
||||
entities = new Object[count];
|
||||
rollers = new Object[count];
|
||||
}
|
||||
|
||||
public boolean add(EntityArtillery entity) {
|
||||
if (entitiesCount >= entities.length) {
|
||||
return false;
|
||||
}
|
||||
entities[entitiesCount++] = entity;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean add(IDrawingRollers roller) {
|
||||
if (rollersCount >= rollers.length) {
|
||||
return false;
|
||||
}
|
||||
rollers[rollersCount++] = roller;
|
||||
return true;
|
||||
}
|
||||
|
||||
public IDrawingObject constructArtillery() {
|
||||
if (entitiesCount == 0 || rollersCount == 0) {
|
||||
return null;
|
||||
}
|
||||
EntityArtillery entity = (EntityArtillery) entities[rnd.nextInt(0, entitiesCount)];
|
||||
IDrawingRollers roller = (IDrawingRollers) rollers[rnd.nextInt(0, rollersCount)];
|
||||
|
||||
if (entity instanceof EntityAdvancedArtillery advancedEntity) {
|
||||
return new DrawingObjectArtillery(new DrawingAdvancedArtillery(advancedEntity, roller));
|
||||
}
|
||||
return new DrawingObjectArtillery(new DrawingArtillery(entity, roller));
|
||||
}
|
||||
}
|
30
FormGallery.form
Normal file
30
FormGallery.form
Normal 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="FormGallery">
|
||||
<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
FormGallery.java
Normal file
66
FormGallery.java
Normal file
@ -0,0 +1,66 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormGallery 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 EntityWithRollers<EntityArtillery, IDrawingRollers> storage;
|
||||
|
||||
public FormGallery() {
|
||||
setTitle("Галлерея");
|
||||
setContentPane(contentPane);
|
||||
|
||||
storage = new EntityWithRollers<>(20);
|
||||
|
||||
for(int i = 0; i < 20; i++) {
|
||||
if (rnd.nextBoolean()) {
|
||||
storage.add(new EntityAdvancedArtillery(
|
||||
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 EntityArtillery(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))
|
||||
));
|
||||
}
|
||||
storage.add(RollersType.random(rnd.nextInt(4, 7), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))));
|
||||
}
|
||||
|
||||
buttonRefresh.addActionListener(e -> {
|
||||
first = storage.constructArtillery();
|
||||
second = storage.constructArtillery();
|
||||
third = storage.constructArtillery();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
158
FormMap.form
158
FormMap.form
@ -1,158 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMap">
|
||||
<grid id="27dc6" binding="artilleryPane" 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="630" height="420"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<minimumSize width="80" height="50"/>
|
||||
<preferredSize width="640" height="480"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="57516" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="dcb7f" class="javax.swing.JLabel" binding="speedLabel">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="998a6" class="javax.swing.JLabel" binding="weightLabel">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="cffd1" class="javax.swing.JLabel" binding="colorLabel">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="52641" binding="pictureBox" layout-manager="GridLayoutManager" row-count="4" column-count="6" 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>
|
||||
<component id="6937b" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<borderPainted value="true"/>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowUp.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="48a84">
|
||||
<constraints>
|
||||
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<vspacer id="f9d1a">
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="e761a" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowDown.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="b99d6" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowLeft.png"/>
|
||||
<label value=""/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="7f134" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="3" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowRight.png"/>
|
||||
<label value=""/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="fc5b2" class="javax.swing.JButton" binding="createButton">
|
||||
<constraints>
|
||||
<grid row="3" 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>
|
||||
<alignmentX value="0.0"/>
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<text value="Создать"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="65e99" class="javax.swing.JButton" binding="createAdvancedButton">
|
||||
<constraints>
|
||||
<grid row="3" column="1" 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>
|
||||
<component id="80160" class="javax.swing.JComboBox" binding="comboBoxSelectorMap">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<model>
|
||||
<item value="Простая карта"/>
|
||||
<item value="Лесная карта"/>
|
||||
</model>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
110
FormMap.java
110
FormMap.java
@ -1,110 +0,0 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.awt.event.ItemListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormMap extends JFrame {
|
||||
private JPanel artilleryPane;
|
||||
private JLabel speedLabel;
|
||||
private JLabel weightLabel;
|
||||
private JLabel colorLabel;
|
||||
private JPanel pictureBox;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonRight;
|
||||
private JButton createButton;
|
||||
private JButton createAdvancedButton;
|
||||
private JComboBox comboBoxSelectorMap;
|
||||
|
||||
private AbstractMap _abstractMap;
|
||||
private Image bufferedImage;
|
||||
|
||||
public FormMap() {
|
||||
this.setTitle("Artillery");
|
||||
this.setContentPane(artilleryPane);
|
||||
|
||||
_abstractMap = new SimpleMap();
|
||||
|
||||
createButton.addActionListener(e -> {
|
||||
Random rnd = new Random();
|
||||
var artillery = new DrawingArtillery(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||
rnd.nextInt(4, 6 + 1)
|
||||
);
|
||||
setData(artillery);
|
||||
});
|
||||
buttonLeft.setForeground(new Color(0, 0, 0, 0));
|
||||
buttonRight.setForeground(new Color(0, 0, 0, 0));
|
||||
buttonUp.setForeground(new Color(0, 0, 0, 0));
|
||||
buttonDown.setForeground(new Color(0, 0, 0, 0));
|
||||
buttonLeft.addActionListener(e -> {
|
||||
if (_abstractMap != null) {
|
||||
bufferedImage = _abstractMap.moveObject(Direction.Left);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonRight.addActionListener(e -> {
|
||||
if (_abstractMap != null) {
|
||||
bufferedImage = _abstractMap.moveObject(Direction.Right);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonUp.addActionListener(e -> {
|
||||
if (_abstractMap != null) {
|
||||
bufferedImage = _abstractMap.moveObject(Direction.Up);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonDown.addActionListener(e -> {
|
||||
if (_abstractMap != null) {
|
||||
bufferedImage = _abstractMap.moveObject(Direction.Down);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
createAdvancedButton.addActionListener(e -> {
|
||||
Random rnd = new Random();
|
||||
var artillery = new DrawingAdvancedArtillery(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||
rnd.nextInt(4, 6 + 1),
|
||||
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||
rnd.nextBoolean(),
|
||||
rnd.nextBoolean()
|
||||
);
|
||||
setData(artillery);
|
||||
});
|
||||
comboBoxSelectorMap.addItemListener(e -> {
|
||||
if (e.getStateChange() == ItemEvent.SELECTED) {
|
||||
switch (e.getItem().toString()) {
|
||||
case "Простая карта" -> _abstractMap = new SimpleMap();
|
||||
case "Лесная карта" -> _abstractMap = new ForestMap();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setData(DrawingArtillery artillery) {
|
||||
Random rnd = new Random();
|
||||
artillery.setPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight());
|
||||
speedLabel.setText(String.format("Скорость: %d", artillery.artillery.getSpeed()));
|
||||
weightLabel.setText(String.format("Вес: %f", artillery.artillery.getWeight()));
|
||||
colorLabel.setText(String.format("Цвет: %x", artillery.getArtillery().getBodyColor().getRGB()));
|
||||
bufferedImage = _abstractMap.createMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawingObjectArtillery(artillery));
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
|
||||
if (bufferedImage != null) {
|
||||
pictureBox.paintComponents(bufferedImage.getGraphics());
|
||||
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,8 +2,8 @@ import javax.swing.*;
|
||||
|
||||
public class Program {
|
||||
public static void main(String[] args) {
|
||||
FormMapWithSetArtilleries form = new FormMapWithSetArtilleries();
|
||||
form.setSize(640, 480);
|
||||
FormGallery form = new FormGallery();
|
||||
form.setSize(320, 240);
|
||||
form.setVisible(true);
|
||||
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user