This commit is contained in:
Дмитрий Блохин 2023-12-30 02:08:34 +04:00
parent 1293ca1ba3
commit 5c0d61e232
18 changed files with 620 additions and 168 deletions

View File

@ -1,29 +1,48 @@
package Drawnings; package Drawnings;
import java.awt.*; import java.awt.*;
import java.util.Random;
import Entities.*; import Entities.*;
import MovementStrategy.*; import MovementStrategy.*;
public class DrawingAirbus { public class DrawingAirbus {
public EntityAirbus entityAirbus; public EntityAirbus entityAirbus;
public DrawingPortholes _portholes; public IDrawingPortholes _portholes;
private int _pictureWidth; private int _pictureWidth;
private int _pictureHeight; private int _pictureHeight;
private int _startPosX; protected int _startPosX;
private int _startPosY; protected int _startPosY;
private int _airbusWidth = 210; private int _airbusWidth = 210;
private int _airbusHeight = 100; private int _airbusHeight = 100;
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isadditionalEngine, int countPortholes, int width, int height) { public int GetPosX() { return _startPosX; }
public int GetPosY() { return _startPosY; }
public int GetWidth() { return _airbusWidth; }
public int GetHeight() { return _airbusHeight; }
public DrawingAirbus(int speed, float weight, Color bodyColor, int countPortholes, int width, int height) {
if (width < _airbusHeight || height < _airbusWidth) if (width < _airbusHeight || height < _airbusWidth)
return; return;
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
entityAirbus = new EntityAirbus(); entityAirbus = new EntityAirbus(speed, weight, bodyColor);
entityAirbus.Init(speed, weight, bodyColor, additionalColor, isCompartment, isadditionalEngine);
_portholes = new DrawingPortholes(); Random random = new Random();
switch (random.nextInt(0,3)) {
case 0:
_portholes = new DrawingPortholesCircle();
break;
case 1:
_portholes = new DrawingPortholesHeart();
break;
case 2:
_portholes = new DrawingPortholesSquare();
break;
default:
_portholes = new DrawingPortholesCircle();
break;
}
_portholes.SetCount(countPortholes); _portholes.SetCount(countPortholes);
} }
@ -40,35 +59,45 @@ public class DrawingAirbus {
} }
} }
public boolean CanMove(Direction direction)
{
if (entityAirbus == null)
{
return false;
}
switch (direction)
{
case Left:
return _startPosX - entityAirbus.Step > 5;
case Right:
return _startPosX + _airbusWidth + entityAirbus.Step < _pictureWidth;
case Up:
return _startPosY - entityAirbus.Step > 0;
case Down:
return _startPosY + _airbusHeight + entityAirbus.Step < _pictureHeight;
default:
return false;
}
}
public void MoveTransport(Direction direction){ public void MoveTransport(Direction direction){
if (entityAirbus == null) { if (!CanMove(direction) || entityAirbus == null)
{
return; return;
} }
switch (direction) { switch (direction) {
case Left: case Left:
if (_startPosX - entityAirbus.Step > 5)
{
_startPosX -= entityAirbus.Step; _startPosX -= entityAirbus.Step;
}
break; break;
case Right: case Right:
if (_startPosX + _airbusWidth + entityAirbus.Step < _pictureWidth)
{
_startPosX += entityAirbus.Step; _startPosX += entityAirbus.Step;
}
break; break;
case Up: case Up:
if (_startPosY - entityAirbus.Step > 0)
{
_startPosY -= entityAirbus.Step; _startPosY -= entityAirbus.Step;
}
break; break;
case Down: case Down:
if (_startPosY + _airbusHeight + entityAirbus.Step < _pictureHeight)
{
_startPosY += entityAirbus.Step; _startPosY += entityAirbus.Step;
}
break; break;
} }
} }
@ -78,25 +107,37 @@ public class DrawingAirbus {
if (entityAirbus == null) { if (entityAirbus == null) {
return; return;
} }
// иллюминаторы
_portholes.Draw(g, _startPosX+30, _startPosY+34);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
//Тело //Тело
g.setColor(entityAirbus.getBodyColor());
g.fillRect(_startPosX + 5, _startPosY + 50, 170, 30);
g.fillArc(_startPosX - 5, _startPosY + 50, 20, 30, 90, 180);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 5, _startPosY + 50, 170, 30); g.drawRect(_startPosX + 5, _startPosY + 50, 170, 30);
g.drawArc(_startPosX - 5, _startPosY + 50, 20, 30, 90, 180); g.drawArc(_startPosX - 5, _startPosY + 50, 20, 30, 90, 180);
_portholes.Draw(g, _startPosX+30, _startPosY+34);
//Заднее крыло //Заднее крыло
g.drawLine( _startPosX, _startPosY, _startPosX + 50, _startPosY + 50); g.setColor(Color.BLACK);
g.drawLine( _startPosX, _startPosY, _startPosX, _startPosY + 52); g.drawPolygon(new int[] {_startPosX + 50,_startPosX, _startPosX}, new int[] {_startPosY + 50, _startPosY, _startPosY + 50}, 3);
g.setColor(entityAirbus.getBodyColor());
g.fillPolygon(new int[] {_startPosX + 50,_startPosX, _startPosX}, new int[] {_startPosY + 50, _startPosY, _startPosY + 50}, 3);
//Заднее боковые крылья //Заднее боковые крылья
g.setColor(Color.BLACK);
g.drawOval(_startPosX - 7, _startPosY + 45, 30, 8); g.drawOval(_startPosX - 7, _startPosY + 45, 30, 8);
g.setColor(entityAirbus.getBodyColor());
g.fillOval(_startPosX - 7, _startPosY + 45, 30, 8); g.fillOval(_startPosX - 7, _startPosY + 45, 30, 8);
//Нос //Нос
g.drawLine( _startPosX + 175, _startPosY + 50, _startPosX + 200, _startPosY + 65); g.setColor(Color.BLACK);
g.drawLine( _startPosX + 200, _startPosY + 65, _startPosX + 175, _startPosY + 80); g.drawPolygon(new int[] {_startPosX + 175,_startPosX + 200, _startPosX + 175}, new int[] {_startPosY + 50, _startPosY + 65, _startPosY + 65}, 3);
g.drawLine( _startPosX + 175, _startPosY + 50, _startPosX + 175, _startPosY + 80); g.drawPolygon(new int[] {_startPosX + 175,_startPosX + 200, _startPosX + 175}, new int[] {_startPosY + 80, _startPosY + 65, _startPosY + 65}, 3);
g.drawLine( _startPosX + 175, _startPosY + 65, _startPosX + 200, _startPosY + 65); g.setColor(entityAirbus.getBodyColor());
g.fillPolygon(new int[] {_startPosX + 175,_startPosX + 200, _startPosX + 175}, new int[] {_startPosY + 50, _startPosY + 65, _startPosY + 65}, 3);
g.fillPolygon(new int[] {_startPosX + 175,_startPosX + 200, _startPosX + 175}, new int[] {_startPosY + 80, _startPosY + 65, _startPosY + 65}, 3);
//Крылья //Крылья
g.setColor(Color.BLACK);
g.drawArc(_startPosX + 49, _startPosY + 62, 5, 5, 90, 180); g.drawArc(_startPosX + 49, _startPosY + 62, 5, 5, 90, 180);
g.drawLine( _startPosX + 51, _startPosY + 62, _startPosX + 140, _startPosY + 62); g.drawLine( _startPosX + 51, _startPosY + 62, _startPosX + 140, _startPosY + 62);
g.drawArc( _startPosX + 137, _startPosY + 62, 5, 5, 2790, 180); g.drawArc( _startPosX + 137, _startPosY + 62, 5, 5, 2790, 180);
@ -105,25 +146,14 @@ public class DrawingAirbus {
g.drawLine(_startPosX + 55, _startPosY + 80, _startPosX + 55, _startPosY + 90); g.drawLine(_startPosX + 55, _startPosY + 80, _startPosX + 55, _startPosY + 90);
g.drawOval(_startPosX + 47, _startPosY + 90, 5, 5); g.drawOval(_startPosX + 47, _startPosY + 90, 5, 5);
g.drawOval( _startPosX + 57, _startPosY + 90, 5, 5); g.drawOval( _startPosX + 57, _startPosY + 90, 5, 5);
g.setColor(entityAirbus.getBodyColor());
g.fillOval(_startPosX + 47, _startPosY + 90, 5, 5);
g.fillOval( _startPosX + 57, _startPosY + 90, 5, 5);
//Передние шасси //Передние шасси
g.setColor(Color.BLACK);
g.drawLine( _startPosX + 165, _startPosY + 80, _startPosX + 165, _startPosY + 90); g.drawLine( _startPosX + 165, _startPosY + 80, _startPosX + 165, _startPosY + 90);
g.drawOval( _startPosX + 163, _startPosY + 91, 5, 5); g.drawOval( _startPosX + 163, _startPosY + 91, 5, 5);
//Пассажирсакий доп. отсек g.setColor(entityAirbus.getBodyColor());
if (entityAirbus.IsCompartment()) g.fillOval( _startPosX + 163, _startPosY + 91, 5, 5);
{
g.setColor(entityAirbus.getAdditionalColor());
g.drawArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180);
g.fillArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180);
}
// Доп. двигатели
if (entityAirbus.IsAdditionalEngine())
{
g.drawLine(_startPosX + 95, _startPosY + 68, _startPosX + 95, _startPosY + 75);
g.setColor(entityAirbus.getAdditionalColor());
int[] xPolygon = { _startPosX + 83, _startPosX + 103, _startPosX + 103, _startPosX + 83, _startPosX + 83};
int[] yPolygon = {_startPosY + 78, _startPosY + 73, _startPosY + 93, _startPosY + 88, _startPosY + 78};
g.drawPolygon(xPolygon, yPolygon, xPolygon.length);
g.fillPolygon(xPolygon, yPolygon, xPolygon.length);
}
} }
} }

