С новым годом!!!

This commit is contained in:
Extrimal 2023-12-24 00:44:41 +03:00
parent 2af67567a2
commit 6d3c5baf22
16 changed files with 516 additions and 125 deletions

View File

@ -0,0 +1,57 @@
import java.util.function.Supplier;
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(DirectionType.LEFT);}
protected boolean MoveRight() {return MoveTo(DirectionType.RIGHT);}
protected boolean MoveUp() {return MoveTo(DirectionType.UP);}
protected boolean MoveDown() {return MoveTo(DirectionType.DOWN);}
protected Supplier<ObjectParameters> getObjectParameters = () -> 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(DirectionType direction){
if(state != Status.INPROGRESS)
return false;
if(moveableObject.checkCanMove(direction)){
moveableObject.moveObject(direction);
return true;
}
return false;
}
}

View File

@ -0,0 +1,120 @@
import java.awt.*;
public class DrawingAircraft {
protected EntityAircraft entityAircraft ;
protected void setEntityShip(EntityAircraft entityShip){
this.entityAircraft = entityAircraft;
}
public EntityAircraft getEntityShip() {
return entityAircraft;
}
private IDrawImprovements drawingImprovements;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
public int getPosX(){
return _startPosX;
}
protected int _startPosY;
public int getPosY(){
return _startPosY;
}
private final int _ShipWidth = 164;
public int getWidth() {
return _ShipWidth;
}
private final int _ShipHeight = 40;
public int getHeight(){
return _ShipHeight;
}
public DrawingAircraft(int speed, double weight, Color bodyColor, int width, int height, int improvementsType, int improvementsNumber) {
if (width < _ShipWidth || height < _ShipHeight)
return;
_pictureWidth = width;
_pictureHeight = height;
entityAircraft = new EntityAircraft(speed, weight, bodyColor);
switch (improvementsType){
case 1:
drawingImprovements = new DrawingImprovementsSquare();
break;
case 2:
drawingImprovements = new DrawingImprovementsCircle();
break;
default:
drawingImprovements = new DrawingImprovementsTriangle();
break;
}
drawingImprovements.setNumber(improvementsNumber);
}
public void setPosition(int x, int y)
{
if (_startPosX > (_pictureWidth - _ShipWidth) || _startPosY>(_pictureHeight - _ShipHeight) || x <= 0 || y <= 0)
x = y = 2;
_startPosX = x;
_startPosY = y;
}
public void drawTransport(Graphics2D g) {
if (entityAircraft == null)
return;
Color penColor = Color.BLACK;
Color bodyColor = entityAircraft.getBodyColor();
g.setColor(penColor);
g.drawLine(_startPosX+4, _startPosY, _startPosX + 124, _startPosY);
g.drawLine(_startPosX+124, _startPosY, _startPosX + 164, _startPosY+20);
g.drawLine(_startPosX + 164, _startPosY+20, _startPosX + 124, _startPosY + 40);
g.drawLine(_startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40);
g.drawLine(_startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40);
g.drawLine(_startPosX+4, _startPosY + 40, _startPosX + 4, _startPosY);
g.setColor(bodyColor);
g.fillRect(_startPosX, _startPosY + 6, 4, 12);
g.fillRect(_startPosX, _startPosY + 24, 4, 12);
g.setColor(penColor);
g.drawOval(_startPosX + 115, _startPosY + 13, 14, 14);
g.drawRect(_startPosX + 63, _startPosY + 13, 27, 14);
g.drawRect(_startPosX + 90, _startPosY + 9, 14, 22);
g.setColor(bodyColor);
drawingImprovements.drawImprovements(g, _startPosX, _startPosY);
}
public boolean canMove(DirectionType direction) {
if (entityAircraft == null) {
return false;
}
switch(direction) {
case LEFT:
return _startPosX - entityAircraft.step.get().intValue() > 0;
case UP:
return _startPosY - entityAircraft.step.get().intValue() > 0;
case RIGHT:
return _startPosX + entityAircraft.step.get().intValue() + _ShipWidth < _pictureWidth;
case DOWN:
return _startPosY + entityAircraft.step.get().intValue() + _ShipWidth < _pictureWidth;
default:
return false;
}
}
public void moveTransport(DirectionType direction)
{
if (entityAircraft == null)
return;
int step = entityAircraft.step.get().intValue();
switch (direction)
{
case LEFT:
if (_startPosX - step > 0)
_startPosX -= step;
break;
case UP:
if (_startPosY - step > 0)
_startPosY -= step;
break;
case RIGHT:
if (_startPosX + _ShipWidth + step < _pictureWidth)
_startPosX += step;
break;
case DOWN:
if (_startPosY + _ShipHeight + step < _pictureHeight)
_startPosY += step;
break;
}
}
}

