This commit is contained in:
Леонид Малафеев 2023-11-24 16:43:36 +04:00
parent 008dde1b0f
commit 984fbdc7da
27 changed files with 848 additions and 200 deletions

124
.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View File

@ -1,22 +0,0 @@
public enum Block {
One(1),
Two(2),
Three(3);
Block(int i) {
}
public static Block GetBlock(int i){
switch (i)
{
case 1:
return One;
case 2:
return Two;
case 3:
return Three;
}
return null;
}
}

View File

@ -0,0 +1,8 @@
package CruiserHard.Complication;
public enum Block {
One(1),
Two(2),
Three(3);
Block(int i) {}
}

View File

@ -1,28 +1,40 @@
package CruiserHard.Complication;
import java.awt.*;
public class DrawBlock {
public class DrawBlock implements IDrawBlock{
private Block blockCount;
public void SetBlockCount(int count){
blockCount = Block.GetBlock(count);
@Override
public void SetBlockCount(int num) {
switch (num){
case 1:
blockCount = Block.One;
break;
case 2:
blockCount = Block.Two;
break;
default:
blockCount = Block.Three;
}
}
public void DrawningBlock(int _startPosX, int _startPosY, int _cruiserWidth, Graphics2D g2d, Color bodyColor){
@Override
public Block GetBlockCount() {
return blockCount;
}
public void DrawBlocks(int _startPosX, int _startPosY,Graphics g2d){
switch (blockCount)
{
case One:
break;
case Two:
DrawFirstDop(_startPosX,_startPosY,_cruiserWidth,g2d,bodyColor);
DrawFirstDop(_startPosX,_startPosY,g2d);
break;
case Three:
DrawSecondDop(_startPosX,_startPosY,_cruiserWidth,g2d,bodyColor);
DrawSecondDop(_startPosX,_startPosY,g2d);
break;
}
}
public void DrawFirstDop(int _startPosX, int _startPosY, int _cruiserWidth, Graphics2D g2d, Color bodyColor) {
public void DrawFirstDop(int _startPosX, int _startPosY, Graphics g2d) {
Polygon elementsSec = new Polygon();
elementsSec.addPoint(_startPosX + 55,_startPosY + 25);
elementsSec.addPoint(_startPosX + 65,_startPosY + 25);
@ -31,8 +43,8 @@ public class DrawBlock {
g2d.setColor(Color.WHITE);
g2d.fillPolygon(elementsSec);
}
public void DrawSecondDop(int _startPosX, int _startPosY, int _cruiserWidth, Graphics2D g2d, Color bodyColor) {
DrawFirstDop(_startPosX,_startPosY,_cruiserWidth,g2d,bodyColor);
public void DrawSecondDop(int _startPosX, int _startPosY, Graphics g2d) {
DrawFirstDop(_startPosX,_startPosY,g2d);
Polygon elementsThree = new Polygon();
elementsThree.addPoint(_startPosX + 75,_startPosY + 15);
elementsThree.addPoint(_startPosX + 85,_startPosY + 15);

View File

@ -0,0 +1,50 @@
package CruiserHard.Complication;
import java.awt.*;
public class DrawBlockOval implements IDrawBlock{
private Block blockCount;
@Override
public void SetBlockCount(int num) {
switch (num){
case 1:
blockCount = Block.One;
break;
case 2:
blockCount = Block.Two;
break;
default:
blockCount = Block.Three;
}
}
@Override
public Block GetBlockCount() {
return blockCount;
}
@Override
public void DrawBlocks(int _startPosX, int _startPosY,Graphics g2d) {
switch (blockCount)
{
case One:
break;
case Two:
DrawFirstDopOval(_startPosX,_startPosY,g2d);
break;
case Three:
DrawSecondDopOval(_startPosX,_startPosY,g2d);
break;
}
}
public void DrawFirstDopOval(int _startPosX, int _startPosY, Graphics g2d) {
g2d.setColor(Color.WHITE);
g2d.fillOval(_startPosX + 55,_startPosY + 25,10,10);
}
public void DrawSecondDopOval(int _startPosX, int _startPosY, Graphics g2d) {
DrawFirstDopOval(_startPosX,_startPosY,g2d);
g2d.setColor(Color.WHITE);
g2d.fillOval(_startPosX + 75,_startPosY + 15, 10, 30);
}
}

View File

@ -0,0 +1,61 @@
package CruiserHard.Complication;
import java.awt.*;
public class DrawBlockTriangle implements IDrawBlock {
private Block blockCount;
@Override
public void SetBlockCount(int num) {
switch (num) {
case 1:
blockCount = Block.One;
break;
case 2:
blockCount = Block.Two;
break;
default:
blockCount = Block.Three;
}
}
@Override
public Block GetBlockCount() {
return blockCount;
}
@Override
public void DrawBlocks(int _startPosX, int _startPosY, Graphics g2d) {
switch (blockCount) {
case One:
break;
case Two:
DrawFirstDopTriangle(_startPosX, _startPosY, g2d);
break;
case Three:
DrawSecondDopTriangle(_startPosX, _startPosY, g2d);
break;
}
}
public void DrawFirstDopTriangle(int _startPosX, int _startPosY, Graphics g2d) {
Polygon elementsSec = new Polygon();
elementsSec.addPoint(_startPosX + 55, _startPosY + 25);
elementsSec.addPoint(_startPosX + 65, _startPosY + 30);
elementsSec.addPoint(_startPosX + 55, _startPosY + 35);
g2d.setColor(Color.WHITE);
g2d.fillPolygon(elementsSec);
}
public void DrawSecondDopTriangle(int _startPosX, int _startPosY, Graphics g2d) {
DrawFirstDopTriangle(_startPosX, _startPosY, g2d);
Polygon elementsThree = new Polygon();
elementsThree.addPoint(_startPosX + 75, _startPosY + 15);
elementsThree.addPoint(_startPosX + 85, _startPosY + 30);
elementsThree.addPoint(_startPosX + 75, _startPosY + 45);
g2d.setColor(Color.WHITE);
g2d.fillPolygon(elementsThree);
}
}

View File

@ -0,0 +1,7 @@
package CruiserHard.Complication;
import java.awt.*;
public interface IDrawBlock {
void SetBlockCount(int number);
Block GetBlockCount();
void DrawBlocks(int _startPosX, int _startPosY,Graphics g2d);
}

View File

@ -1,3 +1,5 @@
package CruiserHard.Direction;
public enum Direction {
Up(1),
Down(2),

View File

@ -1,26 +1,41 @@
package CruiserHard.Drawing;
import CruiserHard.Direction.Direction;
import CruiserHard.Entity.EntityCruiser;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.*;
public class DrawingCruiser {
private EntityCruiser cruiser; //Класс-сущность
public EntityCruiser GetCruiser(){return cruiser;}
public class DrawingCruiser extends JPanel {
public EntityCruiser cruiser; //Класс-сущность
public int _startPosX; //Координаты отрисовки по оси x
public EntityCruiser GetCruiser(){return cruiser;}
public int GetStartX() {
return _startPosX;
}
public int _startPosY; //Координаты отрисовки по оси y
public int GetStartY() {
return _startPosY;
}
private Integer _pictureWidth = null; //Ширина окна
private Integer _pictureHeight = null; //Высота окна
private final int _cruiserWidth = 150; //Ширина отрисовки крейсера
public int GetWidth() {
return _cruiserWidth;
}
private final int _cruiserHeight = 60; //Высота отрисовки крейсера
private DrawBlock drawBlock = new DrawBlock();
public int GetHeight() {
return _cruiserHeight;
}
//Инициализация
public void Init(int speed, float weight, Color bodyColor, boolean rocketMines, boolean helipad)
{
cruiser = new EntityCruiser();
cruiser.Init(speed,weight,bodyColor, rocketMines, helipad);
Random random = new Random();
drawBlock.SetBlockCount(random.nextInt(1, 4));
public DrawingCruiser(int speed, float weight, Color bodyColor, int width, int height, int blockCount) {
cruiser = new EntityCruiser(speed,weight,bodyColor);
_pictureWidth = width;
_pictureHeight = height;
}
//Начальные коордитанты
@ -56,7 +71,7 @@ public class DrawingCruiser {
}
//Отрисовка транспорта
public void DrawTransport(Graphics g)
public void DrawTransport(Graphics gr)
{
if (GetCruiser() == null) return;
@ -64,7 +79,8 @@ public class DrawingCruiser {
{
return;
}
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(gr);
Graphics2D g2d = (Graphics2D) gr;
g2d.setColor(cruiser.GetBodyColor());
// палуба
Polygon paluba = new Polygon();
@ -87,22 +103,23 @@ public class DrawingCruiser {
g2d.setColor(Color.BLACK);
g2d.fillPolygon(elements);
g2d.fill(new Ellipse2D.Double(_startPosX + 100, _startPosY + 20, 20, 20));
// турбины
g2d.setColor(Color.BLACK);
g2d.fillRect(_startPosX, _startPosY + 10, 10, 20);
g2d.fillRect(_startPosX, _startPosY + 35, 10, 20);
// шахты
if (cruiser.GetRocketMines()) {
g2d.setColor(Color.green);
g2d.fillRect(_startPosX + 15, _startPosY + 10, 10, 15);
g2d.fillRect(_startPosX + 30, _startPosY + 10, 10, 15);
}
// верт площадка
if (cruiser.GetHelipad()) {
g2d.setColor(Color.ORANGE);
g2d.fill(new Ellipse2D.Double(_startPosX + 15, _startPosY + 25, 25, 25));
drawBlock.DrawningBlock(_startPosX, _startPosY, _cruiserWidth, g2d, cruiser.GetBodyColor());
}
//Изменение границ отрисовки
public boolean CanMove(Direction direction) {
if (cruiser == null) {
return false;
}
return switch (direction) {
case Left -> _startPosX - cruiser.GetStep() > 0;
case Up -> _startPosY - cruiser.GetStep() > 0;
case Right -> _startPosX + _cruiserWidth + cruiser.GetStep() < _pictureWidth;
case Down -> _startPosY + _cruiserHeight + cruiser.GetStep() < _pictureHeight;
};
}
}

View File

@ -0,0 +1,52 @@
package CruiserHard.Drawing;
import java.awt.*;
import java.util.Random;
import CruiserHard.Complication.DrawBlock;
import CruiserHard.Complication.DrawBlockOval;
import CruiserHard.Complication.DrawBlockTriangle;
import CruiserHard.Complication.IDrawBlock;
import CruiserHard.Entity.*;
public class DrawingHelipadCruiser extends DrawingCruiser {
private IDrawBlock drawingBlocks;
public DrawingHelipadCruiser(int speed, float weight, Color bodyColor, Color
elementsColor, boolean rocketMines, boolean helipad, int width, int height, int blockCount)
{
super(speed, weight, bodyColor, width, height, blockCount);
if (cruiser != null)
{
cruiser = new EntityHelipadCruiser(speed, weight, bodyColor,
elementsColor, rocketMines, helipad);
}
Random rand = new Random();
drawingBlocks = switch (rand.nextInt(3)) {
case 0 -> new DrawBlock();
case 1 -> new DrawBlockOval();
case 2 -> new DrawBlockTriangle();
default -> new DrawBlock();
};
drawingBlocks.SetBlockCount(blockCount);
}
@Override
public void DrawTransport(Graphics g) {
if (!(cruiser instanceof EntityHelipadCruiser helipadCrusier)) {
return;
}
super.DrawTransport(g);
drawingBlocks.DrawBlocks(_startPosX, _startPosY,g);
if (helipadCrusier.GetRocketMines()){
g.setColor(Color.green);
g.fillRect(_startPosX + 15, _startPosY + 10, 10, 15);
g.fillRect(_startPosX + 30, _startPosY + 10, 10, 15);
}
if (helipadCrusier.GetHelipad())
{
// верт площадка
g.setColor(Color.ORANGE);
g.fillOval(_startPosX + 15, _startPosY + 25, 25, 25);
}
}
}

View File

@ -0,0 +1,26 @@
package CruiserHard.Entity;
import java.awt.*;
import java.util.*;
public class EntityCruiser {
private int Speed; //Скорость
private float Weight; //Вес
private Color BodyColor; //Цвет
private float Step; //Шаг при перемещении
//Инициализация
public EntityCruiser(int speed, float weight, Color bodyColor) {
Random random = new Random();
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
Step = Speed * 100 / Weight;
}
public Color GetBodyColor(){
return BodyColor;
}
public float GetStep(){
return Step;
}
}

View File

@ -0,0 +1,23 @@
package CruiserHard.Entity;
import java.awt.*;
public class EntityHelipadCruiser extends EntityCruiser {
private Color ElementsColor;
public Color GetElementsColor() { return ElementsColor; }
private boolean RocketMines;
public boolean GetRocketMines(){
return RocketMines;
}
private boolean Helipad;
public boolean GetHelipad(){
return Helipad;
}
public EntityHelipadCruiser(int speed, float weight, Color bodyColor, Color
elementsColor, boolean rocketMines, boolean helipad) {
super(speed, weight, bodyColor);
ElementsColor = elementsColor;
RocketMines = rocketMines;
Helipad = helipad;
}
}

View File

@ -0,0 +1,176 @@
package CruiserHard;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import CruiserHard.Drawing.*;
import CruiserHard.Direction.*;
import CruiserHard.MovementStrategy.*;
public class FormCruiser{
private DrawingCruiser _drawingCruiser;
private AbstractStrategy _abstractStrategy;
Canvas canv;
public void Draw(){
canv.repaint();
}
public FormCruiser(){
JFrame frame = new JFrame("RoadTrain");
JButton buttonCreateCruiser = new JButton("Крейсер");
buttonCreateCruiser.setFocusPainted(false);
buttonCreateCruiser.setContentAreaFilled(false);
JButton buttonCreateHelipadCruiser = new JButton("СуперКрейсер");
buttonCreateHelipadCruiser.setFocusPainted(false);
buttonCreateHelipadCruiser.setContentAreaFilled(false);
String[] items = {
"ToCenter",
"ToBorder"
};
JComboBox comboBoxStrategy = new JComboBox(items);
JButton buttonStep = new JButton("Шаг");
buttonStep.setFocusPainted(false);
buttonStep.setContentAreaFilled(false);
JButton buttonUp = new JButton();
buttonUp.setFocusPainted(false);
buttonUp.setContentAreaFilled(false);
buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon(((new ImageIcon("src/CruiserHard/Images/totop.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonDown = new JButton();
buttonDown.setFocusPainted(false);
buttonDown.setContentAreaFilled(false);
buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon(((new ImageIcon("src/CruiserHard/Images/todown.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonLeft = new JButton();
buttonLeft.setFocusPainted(false);
buttonLeft.setContentAreaFilled(false);
buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon(((new ImageIcon("src/CruiserHard/Images/toleft.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonRight = new JButton();
buttonRight.setFocusPainted(false);
buttonRight.setContentAreaFilled(false);
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon(((new ImageIcon("src/CruiserHard/Images/toright.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
buttonCreateCruiser.addActionListener(
e -> {
Random random = new Random();
_drawingCruiser = new DrawingCruiser(random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1);
_drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома
random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома
,this.canv.getWidth(), this.canv.getHeight());
canv._drawingCruiser = _drawingCruiser;
Draw();
}
);
buttonCreateHelipadCruiser.addActionListener(
e -> {
Random random = new Random();
_drawingCruiser = new DrawingHelipadCruiser(random.nextInt(100) + 50,
random.nextInt(1000) + 500,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(),
this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1);
_drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома
random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома
,this.canv.getWidth(), this.canv.getHeight());
canv._drawingCruiser = _drawingCruiser;
Draw();
}
);
ActionListener actionListener = e -> {
if (_drawingCruiser == null){
return;
}
switch ((((JButton)(e.getSource())).getName())){
case "up":
_drawingCruiser.MoveTransport(Direction.Up);
break;
case "down":
_drawingCruiser.MoveTransport(Direction.Down);
break;
case "left":
_drawingCruiser.MoveTransport(Direction.Left);
break;
case "right":
_drawingCruiser.MoveTransport(Direction.Right);
break;
}
Draw();
};
buttonUp.addActionListener(actionListener);
buttonDown.addActionListener(actionListener);
buttonLeft.addActionListener(actionListener);
buttonRight.addActionListener(actionListener);
buttonStep.addActionListener(e -> {
if (_drawingCruiser == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
_abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()) {
case 0 -> new MoveToCenter();
case 1 -> new MoveToBorder();
default -> null;
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawingObjectCruiser(_drawingCruiser), this.canv.getWidth(), this.canv.getHeight());
}
if (_abstractStrategy == null)
{
return;
}
comboBoxStrategy.setEnabled(false);
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
});
frame.setSize(910, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 895, 500 - 40); // 40 - const, высота панели сверху
buttonCreateCruiser.setBounds(20, 420, 100, 40);
buttonCreateHelipadCruiser.setBounds(140, 420, 100, 40);
buttonUp.setBounds(800, 380, 40, 40);
buttonDown.setBounds(800, 420, 40, 40);
buttonLeft.setBounds(760, 420, 40, 40);
buttonRight.setBounds(840, 420, 40, 40);
comboBoxStrategy.setBounds(800,10,100,50);
buttonStep.setBounds(800,80,100,40);
frame.add(canv);
frame.add(buttonCreateCruiser);
frame.add(buttonCreateHelipadCruiser);
frame.add(buttonUp);
frame.add(buttonDown);
frame.add(buttonLeft);
frame.add(buttonRight);
frame.add(comboBoxStrategy);
frame.add(buttonStep);
frame.setVisible(true);
}
class Canvas extends JComponent{
public DrawingCruiser _drawingCruiser;
public Canvas(){}
public void paintComponent(Graphics g){
if (_drawingCruiser == null){
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
_drawingCruiser.DrawTransport(g2d);
super.repaint();
}
}
}

View File

Before

Width:  |  Height:  |  Size: 888 B

After

Width:  |  Height:  |  Size: 888 B

View File

Before

Width:  |  Height:  |  Size: 904 B

After

Width:  |  Height:  |  Size: 904 B

View File

Before

Width:  |  Height:  |  Size: 892 B

After

Width:  |  Height:  |  Size: 892 B

View File

Before

Width:  |  Height:  |  Size: 895 B

After

Width:  |  Height:  |  Size: 895 B

View File

@ -1,3 +1,4 @@
package CruiserHard;
public class Main {
public static void main(String[] args) {
FormCruiser fm = new FormCruiser();

View File

@ -0,0 +1,91 @@
package CruiserHard.MovementStrategy;;
import CruiserHard.Direction.*;
public abstract class AbstractStrategy {
private IMoveableObject moveableObject;
private Status state = Status.NotInit;
private int fieldWidth;
protected int GetFieldWidth() {
return fieldWidth;
}
private int fieldHeight;
protected int GetFieldHeight() {
return fieldHeight;
}
public Status GetStatus() {
return state;
}
public void SetData(IMoveableObject moveableObject, int width, int height) {
if (moveableObject == null) {
state = Status.NotInit;
return;
}
state = Status.InProgress;
this.moveableObject = moveableObject;
fieldWidth = width;
fieldHeight = height;
}
public void MakeStep() {
if (state != Status.InProgress) {
return;
}
if (isTargetDestination()) {
state = Status.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() {
return MoveTo(Direction.Left);
}
protected boolean MoveRight() {
return MoveTo(Direction.Right);
}
protected boolean MoveUp() { return MoveTo(Direction.Up); }
protected boolean MoveDown() {
return MoveTo(Direction.Down);
}
protected ObjectParameters GetObjectParameters() {
if (moveableObject == null) {
return null;
}
return moveableObject.GetObjectPosition();
}
protected Integer GetStep() {
if (state != Status.InProgress) {
return null;
}
return moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean isTargetDestination();
private boolean MoveTo(Direction direction) {
if (state != Status.InProgress) {
return false;
}
if (moveableObject == null) {
return false;
}
if (moveableObject.CheckCanMove(direction)) {
moveableObject.MoveObject(direction);
return true;
}
return false;
}
}

View File

@ -0,0 +1,39 @@
package CruiserHard.MovementStrategy;
import CruiserHard.Direction.*;
import CruiserHard.Drawing.*;
public class DrawingObjectCruiser implements IMoveableObject{
private DrawingCruiser _drawingCruiser = null;
public DrawingObjectCruiser (DrawingCruiser drawingCruiser)
{
_drawingCruiser = drawingCruiser;
}
@Override
public ObjectParameters GetObjectPosition() {
if (_drawingCruiser == null || _drawingCruiser.cruiser == null) {
return null;
}
return new ObjectParameters(_drawingCruiser.GetStartX(), _drawingCruiser.GetStartY(),
_drawingCruiser.GetWidth(), _drawingCruiser.GetHeight());
}
@Override
public int GetStep() {
if (_drawingCruiser != null && _drawingCruiser.cruiser!=null)
return (int)(_drawingCruiser.cruiser.GetStep());
return 0;
}
@Override
public boolean CheckCanMove(Direction direction) {
if (_drawingCruiser != null)
return _drawingCruiser.CanMove(direction);
return false;
}
@Override
public void MoveObject(Direction direction) {
if (_drawingCruiser != null)
_drawingCruiser.MoveTransport(direction);
}
}

View File

@ -0,0 +1,12 @@
package CruiserHard.MovementStrategy;
import CruiserHard.Direction.*;
public interface IMoveableObject{
ObjectParameters GetObjectPosition();
int GetStep();
boolean CheckCanMove(Direction direction);
void MoveObject(Direction direction);
}

View File

@ -0,0 +1,34 @@
package CruiserHard.MovementStrategy;
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean isTargetDestination() {
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.RightBorder() <= GetFieldWidth() &&
objParams.RightBorder() + GetStep() >= GetFieldWidth() &&
objParams.DownBorder() <= GetFieldHeight() &&
objParams.DownBorder() + GetStep() >= GetFieldHeight();
}
@Override
protected void MoveToTarget() {
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth();
if (Math.abs(diffX) > GetStep()) {
if (diffX < 0) {
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - GetFieldHeight();
if (Math.abs(diffY) > GetStep()) {
if (diffY < 0) {
MoveDown();
}
}
}
}

View File

@ -0,0 +1,38 @@
package CruiserHard.MovementStrategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean isTargetDestination() {
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.ObjectMiddleHorizontal() <= GetFieldWidth() / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= GetFieldWidth() / 2 &&
objParams.ObjectMiddleVertical() <= GetFieldHeight() / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= GetFieldHeight() / 2;
}
@Override
protected void MoveToTarget() {
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth() / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0) {
MoveLeft();
} else {
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - GetFieldHeight() / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
} else {
MoveDown();
}
}
}
}

View File

@ -0,0 +1,33 @@
package CruiserHard.MovementStrategy;
public class ObjectParameters {
private final int _x;
private final int _y;
private final int _width;
private final int _height;
public int RightBorder() {
return _x + _width;
}
public int DownBorder() {
return _y + _height;
}
public int ObjectMiddleHorizontal() {
return _x + _width / 2;
}
public int ObjectMiddleVertical() {
return _y + _height / 2;
}
public ObjectParameters(int x, int y, int width, int height) {
_x = x;
_y = y;
_width = width;
_height = height;
}
}

View File

@ -0,0 +1,6 @@
package CruiserHard.MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}

View File

@ -1,31 +0,0 @@
import java.awt.*;
import java.util.*;
public class EntityCruiser {
private int Speed; //Скорость
private float Weight; //Вес
private Color BodyColor; //Цвет
private float Step; //Шаг при перемещении
private boolean Helipad; //Наличие верт. площадки
private boolean RocketMines; //Наличие шахт
//Инициализация
public void Init(int speed, float weight, Color bodyColor, boolean rocketMines, boolean helipad)
{
Random random = new Random();
Speed = speed <= 0 ? random.nextInt(50, 150) : speed;
Weight = weight <= 0 ? random.nextInt(50, 150) : weight;
BodyColor = bodyColor;
RocketMines = rocketMines;
Helipad = helipad;
Step = Speed * 100 / Weight;
}
public boolean GetHelipad() {return Helipad;}
public boolean GetRocketMines() {return RocketMines;}
public Color GetBodyColor(){
return BodyColor;
}
public float GetStep(){
return Step;
}
}

View File

@ -1,111 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.util.*;
public class FormCruiser{
private DrawingCruiser _drawingCruiser;
Canvas canv;
public void Draw(){
canv.repaint();
}
public FormCruiser(){
JFrame frame = new JFrame("Cruiser");
JButton buttonCreate = new JButton("Создать");
buttonCreate.setFocusPainted(false);
buttonCreate.setContentAreaFilled(false);
JButton buttonUp = new JButton();
buttonUp.setFocusPainted(false);
buttonUp.setContentAreaFilled(false);
buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon(((new ImageIcon("src/Images/totop.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonDown = new JButton();
buttonDown.setFocusPainted(false);
buttonDown.setContentAreaFilled(false);
buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon(((new ImageIcon("src/Images/todown.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonLeft = new JButton();
buttonLeft.setFocusPainted(false);
buttonLeft.setContentAreaFilled(false);
buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon(((new ImageIcon("src/Images/toleft.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonRight = new JButton();
buttonRight.setFocusPainted(false);
buttonRight.setContentAreaFilled(false);
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon(((new ImageIcon("src/Images/toright.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
buttonCreate.addActionListener(
e -> {
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingCruiser = new DrawingCruiser();
_drawingCruiser.Init(
random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean());
_drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома
random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома
,this.canv.getWidth(), this.canv.getHeight());
canv._drawingCruiser = _drawingCruiser;
Draw();
}
);
ActionListener actionListener = e -> {
if (_drawingCruiser == null){
return;
}
switch ((((JButton)(e.getSource())).getName())){
case "up":
_drawingCruiser.MoveTransport(Direction.Up);
break;
case "down":
_drawingCruiser.MoveTransport(Direction.Down);
break;
case "left":
_drawingCruiser.MoveTransport(Direction.Left);
break;
case "right":
_drawingCruiser.MoveTransport(Direction.Right);
break;
}
Draw();
};
buttonUp.addActionListener(actionListener);
buttonDown.addActionListener(actionListener);
buttonLeft.addActionListener(actionListener);
buttonRight.addActionListener(actionListener);
frame.setSize(910, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 895, 500 - 40); // 40 - const, высота панели сверху
buttonCreate.setBounds(20, 420, 100, 40);
buttonUp.setBounds(800, 380, 40, 40);
buttonDown.setBounds(800, 420, 40, 40);
buttonLeft.setBounds(760, 420, 40, 40);
buttonRight.setBounds(840, 420, 40, 40);
frame.add(canv);
frame.add(buttonCreate);
frame.add(buttonUp);
frame.add(buttonDown);
frame.add(buttonLeft);
frame.add(buttonRight);
frame.setVisible(true);
}
class Canvas extends JComponent{
public DrawingCruiser _drawingCruiser;
public Canvas(){}
public void paintComponent(Graphics g){
if (_drawingCruiser == null){
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
_drawingCruiser.DrawTransport(g2d);
super.repaint();
}
}
}