View File

@ -0,0 +1,44 @@
package Drawnings;
import Entities.EntityPlane;
import java.awt.*;
public class DrawingPlane extends DrawingAirbus {
public DrawingPlane(int speed, float weight, Color bodyColor, int countPortholes, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine, int width, int height)
{
super(speed, weight, bodyColor, countPortholes, width, height);
if (entityAirbus != null) {
entityAirbus = new EntityPlane(speed, weight, bodyColor, additionalColor, isCompartment, isAdditionalEngine);
}
}
@Override
public void DrawTransport(Graphics2D g) {
if (entityAirbus == null)
{
return;
}
Color additionalColor = ((EntityPlane)entityAirbus).getAdditionalColor();
//Пассажирсакий доп. отсек
if (((EntityPlane)entityAirbus).IsCompartment())
{
g.setColor(additionalColor);
g.drawArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180);
g.fillArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180);
}
super.DrawTransport(g);
// Доп. двигатели
if (((EntityPlane)entityAirbus).IsAdditionalEngine())
{
g.drawLine(_startPosX + 95, _startPosY + 68, _startPosX + 95, _startPosY + 75);
g.setColor(additionalColor);
int[] xPolygon = { _startPosX + 83, _startPosX + 103, _startPosX + 103, _startPosX + 83, _startPosX + 83};
int[] yPolygon = {_startPosY + 78, _startPosY + 73, _startPosY + 93, _startPosY + 88, _startPosY + 78};
g.drawPolygon(xPolygon, yPolygon, xPolygon.length);
g.fillPolygon(xPolygon, yPolygon, xPolygon.length);
}
}
}