View File

@ -1,87 +1,20 @@
import javax.swing.*;
import java.awt.*; import java.awt.*;
public class DrawingAircraftCarrier extends JPanel { public class DrawingAircraftCarrier extends DrawingAircraft {
private EntityAircraftCarrier entityAircraftCarrier; public DrawingAircraftCarrier(int speed, double weight, Color bodyColor,
public EntityAircraftCarrier getEntityAircraftCarrier(){ Color additionalColor, boolean cabin, boolean runway, int width, int height, int improvementsType, int improvementsNumber){
return entityAircraftCarrier; super(speed, weight, bodyColor, width, height, improvementsType, improvementsNumber);
if(entityAircraft != null)
entityAircraft = new EntityAircraftCarrier(speed, weight, bodyColor, additionalColor,cabin, runway);
} }
private int _pictureWidth; public void drawTransport(Graphics2D g)
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int _ShipWidth = 164;
private final int _ShipHeight = 40;
private DrawingImprovements drawingImprovements;
public boolean init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean Cabin, boolean Runway, int width, int height, int improvementsNumber)
{ {
if (_ShipWidth > width || _ShipHeight > height) if (!(entityAircraft instanceof EntityAircraftCarrier))
return false;
_pictureWidth = width;
_pictureHeight = height;
entityAircraftCarrier = new EntityAircraftCarrier();
entityAircraftCarrier.init(speed, weight, bodyColor, additionalColor,
Cabin, Runway);
drawingImprovements = new DrawingImprovements();
drawingImprovements.setNumber(improvementsNumber);
return true;
}
public void setPosition(int x, int y)
{
if (_startPosX > (_pictureWidth - _ShipWidth) || _startPosY>(_pictureHeight - _ShipHeight) || x <= 0 || y <= 0)
x = y = 2;
_startPosX = x;
_startPosY = y;
}
public void moveTransport(DirectionType direction)
{
if (entityAircraftCarrier == null)
return; return;
int step = entityAircraftCarrier.step.get().intValue(); EntityAircraftCarrier entityAircraftCarrier = (EntityAircraftCarrier) entityAircraft;
switch (direction)
{
case LEFT:
if (_startPosX - step > 0)
_startPosX -= step;
break;
case UP:
if (_startPosY - step > 0)
_startPosY -= step;
break;
case RIGHT:
if (_startPosX + _ShipWidth + step < _pictureWidth)
_startPosX += step;
break;
case DOWN:
if (_startPosY + _ShipHeight + step < _pictureHeight)
_startPosY += step;
break;
}
}
public void drawTransport(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
if (entityAircraftCarrier == null)
return;
Color penColor = Color.BLACK;
Color bodyColor = entityAircraftCarrier.getBodyColor();
Color additionalColor = entityAircraftCarrier.getAdditionalColor(); Color additionalColor = entityAircraftCarrier.getAdditionalColor();
g.setColor(penColor); super.drawTransport(g);
g.drawLine(_startPosX+4, _startPosY, _startPosX + 124, _startPosY);
g.drawLine(_startPosX+124, _startPosY, _startPosX + 164, _startPosY+20);
g.drawLine(_startPosX + 164, _startPosY+20, _startPosX + 124, _startPosY + 40);
g.drawLine(_startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40);
g.drawLine(_startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40);
g.drawLine(_startPosX+4, _startPosY + 40, _startPosX + 4, _startPosY);
g.setColor(bodyColor);
g.fillRect(_startPosX, _startPosY + 6, 4, 12);
g.fillRect(_startPosX, _startPosY + 24, 4, 12);
g.setColor(penColor);
g.drawOval(_startPosX + 115, _startPosY + 13, 14, 14);
g.drawRect(_startPosX + 63, _startPosY + 13, 27, 14);
g.drawRect(_startPosX + 90, _startPosY + 9, 14, 22);
if(entityAircraftCarrier.getRunway()){ if(entityAircraftCarrier.getRunway()){
g.setColor(additionalColor); g.setColor(additionalColor);
g.fillRect(_startPosX + 4, _startPosY + 13, 59, 14); g.fillRect(_startPosX + 4, _startPosY + 13, 59, 14);
@ -96,7 +29,5 @@ public class DrawingAircraftCarrier extends JPanel {
g.setColor(additionalColor); g.setColor(additionalColor);
g.fillRect(_startPosX + 90, _startPosY, 15, 9); g.fillRect(_startPosX + 90, _startPosY, 15, 9);
} }
g.setColor(bodyColor);
drawingImprovements.drawEngines(g, _startPosX, _startPosY);
} }
} }

