Лабораторная работа №2
This commit is contained in:
parent
471fd18960
commit
8945d1c2cc
@ -2,5 +2,6 @@
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -1,6 +0,0 @@
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
@ -1,223 +0,0 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
public class DrawningAirFighter {
|
||||
private EntityAirFighter entityAirFighter;
|
||||
public EntityAirFighter getEntityAirFighter() {
|
||||
return entityAirFighter;
|
||||
}
|
||||
private Integer _pictureWidth;
|
||||
private Integer _pictureHeight;
|
||||
private Integer _startPosX;
|
||||
private Integer _startPosY;
|
||||
private final int _drawingAirFighterWidth = 76;
|
||||
private final int _drawingAirFighterHeight = 80;
|
||||
public DrawningAirFighterEngine _drawningAirFighterEngine;
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean pgo, boolean rockets) {
|
||||
entityAirFighter = new EntityAirFighter();
|
||||
entityAirFighter.Init(speed, weight, bodyColor, additionalColor, pgo, rockets);
|
||||
_startPosY = null;
|
||||
_startPosX = null;
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
|
||||
_drawningAirFighterEngine = new DrawningAirFighterEngine();
|
||||
Random random = new Random();
|
||||
int[] countengine = {2, 4, 6};
|
||||
int engineCount = countengine[random.nextInt(countengine.length)];
|
||||
_drawningAirFighterEngine.setEnumNumber(engineCount);
|
||||
|
||||
}
|
||||
public void setPosition(int x, int y) {
|
||||
if (_pictureHeight == null || _pictureWidth == null)
|
||||
return;
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
|
||||
if (_drawingAirFighterWidth + x > _pictureWidth || x < 0) {
|
||||
_startPosX = 0;
|
||||
}
|
||||
if (_drawingAirFighterHeight + y > _pictureHeight || y < 0) {
|
||||
_startPosY = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean setPictureSize(int width, int height) {
|
||||
|
||||
if (_drawingAirFighterHeight > height || _drawingAirFighterWidth > width)
|
||||
return false;
|
||||
_pictureHeight = height;
|
||||
_pictureWidth = width;
|
||||
|
||||
if (_startPosX != null && _startPosY != null) {
|
||||
if (_startPosX + _drawingAirFighterWidth > width)
|
||||
_startPosX = width - _drawingAirFighterWidth;
|
||||
if (_startPosY + _drawingAirFighterHeight > height)
|
||||
_startPosY = height - _drawingAirFighterHeight;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean moveTransport(DirectionType direction) {
|
||||
if (entityAirFighter == null || _pictureWidth == null || _pictureHeight == null)
|
||||
return false;
|
||||
switch (direction) {
|
||||
case Left:
|
||||
if (_startPosX - entityAirFighter.Step() > 0)
|
||||
_startPosX -= (int) entityAirFighter.Step();
|
||||
return true;
|
||||
case Up:
|
||||
if (_startPosY - entityAirFighter.Step() > 0)
|
||||
_startPosY -= (int) entityAirFighter.Step();
|
||||
return true;
|
||||
case Right:
|
||||
if (_startPosX + entityAirFighter.Step() < _pictureWidth - _drawingAirFighterWidth)
|
||||
_startPosX += (int) entityAirFighter.Step();
|
||||
return true;
|
||||
case Down:
|
||||
if (_startPosY + entityAirFighter.Step() < _pictureHeight - _drawingAirFighterHeight)
|
||||
_startPosY += (int) entityAirFighter.Step();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void drawAirFighter(Graphics g) {
|
||||
if (entityAirFighter == null || _startPosX == null || _startPosY == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
Point[] rtail = new Point[]{
|
||||
new Point (_startPosX + 10,_startPosY + 25),
|
||||
new Point (_startPosX + 10,_startPosY + 8),
|
||||
new Point (_startPosX + 20,_startPosY + 17),
|
||||
new Point (_startPosX + 20, _startPosY + 25),
|
||||
};
|
||||
Polygon rtailPolygon = new Polygon();
|
||||
for (Point point : rtail)
|
||||
rtailPolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getBodyColor());
|
||||
g2d.fillPolygon(rtailPolygon);
|
||||
|
||||
Point[] ltail = new Point[]{
|
||||
new Point (_startPosX + 10,_startPosY + 49),
|
||||
new Point (_startPosX + 10,_startPosY + 66),
|
||||
new Point (_startPosX + 20,_startPosY + 57),
|
||||
new Point (_startPosX + 20, _startPosY + 49),
|
||||
};
|
||||
Polygon ltailPolygon = new Polygon();
|
||||
for (Point point : ltail)
|
||||
ltailPolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getBodyColor());
|
||||
g2d.fillPolygon(ltailPolygon);
|
||||
|
||||
Point[] rwing = new Point[]{
|
||||
new Point (_startPosX + 45,_startPosY + 49),
|
||||
new Point (_startPosX + 45,_startPosY + 74),
|
||||
new Point (_startPosX + 37,_startPosY + 74),
|
||||
new Point (_startPosX + 30, _startPosY + 49),
|
||||
};
|
||||
Polygon rwingPolygon = new Polygon();
|
||||
for (Point point : rwing)
|
||||
rwingPolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getBodyColor());
|
||||
g2d.fillPolygon(rwingPolygon);
|
||||
|
||||
Point[] lwing = new Point[]{
|
||||
new Point (_startPosX + 45,_startPosY + 25),
|
||||
new Point (_startPosX + 45,_startPosY ),
|
||||
new Point (_startPosX + 37,_startPosY ),
|
||||
new Point (_startPosX + 30, _startPosY + 25),
|
||||
};
|
||||
Polygon lwingPolygon = new Polygon();
|
||||
for (Point point : lwing)
|
||||
lwingPolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getBodyColor());
|
||||
g2d.fillPolygon(lwingPolygon);
|
||||
|
||||
g2d.drawRect(_startPosX + 10, _startPosY+ 25, 50, 24);
|
||||
g2d.fillRect(_startPosX + 10, _startPosY+ 25, 50, 24);
|
||||
|
||||
Point[] nose = new Point[]{
|
||||
new Point (_startPosX + 60,_startPosY + 25),
|
||||
new Point (_startPosX + 76,_startPosY + 37),
|
||||
new Point (_startPosX + 60,_startPosY + 49),
|
||||
};
|
||||
Polygon nosePolygon = new Polygon();
|
||||
for (Point point : nose)
|
||||
nosePolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(Color.GRAY);
|
||||
g2d.fillPolygon(nosePolygon);
|
||||
|
||||
if (entityAirFighter.getPgo()) {
|
||||
Point[] pgo1 = new Point[]{
|
||||
new Point ( _startPosX + 50, _startPosY + 25),
|
||||
new Point ( _startPosX + 50, _startPosY + 15),
|
||||
new Point ( _startPosX + 55, _startPosY + 25),
|
||||
};
|
||||
Polygon pgo1Polygon = new Polygon();
|
||||
for (Point point : pgo1)
|
||||
pgo1Polygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getAdditionalColor());
|
||||
g2d.fillPolygon(pgo1Polygon);
|
||||
|
||||
Point[] pgo2 = new Point[]{
|
||||
new Point (_startPosX + 50, _startPosY + 49),
|
||||
new Point (_startPosX + 50, _startPosY + 59),
|
||||
new Point (_startPosX + 55, _startPosY + 49),
|
||||
};
|
||||
Polygon pgo2Polygon = new Polygon();
|
||||
for (Point point : pgo2)
|
||||
pgo2Polygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getAdditionalColor());
|
||||
g2d.fillPolygon(pgo2Polygon);
|
||||
}
|
||||
|
||||
if (entityAirFighter.getRockets()) {
|
||||
Point[] rocket1 = new Point[]{
|
||||
new Point (_startPosX + 50, _startPosY + 71),
|
||||
new Point (_startPosX + 50, _startPosY+ 74),
|
||||
new Point (_startPosX + 55, _startPosY + 69),
|
||||
new Point (_startPosX + 50, _startPosY + 64),
|
||||
};
|
||||
Polygon rocket1Polygon = new Polygon();
|
||||
for (Point point : rocket1)
|
||||
rocket1Polygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getAdditionalColor());
|
||||
g2d.fillPolygon(rocket1Polygon);
|
||||
|
||||
g.drawRect(_startPosX + 45, _startPosY + 67, 5, 4);
|
||||
g.fillRect(_startPosX + 45, _startPosY + 67, 5, 4);
|
||||
|
||||
Point[] rocket2 = new Point[]{
|
||||
new Point (_startPosX + 50, _startPosY + 7),
|
||||
new Point (_startPosX + 50, _startPosY+ 10),
|
||||
new Point (_startPosX + 55, _startPosY + 5),
|
||||
new Point (_startPosX + 50, _startPosY),
|
||||
};
|
||||
Polygon rocket2Polygon = new Polygon();
|
||||
for (Point point : rocket2)
|
||||
rocket2Polygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getAdditionalColor());
|
||||
g2d.fillPolygon(rocket2Polygon);
|
||||
|
||||
g.drawRect(_startPosX + 45, _startPosY + 3, 5, 4);
|
||||
g.fillRect(_startPosX + 45, _startPosY + 3, 5, 4);
|
||||
}
|
||||
|
||||
_drawningAirFighterEngine.drawAirFighterEngine(g, entityAirFighter.getAdditionalColor(), _startPosX, _startPosY);
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningAirFighterEngine {
|
||||
private EngineCount _engineCount;
|
||||
public void setEnumNumber(int engineCount) {
|
||||
for (EngineCount value : EngineCount.values()) {
|
||||
if (value.getEnumNumber() == engineCount) {
|
||||
_engineCount = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void drawAirFighterEngine(Graphics g, Color color, float startPosX, float startPosY) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setColor(color);
|
||||
g2d.setStroke(new BasicStroke(4));
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 2){
|
||||
g2d.drawRect((int)startPosX + 30, (int) startPosY + 69, 10, 2 );
|
||||
g2d.drawRect((int)startPosX + 30, (int) startPosY + 3, 10, 2 );
|
||||
}
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 4){
|
||||
g2d.drawRect((int)startPosX + 30, (int) startPosY + 62, 10, 2 );
|
||||
g2d.drawRect((int)startPosX + 30, (int) startPosY + 10, 10, 2 );
|
||||
}
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 6){
|
||||
g2d.drawRect((int)startPosX + 30, (int) startPosY + 55, 10, 2 );
|
||||
g2d.drawRect((int)startPosX + 30, (int) startPosY + 17, 10, 2 );
|
||||
}
|
||||
}
|
||||
}
|
203
ProjectAirFighter/src/Drawnings/DrawningAirCraft.java
Normal file
203
ProjectAirFighter/src/Drawnings/DrawningAirCraft.java
Normal file
@ -0,0 +1,203 @@
|
||||
package Drawnings;
|
||||
|
||||
import Entities.EntityAirCraft;
|
||||
import MovementStrategy.MovementDirection;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningAirCraft {
|
||||
private final EntityAirCraft entityAirCraft;
|
||||
|
||||
public EntityAirCraft getEntityAirCraft() {
|
||||
return entityAirCraft;
|
||||
}
|
||||
|
||||
private Integer _pictureWidth;
|
||||
private Integer _pictureHeight;
|
||||
protected Integer _startPosX;
|
||||
protected Integer _startPosY;
|
||||
private int _drawingAirCraftWidth = 66;
|
||||
private int _drawingAirCraftHeight = 74;
|
||||
|
||||
public int GetPosX() {
|
||||
return _startPosX;
|
||||
}
|
||||
|
||||
public int GetPosY() {
|
||||
return _startPosY;
|
||||
}
|
||||
|
||||
public int GetWidth() {
|
||||
return _drawingAirCraftWidth;
|
||||
}
|
||||
|
||||
public int GetHeight() {
|
||||
return _drawingAirCraftHeight;
|
||||
}
|
||||
|
||||
private IDrawEngine drawEngine;
|
||||
|
||||
public DrawningAirCraft(int speed, float weight, Color bodyColor, int engineType) {
|
||||
entityAirCraft = new EntityAirCraft(speed, weight, bodyColor);
|
||||
_startPosY = null;
|
||||
_startPosX = null;
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
switch (engineType) {
|
||||
case 0:
|
||||
drawEngine = new DrawningEngine();
|
||||
break;
|
||||
case 1:
|
||||
drawEngine = new DrawningOvalEngine();
|
||||
break;
|
||||
case 2:
|
||||
drawEngine = new DrawningLongEngine();
|
||||
break;
|
||||
}
|
||||
|
||||
Random random = new Random();
|
||||
int[] countengine = {2, 4, 6};
|
||||
int engineCount = countengine[random.nextInt(countengine.length)];
|
||||
System.out.print(engineCount);
|
||||
drawEngine.setNumber(engineCount);
|
||||
}
|
||||
|
||||
protected DrawningAirCraft(int speed, float weight, Color bodyColor, int EngineType, int aircraftWidth, int aircraftHeight) {
|
||||
this(speed, weight, bodyColor, EngineType);
|
||||
_drawingAirCraftHeight = aircraftHeight;
|
||||
_drawingAirCraftWidth = aircraftWidth;
|
||||
}
|
||||
|
||||
public void setPosition(int x, int y) {
|
||||
if (_pictureHeight == null || _pictureWidth == null)
|
||||
return;
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
|
||||
if (_drawingAirCraftWidth + x > _pictureWidth || x < 0) {
|
||||
_startPosX = 0;
|
||||
}
|
||||
if (_drawingAirCraftHeight + y > _pictureHeight || y < 0) {
|
||||
_startPosY = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean setPictureSize(int width, int height) {
|
||||
|
||||
if (_drawingAirCraftHeight > height || _drawingAirCraftWidth > width)
|
||||
return false;
|
||||
_pictureHeight = height;
|
||||
_pictureWidth = width;
|
||||
|
||||
if (_startPosX != null && _startPosY != null) {
|
||||
if (_startPosX + _drawingAirCraftWidth > width)
|
||||
_startPosX = width - _drawingAirCraftWidth;
|
||||
if (_startPosY + _drawingAirCraftHeight > height)
|
||||
_startPosY = height - _drawingAirCraftHeight;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean moveTransport(MovementDirection direction) {
|
||||
if (entityAirCraft == null || _pictureWidth == null || _pictureHeight == null)
|
||||
return false;
|
||||
switch (direction) {
|
||||
case MovementDirection.Left:
|
||||
if (_startPosX - entityAirCraft.getStep() > 0)
|
||||
_startPosX -= (int) entityAirCraft.getStep();
|
||||
return true;
|
||||
case MovementDirection.Up:
|
||||
if (_startPosY - entityAirCraft.getStep() > 0)
|
||||
_startPosY -= (int) entityAirCraft.getStep();
|
||||
return true;
|
||||
case MovementDirection.Right:
|
||||
if (_startPosX + entityAirCraft.getStep() < _pictureWidth - _drawingAirCraftWidth)
|
||||
_startPosX += (int) entityAirCraft.getStep();
|
||||
return true;
|
||||
case MovementDirection.Down:
|
||||
if (_startPosY + entityAirCraft.getStep() < _pictureHeight - _drawingAirCraftHeight)
|
||||
_startPosY += (int) entityAirCraft.getStep();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void drawAirCraft(Graphics g) {
|
||||
if (entityAirCraft == null || _startPosX == null || _startPosY == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
Point[] rtail = new Point[]{
|
||||
new Point(_startPosX + 10, _startPosY + 25),
|
||||
new Point(_startPosX + 10, _startPosY + 8),
|
||||
new Point(_startPosX + 20, _startPosY + 17),
|
||||
new Point(_startPosX + 20, _startPosY + 25),
|
||||
};
|
||||
Polygon rtailPolygon = new Polygon();
|
||||
for (Point point : rtail)
|
||||
rtailPolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirCraft.getBodyColor());
|
||||
g2d.fillPolygon(rtailPolygon);
|
||||
|
||||
Point[] ltail = new Point[]{
|
||||
new Point(_startPosX + 10, _startPosY + 49),
|
||||
new Point(_startPosX + 10, _startPosY + 66),
|
||||
new Point(_startPosX + 20, _startPosY + 57),
|
||||
new Point(_startPosX + 20, _startPosY + 49),
|
||||
};
|
||||
Polygon ltailPolygon = new Polygon();
|
||||
for (Point point : ltail)
|
||||
ltailPolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirCraft.getBodyColor());
|
||||
g2d.fillPolygon(ltailPolygon);
|
||||
|
||||
Point[] rwing = new Point[]{
|
||||
new Point(_startPosX + 45, _startPosY + 49),
|
||||
new Point(_startPosX + 45, _startPosY + 74),
|
||||
new Point(_startPosX + 37, _startPosY + 74),
|
||||
new Point(_startPosX + 30, _startPosY + 49),
|
||||
};
|
||||
Polygon rwingPolygon = new Polygon();
|
||||
for (Point point : rwing)
|
||||
rwingPolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirCraft.getBodyColor());
|
||||
g2d.fillPolygon(rwingPolygon);
|
||||
|
||||
Point[] lwing = new Point[]{
|
||||
new Point(_startPosX + 45, _startPosY + 25),
|
||||
new Point(_startPosX + 45, _startPosY),
|
||||
new Point(_startPosX + 37, _startPosY),
|
||||
new Point(_startPosX + 30, _startPosY + 25),
|
||||
};
|
||||
Polygon lwingPolygon = new Polygon();
|
||||
for (Point point : lwing)
|
||||
lwingPolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirCraft.getBodyColor());
|
||||
g2d.fillPolygon(lwingPolygon);
|
||||
|
||||
g2d.drawRect(_startPosX + 10, _startPosY + 25, 50, 24);
|
||||
g2d.fillRect(_startPosX + 10, _startPosY + 25, 50, 24);
|
||||
|
||||
Point[] nose = new Point[]{
|
||||
new Point(_startPosX + 60, _startPosY + 25),
|
||||
new Point(_startPosX + 76, _startPosY + 37),
|
||||
new Point(_startPosX + 60, _startPosY + 49),
|
||||
};
|
||||
Polygon nosePolygon = new Polygon();
|
||||
for (Point point : nose)
|
||||
nosePolygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(Color.GRAY);
|
||||
g2d.fillPolygon(nosePolygon);
|
||||
|
||||
drawEngine.drawEngine(g2d, entityAirCraft.getBodyColor(), _startPosX, _startPosY);
|
||||
}
|
||||
}
|
82
ProjectAirFighter/src/Drawnings/DrawningAirFighter.java
Normal file
82
ProjectAirFighter/src/Drawnings/DrawningAirFighter.java
Normal file
@ -0,0 +1,82 @@
|
||||
package Drawnings;
|
||||
|
||||
import Entities.EntityAirFighter;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningAirFighter extends DrawningAirCraft {
|
||||
private EntityAirFighter entityAirFighter;
|
||||
|
||||
public DrawningAirFighter(int speed, float weight, Color bodyColor, int engineType, Color additionalColor, boolean pgo, boolean rockets) {
|
||||
super(speed, weight, bodyColor, engineType, 76, 80);
|
||||
entityAirFighter = new EntityAirFighter(speed, weight, bodyColor, additionalColor, pgo, rockets);
|
||||
}
|
||||
|
||||
public void drawAirCraft(Graphics g) {
|
||||
if (entityAirFighter == null || _startPosX == null || _startPosY == null) {
|
||||
return;
|
||||
}
|
||||
super.drawAirCraft(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
if (entityAirFighter.getPgo()) {
|
||||
Point[] pgo1 = new Point[]{
|
||||
new Point(_startPosX + 50, _startPosY + 25),
|
||||
new Point(_startPosX + 50, _startPosY + 15),
|
||||
new Point(_startPosX + 55, _startPosY + 25),
|
||||
};
|
||||
Polygon pgo1Polygon = new Polygon();
|
||||
for (Point point : pgo1)
|
||||
pgo1Polygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getAdditionalColor());
|
||||
g2d.fillPolygon(pgo1Polygon);
|
||||
|
||||
Point[] pgo2 = new Point[]{
|
||||
new Point(_startPosX + 50, _startPosY + 49),
|
||||
new Point(_startPosX + 50, _startPosY + 59),
|
||||
new Point(_startPosX + 55, _startPosY + 49),
|
||||
};
|
||||
Polygon pgo2Polygon = new Polygon();
|
||||
for (Point point : pgo2)
|
||||
pgo2Polygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getAdditionalColor());
|
||||
g2d.fillPolygon(pgo2Polygon);
|
||||
}
|
||||
|
||||
if (entityAirFighter.getRockets()) {
|
||||
Point[] rocket1 = new Point[]{
|
||||
new Point(_startPosX + 50, _startPosY + 71),
|
||||
new Point(_startPosX + 50, _startPosY + 74),
|
||||
new Point(_startPosX + 55, _startPosY + 69),
|
||||
new Point(_startPosX + 50, _startPosY + 64),
|
||||
};
|
||||
Polygon rocket1Polygon = new Polygon();
|
||||
for (Point point : rocket1)
|
||||
rocket1Polygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getAdditionalColor());
|
||||
g2d.fillPolygon(rocket1Polygon);
|
||||
|
||||
g.drawRect(_startPosX + 45, _startPosY + 67, 5, 4);
|
||||
g.fillRect(_startPosX + 45, _startPosY + 67, 5, 4);
|
||||
|
||||
Point[] rocket2 = new Point[]{
|
||||
new Point(_startPosX + 50, _startPosY + 7),
|
||||
new Point(_startPosX + 50, _startPosY + 10),
|
||||
new Point(_startPosX + 55, _startPosY + 5),
|
||||
new Point(_startPosX + 50, _startPosY),
|
||||
};
|
||||
Polygon rocket2Polygon = new Polygon();
|
||||
for (Point point : rocket2)
|
||||
rocket2Polygon.addPoint(point.x, point.y);
|
||||
|
||||
g2d.setColor(entityAirFighter.getAdditionalColor());
|
||||
g2d.fillPolygon(rocket2Polygon);
|
||||
|
||||
g.drawRect(_startPosX + 45, _startPosY + 3, 5, 4);
|
||||
g.fillRect(_startPosX + 45, _startPosY + 3, 5, 4);
|
||||
}
|
||||
}
|
||||
}
|
38
ProjectAirFighter/src/Drawnings/DrawningEngine.java
Normal file
38
ProjectAirFighter/src/Drawnings/DrawningEngine.java
Normal file
@ -0,0 +1,38 @@
|
||||
package Drawnings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningEngine implements IDrawEngine {
|
||||
private EngineCount _engineCount;
|
||||
|
||||
@Override
|
||||
public void setNumber(int engineCount) {
|
||||
for (EngineCount value : EngineCount.values()) {
|
||||
if (value.getEnumNumber() == engineCount) {
|
||||
_engineCount = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawEngine(Graphics2D g2d, Color color, int _startX, int _startY) {
|
||||
g2d.setColor(color);
|
||||
g2d.setStroke(new BasicStroke(4));
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 2){
|
||||
g2d.drawRect(_startX + 30, _startY + 69, 10, 2 );
|
||||
g2d.drawRect(_startX + 30, _startY + 3, 10, 2 );
|
||||
}
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 4){
|
||||
g2d.drawRect(_startX + 30, _startY + 62, 10, 2 );
|
||||
g2d.drawRect(_startX + 30, _startY + 10, 10, 2 );
|
||||
}
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 6){
|
||||
g2d.drawRect(_startX + 30, _startY + 55, 10, 2 );
|
||||
g2d.drawRect(_startX + 30, _startY + 17, 10, 2 );
|
||||
}
|
||||
}
|
||||
}
|
38
ProjectAirFighter/src/Drawnings/DrawningLongEngine.java
Normal file
38
ProjectAirFighter/src/Drawnings/DrawningLongEngine.java
Normal file
@ -0,0 +1,38 @@
|
||||
package Drawnings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningLongEngine implements IDrawEngine {
|
||||
private EngineCount _engineCount;
|
||||
|
||||
@Override
|
||||
public void setNumber(int engineCount) {
|
||||
for (EngineCount value : EngineCount.values()) {
|
||||
if (value.getEnumNumber() == engineCount) {
|
||||
_engineCount = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawEngine(Graphics2D g2d, Color color, int _startX, int _startY) {
|
||||
g2d.setColor(color);
|
||||
g2d.setStroke(new BasicStroke(4));
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 2) {
|
||||
g2d.drawRoundRect(_startX + 25, _startY + 69, 15, 2, 5, 5);
|
||||
g2d.drawRoundRect(_startX + 25, _startY + 3, 15, 2, 5, 5);
|
||||
}
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 4) {
|
||||
g2d.drawRoundRect(_startX + 25, _startY + 62, 15, 2, 5, 5);
|
||||
g2d.drawRoundRect(_startX + 25, _startY + 10, 15, 2, 5, 5);
|
||||
}
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 6) {
|
||||
g2d.drawRoundRect(_startX + 25, _startY + 55, 15, 2, 5, 5);
|
||||
g2d.drawRoundRect(_startX + 25, _startY + 17, 15, 2, 5, 5);
|
||||
}
|
||||
}
|
||||
}
|
38
ProjectAirFighter/src/Drawnings/DrawningOvalEngine.java
Normal file
38
ProjectAirFighter/src/Drawnings/DrawningOvalEngine.java
Normal file
@ -0,0 +1,38 @@
|
||||
package Drawnings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningOvalEngine implements IDrawEngine {
|
||||
private EngineCount _engineCount;
|
||||
|
||||
@Override
|
||||
public void setNumber(int engineCount) {
|
||||
for (EngineCount value : EngineCount.values()) {
|
||||
if (value.getEnumNumber() == engineCount) {
|
||||
_engineCount = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawEngine(Graphics2D g2d, Color color, int _startX, int _startY) {
|
||||
g2d.setColor(color);
|
||||
g2d.setStroke(new BasicStroke(4));
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 2) {
|
||||
g2d.drawOval(_startX + 30, _startY + 69, 10, 2);
|
||||
g2d.drawOval(_startX + 30, _startY + 3, 10, 2);
|
||||
}
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 4) {
|
||||
g2d.drawOval(_startX + 30, _startY + 62, 10, 2);
|
||||
g2d.drawOval(_startX + 30, _startY + 10, 10, 2);
|
||||
}
|
||||
|
||||
if (_engineCount.getEnumNumber() >= 6) {
|
||||
g2d.drawOval(_startX + 30, _startY + 55, 10, 2);
|
||||
g2d.drawOval(_startX + 30, _startY + 17, 10, 2);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package Drawnings;
|
||||
|
||||
public enum EngineCount {
|
||||
Two(2),
|
||||
Three(4),
|
||||
@ -9,5 +11,4 @@ public enum EngineCount {
|
||||
public int getEnumNumber() {
|
||||
return EnumNumber;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
8
ProjectAirFighter/src/Drawnings/IDrawEngine.java
Normal file
8
ProjectAirFighter/src/Drawnings/IDrawEngine.java
Normal file
@ -0,0 +1,8 @@
|
||||
package Drawnings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawEngine {
|
||||
void setNumber(int x);
|
||||
void drawEngine(Graphics2D graphics2D, Color color, int _startX, int _startY);
|
||||
}
|
28
ProjectAirFighter/src/Entities/EntityAirCraft.java
Normal file
28
ProjectAirFighter/src/Entities/EntityAirCraft.java
Normal file
@ -0,0 +1,28 @@
|
||||
package Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
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;
|
||||
}
|
||||
private double Step;
|
||||
public double getStep() {
|
||||
return Speed*100/Weight;
|
||||
}
|
||||
|
||||
public EntityAirCraft(int speed, double weight, Color bodyColor) {
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
24
ProjectAirFighter/src/Entities/EntityAirFighter.java
Normal file
24
ProjectAirFighter/src/Entities/EntityAirFighter.java
Normal file
@ -0,0 +1,24 @@
|
||||
package Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityAirFighter extends EntityAirCraft {
|
||||
private Color AdditionalColor;
|
||||
public Color getAdditionalColor() {
|
||||
return AdditionalColor;
|
||||
}
|
||||
private boolean Pgo;
|
||||
public boolean getPgo() {
|
||||
return Pgo;
|
||||
}
|
||||
private boolean Rockets;
|
||||
public boolean getRockets() {
|
||||
return Rockets;
|
||||
}
|
||||
public EntityAirFighter(int speed, double weight, Color bodyColor, Color additionalColor, boolean pgo, boolean rockets) {
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor = additionalColor;
|
||||
Pgo = pgo;
|
||||
Rockets = rockets;
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityAirFighter {
|
||||
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;
|
||||
public Color getAdditionalColor() {
|
||||
return AdditionalColor;
|
||||
}
|
||||
public double Step() {
|
||||
return Speed*100/Weight;
|
||||
}
|
||||
private boolean Pgo;
|
||||
public boolean getPgo() {
|
||||
return Pgo;
|
||||
}
|
||||
private boolean Rockets;
|
||||
public boolean getRockets() {
|
||||
return Rockets;
|
||||
}
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean pgo, boolean rockets) {
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Pgo = pgo;
|
||||
Rockets = rockets;
|
||||
}
|
||||
}
|
@ -8,61 +8,55 @@
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="1ff9a" binding="PictureBox" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="1ff9a" binding="PictureBox" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<properties>
|
||||
<toolTipText value=""/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="a8c2" class="javax.swing.JButton" binding="buttonCreate">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Создать"/>
|
||||
</properties>
|
||||
</component>
|
||||
<vspacer id="7b40c">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
<grid row="2" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="88" height="14"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="3dc0" class="javax.swing.JButton" binding="buttonDown">
|
||||
<hspacer id="e0b21">
|
||||
<constraints>
|
||||
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="arrowDown.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="abaeb">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<component id="ccb2a" class="javax.swing.JButton" binding="buttonUp">
|
||||
<component id="a8c2" class="javax.swing.JButton" binding="buttonCreateAirFighter">
|
||||
<constraints>
|
||||
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="150" height="30"/>
|
||||
<preferred-size width="150" height="30"/>
|
||||
<maximum-size width="150" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="arrowUp.png"/>
|
||||
<text value=""/>
|
||||
<text value="Создать истребитель"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="79b3e" class="javax.swing.JButton" binding="buttonCreateAirCraft">
|
||||
<constraints>
|
||||
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="150" height="30"/>
|
||||
<preferred-size width="150" height="30"/>
|
||||
<maximum-size width="150" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Создать самолёт"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="fe25b" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<grid row="5" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
@ -73,9 +67,17 @@
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="3eabd" class="javax.swing.JButton" binding="buttonStrategyStep">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Шаг"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="86d84" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<grid row="5" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
@ -86,6 +88,42 @@
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="3dc0" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="5" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="arrowDown.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="ccb2a" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="arrowUp.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="988c8" class="javax.swing.JComboBox" binding="comboBoxStrategy">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="9" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<actionCommand value=""/>
|
||||
<doubleBuffered value="false"/>
|
||||
<toolTipText value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
|
@ -1,3 +1,7 @@
|
||||
import Drawnings.DrawningAirCraft;
|
||||
import Drawnings.DrawningAirFighter;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
@ -7,16 +11,45 @@ import java.util.Random;
|
||||
import java.util.List;
|
||||
|
||||
public class FormAirFighter extends JFrame {
|
||||
protected DrawningAirFighter _drawningAirFighter = new DrawningAirFighter();
|
||||
protected DrawningAirCraft _drawningAirCraft;
|
||||
JPanel PanelWrapper;
|
||||
private JPanel PictureBox;
|
||||
private JButton buttonCreate;
|
||||
private JButton buttonCreateAirFighter;
|
||||
private JButton buttonCreateAirCraft;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonUp;
|
||||
|
||||
private JComboBox comboBoxStrategy;
|
||||
private JButton buttonStrategyStep;
|
||||
private AbstractStrategy _strategy;
|
||||
private List<JComponent> controls;
|
||||
|
||||
private void createObject(String type) {
|
||||
Random random = new Random();
|
||||
switch (type) {
|
||||
case "Drawnings.DrawningAirCraft":
|
||||
_drawningAirCraft = new DrawningAirCraft(random.nextInt(30,100), random.nextInt(100,500),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3));
|
||||
break;
|
||||
case "Drawnings.DrawningAirFighter":
|
||||
_drawningAirCraft = new DrawningAirFighter(random.nextInt(30,100), random.nextInt(100,500),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||
random.nextBoolean(), random.nextBoolean());
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
_drawningAirCraft.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
|
||||
_drawningAirCraft.setPosition(random.nextInt(25, 100),
|
||||
random.nextInt(25, 100));
|
||||
_strategy = null;
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
|
||||
Draw();
|
||||
}
|
||||
|
||||
public FormAirFighter() {
|
||||
buttonUp.setName("buttonUp");
|
||||
buttonDown.setName("buttonDown");
|
||||
@ -25,25 +58,19 @@ public class FormAirFighter extends JFrame {
|
||||
|
||||
InitializeControlsRepaintList();
|
||||
|
||||
buttonCreate.addActionListener(new ActionListener() {
|
||||
buttonCreateAirFighter.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
_drawningAirFighter = new DrawningAirFighter();
|
||||
Random random = new Random();
|
||||
|
||||
_drawningAirFighter.Init(random.nextInt(30, 100),
|
||||
random.nextInt(100, 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() );
|
||||
_drawningAirFighter.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
|
||||
_drawningAirFighter.setPosition(random.nextInt(25, 100),
|
||||
random.nextInt(25, 100));
|
||||
|
||||
Draw();
|
||||
createObject("Drawnings.DrawningAirFighter");
|
||||
|
||||
}
|
||||
});
|
||||
buttonCreateAirCraft.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
createObject("Drawnings.DrawningAirCraft");
|
||||
}
|
||||
});
|
||||
ActionListener buttonMoveClickedListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@ -52,36 +79,72 @@ public class FormAirFighter extends JFrame {
|
||||
|
||||
switch (buttonName) {
|
||||
case "buttonUp": {
|
||||
result = _drawningAirFighter.moveTransport(DirectionType.Up);
|
||||
result = _drawningAirCraft.moveTransport(MovementDirection.Up);
|
||||
}
|
||||
break;
|
||||
case "buttonDown": {
|
||||
result = _drawningAirFighter.moveTransport(DirectionType.Down);
|
||||
result = _drawningAirCraft.moveTransport(MovementDirection.Down);
|
||||
}
|
||||
break;
|
||||
case "buttonLeft": {
|
||||
result = _drawningAirFighter.moveTransport(DirectionType.Left);
|
||||
result = _drawningAirCraft.moveTransport(MovementDirection.Left);
|
||||
}
|
||||
break;
|
||||
case "buttonRight": {
|
||||
result = _drawningAirFighter.moveTransport(DirectionType.Right);
|
||||
result = _drawningAirCraft.moveTransport(MovementDirection.Right);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
if (result)
|
||||
Draw();
|
||||
|
||||
}
|
||||
};
|
||||
buttonRight.addActionListener(buttonMoveClickedListener);
|
||||
buttonDown.addActionListener(buttonMoveClickedListener);
|
||||
buttonLeft.addActionListener(buttonMoveClickedListener);
|
||||
buttonUp.addActionListener(buttonMoveClickedListener);
|
||||
comboBoxStrategy.addItem("К центру");
|
||||
comboBoxStrategy.addItem("К краю");
|
||||
buttonStrategyStep.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_drawningAirCraft == null)
|
||||
return;
|
||||
if (comboBoxStrategy.isEnabled()) {
|
||||
switch (comboBoxStrategy.getSelectedIndex()) {
|
||||
case 0:
|
||||
_strategy = new MoveToCenter();
|
||||
break;
|
||||
case 1:
|
||||
_strategy = new MoveToBorder();
|
||||
break;
|
||||
|
||||
default:
|
||||
_strategy = null;
|
||||
break;
|
||||
|
||||
}
|
||||
if (_strategy == null) {
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableAirCraft(_drawningAirCraft), PictureBox.getWidth(), PictureBox.getHeight());
|
||||
}
|
||||
if (_strategy == null) {
|
||||
return;
|
||||
}
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish) {
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
private void Draw() {
|
||||
if (_drawningAirFighter.getEntityAirFighter() == null)
|
||||
if (_drawningAirCraft.getEntityAirCraft() == null)
|
||||
return;
|
||||
if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) {
|
||||
return;
|
||||
@ -89,10 +152,9 @@ public class FormAirFighter extends JFrame {
|
||||
Graphics g = PictureBox.getGraphics();
|
||||
g.setColor(PictureBox.getBackground());
|
||||
g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight());
|
||||
_drawningAirFighter.drawAirFighter(g);
|
||||
_drawningAirCraft.drawAirCraft(g);
|
||||
|
||||
RepaintControls();
|
||||
|
||||
}
|
||||
private void RepaintControls() {
|
||||
for (JComponent control : controls) {
|
||||
@ -100,15 +162,15 @@ public class FormAirFighter extends JFrame {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void InitializeControlsRepaintList() {
|
||||
controls = new LinkedList<>();
|
||||
controls.add(buttonCreate);
|
||||
controls.add(buttonCreateAirFighter);
|
||||
controls.add(buttonCreateAirCraft);
|
||||
controls.add(buttonUp);
|
||||
controls.add(buttonDown);
|
||||
controls.add(buttonLeft);
|
||||
controls.add(buttonRight);
|
||||
controls.add(comboBoxStrategy);
|
||||
controls.add(buttonStrategyStep);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
66
ProjectAirFighter/src/MovementStrategy/AbstractStrategy.java
Normal file
66
ProjectAirFighter/src/MovementStrategy/AbstractStrategy.java
Normal file
@ -0,0 +1,66 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _moveableObject;
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
protected int FieldWidth;
|
||||
protected int FieldHeight;
|
||||
public StrategyStatus GetStatus() { return _state; }
|
||||
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height)
|
||||
{
|
||||
if (moveableObject == null)
|
||||
{
|
||||
_state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = StrategyStatus.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestination())
|
||||
{
|
||||
_state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
protected boolean MoveLeft() { return MoveTo(MovementDirection.Left); }
|
||||
protected boolean MoveRight() { return MoveTo(MovementDirection.Right); }
|
||||
protected boolean MoveUp() { return MoveTo(MovementDirection.Up); }
|
||||
protected boolean MoveDown() { return MoveTo(MovementDirection.Down); }
|
||||
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
|
||||
|
||||
protected int GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return _moveableObject.GetStep();
|
||||
}
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract boolean IsTargetDestination();
|
||||
private boolean MoveTo(MovementDirection directionType)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject.TryMoveObject(directionType))
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean TryMoveObject(MovementDirection direction);
|
||||
void MoveObject(MovementDirection direction);
|
||||
}
|
47
ProjectAirFighter/src/MovementStrategy/MoveToBorder.java
Normal file
47
ProjectAirFighter/src/MovementStrategy/MoveToBorder.java
Normal file
@ -0,0 +1,47 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy {
|
||||
protected boolean IsTargetDestination()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder() <= FieldWidth &&
|
||||
objParams.RightBorder() + GetStep() >= FieldWidth &&
|
||||
objParams.DownBorder() <= FieldHeight &&
|
||||
objParams.DownBorder() + GetStep() >= FieldHeight;
|
||||
}
|
||||
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.RightBorder() - FieldWidth;
|
||||
if (Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.DownBorder() - FieldHeight;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
46
ProjectAirFighter/src/MovementStrategy/MoveToCenter.java
Normal file
46
ProjectAirFighter/src/MovementStrategy/MoveToCenter.java
Normal file
@ -0,0 +1,46 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy {
|
||||
protected boolean IsTargetDestination()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return (objParams.ObjectMiddleHorizontal() - GetStep() <= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical() - GetStep() <= FieldHeight / 2 &&
|
||||
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2);
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
24
ProjectAirFighter/src/MovementStrategy/MoveableAirCraft.java
Normal file
24
ProjectAirFighter/src/MovementStrategy/MoveableAirCraft.java
Normal file
@ -0,0 +1,24 @@
|
||||
package MovementStrategy;
|
||||
|
||||
import Drawnings.DrawningAirCraft;
|
||||
|
||||
public class MoveableAirCraft implements IMoveableObject {
|
||||
private DrawningAirCraft _airCraft = null;
|
||||
public MoveableAirCraft(DrawningAirCraft drawningAirCraft)
|
||||
{
|
||||
_airCraft = drawningAirCraft;
|
||||
}
|
||||
|
||||
public ObjectParameters GetObjectPosition()
|
||||
{
|
||||
if (_airCraft == null || _airCraft.getEntityAirCraft() == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_airCraft.GetPosX(), _airCraft.GetPosY(), _airCraft.GetWidth(), _airCraft.GetHeight());
|
||||
}
|
||||
|
||||
public int GetStep() { return (int) _airCraft.getEntityAirCraft().getStep(); }
|
||||
public boolean TryMoveObject(MovementDirection direction) { return _airCraft.moveTransport(direction); }
|
||||
public void MoveObject(MovementDirection direction) { _airCraft.moveTransport(direction); }
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum MovementDirection {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right;
|
||||
}
|
25
ProjectAirFighter/src/MovementStrategy/ObjectParameters.java
Normal file
25
ProjectAirFighter/src/MovementStrategy/ObjectParameters.java
Normal 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum StrategyStatus {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user