View File

@ -1,67 +0,0 @@
package Drawnings;
import java.awt.*;
import Entities.*;
public class DrawingPortholes {
private CountPortholes _porthole;
public CountPortholes getCount()
{
return _porthole;
}
public void SetCount (int count) {
switch (count) {
case 10:
_porthole = CountPortholes.Ten;
break;
case 20:
_porthole = CountPortholes.Twenty;
break;
case 30:
_porthole = CountPortholes.Thirty;
break;
default:
_porthole = CountPortholes.Ten;
break;
}
}
public void Draw (Graphics2D g, int _startPosx, int _startPoxY) {
g.setColor(Color.BLACK);
if (_porthole == null) {return;}
for (int i = 0; i < 10; ++i)
{
g.setColor(Color.cyan);
g.fillOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3);
}
if (_porthole != CountPortholes.Ten) {
for (int i=0; i < 5; ++i)
{
g.setColor(Color.cyan);
g.fillOval(_startPosx - 15 + i*5, _startPoxY+28, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx - 15 + i*5, _startPoxY+28, 3, 3);
}
for (int i=0; i < 5; ++i)
{
g.setColor(Color.cyan);
g.fillOval(_startPosx + 115 + i*5, _startPoxY+28, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx + 115 + i*5, _startPoxY+28, 3, 3);
}
}
if (_porthole == CountPortholes.Thirty){
for (int i = 0; i < 10; ++i)
{
g.setColor(Color.cyan);
g.fillOval(_startPosx + 19 + i*8, _startPoxY+37, 3, 3);
g.setColor(Color.black);
g.drawOval(_startPosx + 19 + i*8, _startPoxY+37, 3, 3);
}
}
}
}