View File

@ -0,0 +1,24 @@
import java.awt.*;
public class DrawingImprovementsCircle implements IDrawImprovements{
private NumberImprovement number;
public void setNumber(int x){
if(x <= 2)
number = NumberImprovement.TWO;
if(x == 4)
number = NumberImprovement.FOUR;
if(x >= 6)
number = NumberImprovement.SIX;
}
public void drawImprovements(Graphics2D g, int _startPosX, int _startPosY){
g.fillOval(_startPosX+5, _startPosY+1, 15, 12);
g.fillOval(_startPosX+5, _startPosY+27, 15, 13);
if (number == NumberImprovement.FOUR || number == NumberImprovement.SIX){
g.fillOval(_startPosX+26, _startPosY+1, 15, 12);
g.fillOval(_startPosX+26, _startPosY+27, 15, 13);
}
if (number == NumberImprovement.SIX){
g.fillOval(_startPosX+48, _startPosY+1, 15, 12);
g.fillOval(_startPosX+48, _startPosY+27, 15, 13);
}
}
}

View File

@ -0,0 +1,25 @@
import java.awt.*;
public class DrawingImprovementsSquare implements IDrawImprovements{
private NumberImprovement number;
public void setNumber(int x){
if(x <= 2)
number = NumberImprovement.TWO;
if(x == 4)
number = NumberImprovement.FOUR;
if(x >= 6)
number = NumberImprovement.SIX;
}
public void drawImprovements(Graphics2D g, int _startPosX, int _startPosY){
g.fillRect(_startPosX+5, _startPosY+1, 15, 12);
g.fillRect(_startPosX+5, _startPosY+27, 15, 13);
if (number == NumberImprovement.FOUR || number == NumberImprovement.SIX){
g.fillRect(_startPosX+26, _startPosY+1, 15, 12);
g.fillRect(_startPosX+26, _startPosY+27, 15, 13);
}
if (number == NumberImprovement.SIX){
g.fillRect(_startPosX+48, _startPosY+1, 15, 12);
g.fillRect(_startPosX+48, _startPosY+27, 15, 13);
}
}
}

View File

@ -0,0 +1,36 @@
import java.awt.*;
public class DrawingImprovementsTriangle implements IDrawImprovements{
private NumberImprovement number;
public void setNumber(int x){
if(x <= 2)
number = NumberImprovement.TWO;
if(x == 4)
number = NumberImprovement.FOUR;
if(x >= 6)
number = NumberImprovement.SIX;
}
public void drawImprovements(Graphics2D g, int _startPosX, int _startPosY){
int[] pointX = new int[]{ _startPosX+5, _startPosX+13, _startPosX+ 21};
int[] pointY = new int[]{ _startPosY + 1, _startPosY+13, _startPosY+1};
g.fillPolygon(pointX, pointY, 3);
pointX = new int[]{ _startPosX+5, _startPosX+13, _startPosX+ 21};
pointY = new int[]{ _startPosY + 40, _startPosY+27, _startPosY+40};
g.fillPolygon(pointX, pointY, 3);
if (number == NumberImprovement.FOUR || number == NumberImprovement.SIX){
pointX = new int[]{ _startPosX+26, _startPosX+34, _startPosX+ 42};
pointY = new int[]{ _startPosY + 1, _startPosY+13, _startPosY+1};
g.fillPolygon(pointX, pointY, 3);
pointX = new int[]{ _startPosX+26, _startPosX+34, _startPosX+ 42};
pointY = new int[]{ _startPosY + 40, _startPosY+27, _startPosY+40};
g.fillPolygon(pointX, pointY, 3);
}
if (number == NumberImprovement.SIX){
pointX = new int[]{ _startPosX+47, _startPosX+55, _startPosX+ 63};
pointY = new int[]{ _startPosY + 1, _startPosY+13, _startPosY+1};
g.fillPolygon(pointX, pointY, 3);
pointX = new int[]{ _startPosX+47, _startPosX+55, _startPosX+ 63};
pointY = new int[]{ _startPosY + 40, _startPosY+27, _startPosY+40};
g.fillPolygon(pointX, pointY, 3);
}
}
}