View File

@ -0,0 +1,53 @@
package Drawnings;
import java.awt.*;
import Entities.*;
public class DrawingPortholesCircle implements IDrawingPortholes {
private CountPortholes _porthole;
public CountPortholes getCount()
{
return _porthole;
}
public void SetCount (int count) {
switch (count) {
case 10:
_porthole = CountPortholes.Ten;
break;
case 20:
_porthole = CountPortholes.Twenty;
break;
case 30:
_porthole = CountPortholes.Thirty;
break;
default:
_porthole = CountPortholes.Ten;
break;
}
}
protected void drawPortholes(Graphics2D g, int posX, int posY){
g.setColor(Color.cyan);
g.fillOval(posX, posY, 3, 3);
g.setColor(Color.black);
g.drawOval(posX, posY, 3, 3);
}
public void Draw (Graphics2D g, int _startPosX, int _startPosY) {
if (_porthole == null) {return;}
for (int i = 0; i < 10; ++i) {
drawPortholes(g, _startPosX + 19 + i * 8, _startPosY + 21);
}
if (_porthole != CountPortholes.Ten) {
for (int i = 0; i < 5; ++i) {
drawPortholes(g, _startPosX - 15 + i * 5, _startPosY + 28);
drawPortholes(g, _startPosX + 115 + i * 5, _startPosY + 28);
}
}
if (_porthole == CountPortholes.Thirty) {
for (int i = 0; i < 10; ++i) {
drawPortholes(g, _startPosX + 19 + i * 8, _startPosY + 37);
}
}
}
}

View File

@ -0,0 +1,16 @@
package Drawnings;
import Entities.CountPortholes;
import java.awt.*;
public class DrawingPortholesHeart extends DrawingPortholesCircle {
protected void drawPortholes(Graphics2D g, int posX, int posY) {
int[] HeartX = {posX + 2, posX, posX, posX + 1, posX + 2, posX + 3, posX + 5, posX + 5};
int[] HeartY = {posY + 4, posY + 2, posY, posY, posY + 1, posY, posY, posY + 2};
g.setColor(Color.cyan);
g.fillPolygon(HeartX, HeartY, HeartX.length);
g.setColor(Color.black);
g.drawPolygon(HeartX, HeartY, HeartX.length);
}
}

View File

@ -0,0 +1,14 @@
package Drawnings;
import Entities.CountPortholes;
import java.awt.*;
public class DrawingPortholesSquare extends DrawingPortholesCircle {
protected void drawPortholes(Graphics2D g, int posX, int posY){
g.setColor(Color.cyan);
g.fillRect(posX, posY, 3, 3);
g.setColor(Color.black);
g.drawRect(posX, posY, 3, 3);
}
}

View File

@ -0,0 +1,10 @@
package Drawnings;
import java.awt.*;
import Entities.CountPortholes;
public interface IDrawingPortholes {
public CountPortholes getCount();
public void SetCount (int count);
public void Draw (Graphics2D g, int _startPosX, int _startPoxY);
}

View File

@ -6,11 +6,7 @@ public class EntityAirbus {
private int Speed; private int Speed;
private float Weight; private float Weight;
private Color BodyColor; private Color BodyColor;
private Color AdditionalColor; public float Step;
private boolean IsCompartment;
private boolean IsAdditionalEngine;
public int Step;
public int getSpeed() { public int getSpeed() {
return Speed; return Speed;
@ -21,26 +17,13 @@ public class EntityAirbus {
public Color getBodyColor() { public Color getBodyColor() {
return BodyColor; return BodyColor;
} }
public Color getAdditionalColor() {
return AdditionalColor;
}
public boolean IsCompartment() {
return IsCompartment;
}
public boolean IsAdditionalEngine() {
return IsAdditionalEngine;
}
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine) public EntityAirbus(int speed, float weight, Color bodyColor)
{ {
Weight = weight; Weight = weight;
Speed = speed; Speed = speed;
BodyColor = bodyColor; BodyColor = bodyColor;
AdditionalColor = additionalColor;
IsCompartment = isCompartment;
IsAdditionalEngine = isAdditionalEngine;
Step = Speed * 100 / (int) Weight; Step = Speed * 100 / (int) Weight;
} }

View File

@ -0,0 +1,21 @@
package Entities;
import javax.swing.text.AttributeSet;
import java.awt.*;
public class EntityPlane extends EntityAirbus {
private Color AdditionalColor;
private boolean IsCompartment;
private boolean IsAdditionalEngine;
public EntityPlane(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
IsCompartment = isCompartment;
IsAdditionalEngine = isAdditionalEngine;
}
public Color getAdditionalColor() { return AdditionalColor; }
public boolean IsCompartment() { return IsCompartment; }
public boolean IsAdditionalEngine() { return IsAdditionalEngine; }
}

View File

@ -7,12 +7,23 @@ import MovementStrategy.*;
public class FormAirbus extends JFrame { public class FormAirbus extends JFrame {
private DrawingAirbus _drawningAirbus; private int width;
private Canvas canvas = new Canvas(); private int height;
private DrawingAirbus _drawingAirbus;
private AbstractStrategy _abstractStrategy;
private Canvas canvas;
JLabel labelCount = new JLabel("Введите число иллюминаторов:"); // выбор кол-ва иллюминаторов
private JTextField fieldCount = new JTextField(); JLabel labelCount;
private JButton buttonCreate; private JTextField fieldCount;
// выбор стратегии
JLabel labelStrategy;
JComboBox comboBoxStrategy;
JButton buttonStrategy;
private JButton buttonCreateAirbus;
private JButton buttonCreatePlane;
private JButton buttonUp; private JButton buttonUp;
private JButton buttonDown; private JButton buttonDown;
private JButton buttonRight; private JButton buttonRight;
@ -26,42 +37,67 @@ public class FormAirbus extends JFrame {
private void InitializeComponent() private void InitializeComponent()
{ {
buttonCreate = new JButton("Создать самолёт"); canvas = new Canvas();
labelCount = new JLabel("Введите число иллюминаторов:");
fieldCount = new JTextField();
labelStrategy = new JLabel("Шаг стратегии:");
comboBoxStrategy = new JComboBox(new Integer[] {0, 1});
buttonStrategy = new JButton("Выбрать стратегию");
buttonStrategy.setMargin(new Insets(0, 0, 0, 0));
buttonCreateAirbus = new JButton("Создать аэробус");
buttonCreateAirbus.setMargin(new Insets(0, 0, 0, 0));
buttonCreatePlane = new JButton("Создать самолёт");
buttonCreatePlane.setMargin(new Insets(0, 0, 0, 0));
buttonUp = new JButton(); buttonUp = new JButton();
buttonUp.setName("up"); buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon("images\\KeyUp.png")); buttonUp.setIcon(new ImageIcon("images\\KeyUp.png"));
buttonUp.setSize(48, 44);
buttonRight = new JButton(); buttonRight = new JButton();
buttonRight.setName("right"); buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon("images\\KeyRight.png")); buttonRight.setIcon(new ImageIcon("images\\KeyRight.png"));
buttonRight.setSize(48, 44);
buttonLeft = new JButton(); buttonLeft = new JButton();
buttonLeft.setName("left"); buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png")); buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png"));
buttonLeft.setSize(48, 44);
buttonDown = new JButton(); buttonDown = new JButton();
buttonDown.setName("down"); buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon("images\\KeyDown.png")); buttonDown.setIcon(new ImageIcon("images\\KeyDown.png"));
buttonDown.setSize(48, 44);
setSize(800,500); setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null); setLayout(null);
buttonCreate.setBounds(12, 355, 170, 44); buttonCreateAirbus.setBounds(12, 355, 146, 33);
buttonCreatePlane.setBounds(182, 355, 146, 33);
labelCount.setBounds(42, 405, 240, 20);
fieldCount.setBounds(240, 407, 48, 20);
labelStrategy.setBounds(630, 20, 146, 33);
comboBoxStrategy.setBounds(630, 50, 146, 20);
buttonStrategy.setBounds(630, 80, 146, 33);
buttonUp.setBounds(679, 313, 48, 44); buttonUp.setBounds(679, 313, 48, 44);
buttonRight.setBounds( 728, 358, 48, 44); buttonRight.setBounds( 728, 358, 48, 44);
buttonLeft.setBounds(630, 358, 48, 44); buttonLeft.setBounds(630, 358, 48, 44);
buttonDown.setBounds( 679, 358, 48, 44); buttonDown.setBounds( 679, 358, 48, 44);
labelCount.setBounds(12, 405, 240, 20); labelCount.setBounds(12, 405, 240, 20);
fieldCount.setBounds(210, 407, 48, 20); fieldCount.setBounds(210, 407, 48, 20);
canvas.setBounds(0,0,800, 460); canvas.setBounds(0,0,790, 460);
add(buttonCreate); add(buttonCreateAirbus);
add(buttonCreatePlane);
add(labelCount);
add(fieldCount);
add(labelStrategy);
add(comboBoxStrategy);
add(buttonStrategy);
add(buttonUp); add(buttonUp);
add(buttonRight); add(buttonRight);
add(buttonDown); add(buttonDown);
@ -70,14 +106,18 @@ public class FormAirbus extends JFrame {
add(fieldCount); add(fieldCount);
add(canvas); add(canvas);
buttonCreate.addActionListener(buttonCreateListener); // логика формы
buttonCreateAirbus.addActionListener(buttonCreateAirbusListener);
buttonCreatePlane.addActionListener(buttonCreatePlaneListener);
buttonStrategy.addActionListener(buttonStrategyListener);
buttonUp.addActionListener(buttonsMoveListener); buttonUp.addActionListener(buttonsMoveListener);
buttonRight.addActionListener(buttonsMoveListener); buttonRight.addActionListener(buttonsMoveListener);
buttonDown.addActionListener(buttonsMoveListener); buttonDown.addActionListener(buttonsMoveListener);
buttonLeft.addActionListener(buttonsMoveListener); buttonLeft.addActionListener(buttonsMoveListener);
} }
ActionListener buttonCreateListener = new ActionListener() { ActionListener buttonCreateAirbusListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int countPortholes; int countPortholes;
try try
@ -95,15 +135,86 @@ public class FormAirbus extends JFrame {
} }
Random rand = new Random(); Random rand = new Random();
_drawningAirbus = new DrawingAirbus(); _drawingAirbus = new DrawingAirbus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
_drawningAirbus.Init(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
rand.nextBoolean(), rand.nextBoolean(),
countPortholes, countPortholes,
canvas.getWidth(), canvas.getHeight()); canvas.getWidth(), canvas.getHeight());
_drawningAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); _drawingAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
comboBoxStrategy.setEnabled(true);
canvas.repaint();
}
};
ActionListener buttonCreatePlaneListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int countPortholes;
try
{
countPortholes = Integer.parseInt(fieldCount.getText());
}
catch (Exception ex)
{
countPortholes = 0;
}
if (countPortholes != 10 && countPortholes != 20 && countPortholes != 30)
{
JOptionPane.showMessageDialog(null, "Число должно быть равно 10, 20 или 30.\nКол-во иллюминаторов приравнено к 10");
countPortholes = 10;
}
Random rand = new Random();
_drawingAirbus = new DrawingPlane(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
countPortholes,
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
rand.nextBoolean(), rand.nextBoolean(),
canvas.getWidth(), canvas.getHeight());
_drawingAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
comboBoxStrategy.setEnabled(true);
canvas.repaint();
}
};
ActionListener buttonStrategyListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawingAirbus == 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 DrawingObjectAirbus(_drawingAirbus), canvas.getWidth(), canvas.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
canvas.repaint(); canvas.repaint();
} }
}; };
@ -111,23 +222,23 @@ public class FormAirbus extends JFrame {
ActionListener buttonsMoveListener = new ActionListener() { ActionListener buttonsMoveListener = new ActionListener() {
// реакция на нажатие // реакция на нажатие
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_drawningAirbus == null) if (_drawingAirbus == null)
{ {
return; return;
} }
String command = ((JButton)(e.getSource())).getName(); String command = ((JButton)(e.getSource())).getName();
switch (command) { switch (command) {
case "up": case "up":
_drawningAirbus.MoveTransport(Direction.Up); _drawingAirbus.MoveTransport(Direction.Up);
break; break;
case "down": case "down":
_drawningAirbus.MoveTransport(Direction.Down); _drawingAirbus.MoveTransport(Direction.Down);
break; break;
case "right": case "right":
_drawningAirbus.MoveTransport(Direction.Right); _drawingAirbus.MoveTransport(Direction.Right);
break; break;
case "left": case "left":
_drawningAirbus.MoveTransport(Direction.Left); _drawingAirbus.MoveTransport(Direction.Left);
break; break;
} }
canvas.repaint(); canvas.repaint();
@ -135,15 +246,14 @@ public class FormAirbus extends JFrame {
}; };
class Canvas extends JComponent{ class Canvas extends JComponent{
public Canvas(){ public Canvas() {}
}
public void paintComponent (Graphics g){ public void paintComponent (Graphics g){
if (_drawningAirbus == null){ if (_drawingAirbus == null){
return; return;
} }
super.paintComponents (g) ; super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
_drawningAirbus.DrawTransport(g2d); _drawingAirbus.DrawTransport(g2d);
super.repaint(); super.repaint();
} }
} }

View File

@ -1,4 +1,5 @@
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
new FormAirbus(); new FormAirbus();
} }