View File

@ -0,0 +1,27 @@
public class DrawingObjectAircraftCarrier implements IMoveableObject{
private final DrawingAircraft drawingAircraft;
public DrawingObjectAircraftCarrier(DrawingAircraft drawingShip){
this.drawingAircraft = drawingShip;
}
public ObjectParameters getObjectPosition(){
if(drawingAircraft == null || drawingAircraft.getEntityShip() == null)
return null;
return new ObjectParameters(drawingAircraft.getPosX(), drawingAircraft.getPosY(),
drawingAircraft.getWidth(), drawingAircraft.getHeight());
}
public int getStep(){
if(drawingAircraft.getEntityShip() == null)
return 0;
return drawingAircraft.getEntityShip().step.get().intValue();
}
public boolean checkCanMove(DirectionType direction){
if(drawingAircraft == null)
return false;
return drawingAircraft.canMove(direction);
}
public void moveObject(DirectionType direction){
drawingAircraft.moveTransport(direction);
}
}

View File

@ -0,0 +1,24 @@
import java.awt.*;
import java.util.function.Supplier;
public class EntityAircraft {
private int speed;
public int getSpeed(){
return speed;
}
private double weight;
public double getWeight(){
return weight;
}
private Color bodyColor;
public Color getBodyColor(){
return bodyColor;
}
public Supplier<Double> step = () ->(double) speed * 100 / weight;
public EntityAircraft(int speed, double weight, Color bodyColor){
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
}
}

View File

@ -1,19 +1,6 @@
import java.awt.*; import java.awt.*;
import java.util.function.Supplier;
public class EntityAircraftCarrier { public class EntityAircraftCarrier extends EntityAircraft{
private int speed;
public int getSpeed(){
return speed;
}
private double weight;
public double getWeight(){
return weight;
}
private Color bodyColor;
public Color getBodyColor(){
return bodyColor;
}
private Color additionalColor; private Color additionalColor;
public Color getAdditionalColor(){ public Color getAdditionalColor(){
return additionalColor; return additionalColor;
@ -26,12 +13,9 @@ public class EntityAircraftCarrier {
public boolean getRunway() { public boolean getRunway() {
return Runway; return Runway;
} }
public Supplier<Double> step = () -> (double) speed * 100 / weight; public EntityAircraftCarrier(int speed, double weight, Color bodyColor, Color
public void init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean Cabin, boolean Runway) { additionalColor, boolean Cabin, boolean Runway) {
this.speed = speed; super(speed, weight, bodyColor);
this.weight = weight;
this.bodyColor = bodyColor;
this.additionalColor = additionalColor; this.additionalColor = additionalColor;
this.Cabin = Cabin; this.Cabin = Cabin;
this.Runway = Runway; this.Runway = Runway;

View File

@ -6,7 +6,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Random; import java.util.Random;
public class FrameAircraftCarrier extends JFrame { public class FrameAircraftCarrier extends JFrame {
private DrawingAircraftCarrier drawingAircraftCarrier; private DrawingAircraft drawingAircraft;
private AbstractStrategy abstractStrategy;
private JComboBox comboBoxStrategy;
private final JComponent pictureBox; private final JComponent pictureBox;
public FrameAircraftCarrier() throws IOException { public FrameAircraftCarrier() throws IOException {
super("Военный корабль"); super("Военный корабль");
@ -16,17 +18,22 @@ public class FrameAircraftCarrier extends JFrame {
public void paintComponent(Graphics graphics){ public void paintComponent(Graphics graphics){
super.paintComponent(graphics); super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics; Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingAircraftCarrier!= null) drawingAircraftCarrier.drawTransport(graphics2D); if (drawingAircraft!= null) drawingAircraft.drawTransport(graphics2D);
super.repaint(); super.repaint();
} }
}; };
JButton createButton = new JButton("Создать"); comboBoxStrategy = new JComboBox<>(new String[]{"К центру", "К границе"});
JButton stepButton = new JButton("Шаг");
JButton createShipButton = new JButton("Создать корабль");
JButton createAircraftCarrierButton = new JButton("Создать Военный корабль");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\right.png")))); JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\right.png"))));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\left.png")))); JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\left.png"))));
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\up.png")))); JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\up.png"))));
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\down.png")))); JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\down.png"))));
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
createButton.addActionListener(e -> buttonCreateClick()); createShipButton.addActionListener(e -> buttonCreateShipClick());
createAircraftCarrierButton.addActionListener(e -> buttonCreateAircraftCarrierClick());
stepButton.addActionListener(e -> buttonStepClick());
rightButton.setActionCommand("right"); rightButton.setActionCommand("right");
rightButton.addActionListener(this::buttonMoveClick); rightButton.addActionListener(this::buttonMoveClick);
leftButton.setActionCommand("left"); leftButton.setActionCommand("left");
@ -38,17 +45,21 @@ public class FrameAircraftCarrier extends JFrame {
//component addition //component addition
setLayout(new BorderLayout()); setLayout(new BorderLayout());
JPanel panelAircraftCarrier = new JPanel(new BorderLayout()); JPanel panelAircraftCarrier = new JPanel(new BorderLayout());
JPanel createPanel = new JPanel(new BorderLayout());
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
createPanel.add(createButton, BorderLayout.SOUTH);
JPanel movementPanel = new JPanel(new GridBagLayout());
JPanel rightPanel = new JPanel(new BorderLayout()); JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(movementPanel, BorderLayout.SOUTH); JPanel leftPanel = new JPanel(new BorderLayout());
rightButton.setPreferredSize(new Dimension(30,30));
GridBagConstraints constraints = new GridBagConstraints(); GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
JPanel createPanel = new JPanel(new GridBagLayout());
constraints.gridx = 0;
constraints.gridy = 0;
createPanel.add(createShipButton, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
createPanel.add(createAircraftCarrierButton, constraints);
JPanel movementPanel = new JPanel(new GridBagLayout());
rightButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 2; constraints.gridx = 2;
constraints.gridy = 1; constraints.gridy = 1;
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
movementPanel.add(rightButton, constraints); movementPanel.add(rightButton, constraints);
leftButton.setPreferredSize(new Dimension(30,30)); leftButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 0; constraints.gridx = 0;
@ -62,46 +73,96 @@ public class FrameAircraftCarrier extends JFrame {
constraints.gridx = 1; constraints.gridx = 1;
constraints.gridy = 1; constraints.gridy = 1;
movementPanel.add(downButton, constraints); movementPanel.add(downButton, constraints);
JPanel stepPanel = new JPanel(new GridBagLayout());
constraints.gridx = 0;
constraints.gridy = 0;
stepPanel.add(comboBoxStrategy, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
stepPanel.add(stepButton, constraints);
add(pictureBox); add(pictureBox);
rightPanel.add(movementPanel, BorderLayout.SOUTH);
rightPanel.add(stepPanel, BorderLayout.NORTH);
leftPanel.add(createPanel, BorderLayout.SOUTH);
panelAircraftCarrier.add(rightPanel, BorderLayout.EAST); panelAircraftCarrier.add(rightPanel, BorderLayout.EAST);
panelAircraftCarrier.add(createPanel, BorderLayout.WEST); panelAircraftCarrier.add(leftPanel, BorderLayout.WEST);
add(panelAircraftCarrier,BorderLayout.CENTER); add(panelAircraftCarrier,BorderLayout.CENTER);
setVisible(true); setVisible(true);
} }
private void buttonCreateClick() { private void buttonCreateAircraftCarrierClick() {
Random random = new Random(); Random random = new Random();
drawingAircraftCarrier = new DrawingAircraftCarrier();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingAircraftCarrier.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, drawingAircraft = new DrawingAircraftCarrier(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
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(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), drawingAircraft.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(),
(random.nextInt(3)+1)*2);
drawingAircraftCarrier.setPosition(random.nextInt(90) + 5, random.nextInt(90) + 5);
draw(); draw();
} }
private void buttonCreateShipClick(){
Random random = new Random();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingAircraft = new DrawingAircraft(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
drawingAircraft.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonStepClick() {
if (drawingAircraft == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
switch(comboBoxStrategy.getSelectedIndex()) {
case 0:
abstractStrategy = new MoveToCenter();
break;
case 1:
abstractStrategy = new MoveToBorder();
break;
default:
abstractStrategy = null;
break;
};
if (abstractStrategy == null)
{
return;
}
abstractStrategy.SetData(new DrawingObjectAircraftCarrier(drawingAircraft), pictureBox.getWidth(),
pictureBox.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (abstractStrategy == null)
{
return;
}
abstractStrategy.MakeStep();
draw();
if (abstractStrategy.GetStatus() == Status.FINISH)
{
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
}
private void buttonMoveClick(ActionEvent event) { private void buttonMoveClick(ActionEvent event) {
if(drawingAircraftCarrier == null || drawingAircraftCarrier.getEntityAircraftCarrier() == null) if(drawingAircraft == null || drawingAircraft.getEntityShip() == null)
return; return;
switch (event.getActionCommand()) switch (event.getActionCommand())
{ {
case "left": case "left":
drawingAircraftCarrier.moveTransport(DirectionType.LEFT); drawingAircraft.moveTransport(DirectionType.LEFT);
break; break;
case "right": case "right":
drawingAircraftCarrier.moveTransport(DirectionType.RIGHT); drawingAircraft.moveTransport(DirectionType.RIGHT);
break; break;
case "up": case "up":
drawingAircraftCarrier.moveTransport(DirectionType.UP); drawingAircraft.moveTransport(DirectionType.UP);
break; break;
case "down": case "down":
drawingAircraftCarrier.moveTransport(DirectionType.DOWN); drawingAircraft.moveTransport(DirectionType.DOWN);
break; break;
} }
draw(); draw();
} }
private void draw() { private void draw() {
if (drawingAircraftCarrier == null) if (drawingAircraft == null)
return; return;
pictureBox.repaint(); pictureBox.repaint();
} }

View File

@ -0,0 +1,6 @@
import java.awt.*;
public interface IDrawImprovements {
public void setNumber(int x);
public void drawImprovements(Graphics2D graphics2D, int _startX, int _startY);
}

View File

@ -0,0 +1,6 @@
public interface IMoveableObject {
ObjectParameters getObjectPosition();
int getStep();
boolean checkCanMove(DirectionType direction);
void moveObject(DirectionType direction);
}

View File

@ -0,0 +1,34 @@
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean IsTargetDestination() {
var objParams = getObjectParameters.get();
if(objParams == null)
return false;
return objParams.getRightBorder() + GetStep() >= getFieldWidth() &&
objParams.getDownBorder() + GetStep() >= getFieldHeight();
}
@Override
protected void MoveToTarget() {
var objParams = getObjectParameters.get();
if(objParams == null)
return;
var diffX = objParams.getRightBorder() - getFieldWidth();
var diffY = objParams.getDownBorder() - getFieldHeight();
if(diffX >= 0)
{
MoveDown();
}
else if(diffY >= 0)
{
MoveRight();
}
else if(Math.abs(diffX) > Math.abs(diffY))
{
MoveRight();
}
else
{
MoveDown();
}
}
}

View File

@ -0,0 +1,32 @@
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean IsTargetDestination() {
var objParams = getObjectParameters.get();
if(objParams == null)
return false;
return objParams.getObjectMiddleHorizontal() <= getFieldWidth() / 2 &&
objParams.getObjectMiddleHorizontal()+GetStep() >= getFieldWidth() / 2 &&
objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 &&
objParams.getObjectMiddleVertical() + GetStep() >= getFieldHeight() / 2;
}
@Override
protected void MoveToTarget() {
var objParams = getObjectParameters.get();
if(objParams == null)
return;
var diffX = objParams.getObjectMiddleHorizontal() - getFieldWidth() / 2;
if(Math.abs(diffX) > GetStep()){
if(diffX > 0)
MoveLeft();
else
MoveRight();
}
var diffY = objParams.getObjectMiddleVertical() - getFieldHeight() / 2;
if(Math.abs(diffY) > GetStep()){
if(diffY > 0)
MoveUp();
else
MoveDown();
}
}
}

View File

@ -0,0 +1,19 @@
public class ObjectParameters {
private final int _x;
private final int _y;
private final int _width;
private final int _height;
public int getLeftBorder() {return _x;}
public int getTopBorder() {return _y;}
public int getRightBorder() {return _x + _width;}
public int getDownBorder() {return _y + _height;}
public int getObjectMiddleHorizontal() {return _x + this._width / 2;}
public int getObjectMiddleVertical() {return _y + this._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,5 @@
public enum Status {
NOTINIT,
INPROGRESS,
FINISH
}