View File

@ -0,0 +1,76 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private Status _state = Status.NotInit;
protected int FieldWidth;
protected int 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;
_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() { return _moveableObject.GetObjectPosition(); }
// шаг
protected int GetStep()
{
if (_state != Status.InProgress)
{
return 0;
}
return _moveableObject.GetStep();
}
// перемещение
protected abstract void MoveToTarget();
// достигнута ли цель
protected abstract boolean IsTargetDestination();
// попытка перемещения по направлению
private boolean MoveTo(Direction directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject.CheckCanMove(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,24 @@
package MovementStrategy;
import Drawnings.*;
public class DrawingObjectAirbus implements IMoveableObject {
private DrawingAirbus _drawingAirbus = null;
public DrawingObjectAirbus(DrawingAirbus drawingAirbus)
{
_drawingAirbus = drawingAirbus;
}
public ObjectParameters GetObjectPosition()
{
if (_drawingAirbus == null || _drawingAirbus.entityAirbus == null)
{
return null;
}
return new ObjectParameters(_drawingAirbus.GetPosX(), _drawingAirbus.GetPosY(), _drawingAirbus.GetWidth(), _drawingAirbus.GetHeight());
}
public int GetStep() { return (int)_drawingAirbus.entityAirbus.Step; }
public boolean CheckCanMove(Direction direction) { return _drawingAirbus.CanMove(direction); }
public void MoveObject(Direction direction) { _drawingAirbus.MoveTransport(direction); }
}

View File

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

View File

@ -0,0 +1,44 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.RightBorder() + GetStep() >= FieldWidth && objParams.DownBorder() + GetStep() >= FieldHeight;
}
// движение к цели
@Override
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder() - FieldWidth;
var diffY = objParams.DownBorder() - FieldHeight;
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,53 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2;
}
// движение к цели
@Override
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,25 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
public int LeftBorder() { return _x; }
public int TopBorder() { return _y; }
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,7 @@
package MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}