2 лаба
This commit is contained in:
parent
0fc01401f2
commit
2c45ba8045
57
AirFighter/src/AbstractStrategy.java
Normal file
57
AirFighter/src/AbstractStrategy.java
Normal 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;
|
||||
}
|
||||
}
|
@ -1,118 +1,33 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingAirFighter extends JPanel {
|
||||
private EntityAirFighter entityAirFighter;
|
||||
public EntityAirFighter getEntityAirFighter(){
|
||||
return entityAirFighter;
|
||||
public class DrawingAirFighter extends DrawingAirplane {
|
||||
|
||||
public DrawingAirFighter(int speed, double weight, Color bodyColor,
|
||||
Color additionalColor, boolean rockets, boolean wings, int width, int height, int enginesType, int enginesNumber){
|
||||
super(speed, weight, bodyColor, width, height, enginesType, enginesNumber);
|
||||
if(entityAirplane != null)
|
||||
entityAirplane = new EntityAirFighter(speed, weight, bodyColor, additionalColor, rockets, wings);
|
||||
}
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private final int _PlaneWidth = 160;
|
||||
private final int _PlaneHeight = 160;
|
||||
private DrawingEngines drawingEngines;
|
||||
public boolean init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean rockets, boolean wings, int width, int height, int enginesNumber)
|
||||
|
||||
public void drawTransport(Graphics2D g)
|
||||
{
|
||||
if (_PlaneWidth > width || _PlaneHeight > height)
|
||||
return false;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
entityAirFighter = new EntityAirFighter();
|
||||
entityAirFighter.init(speed, weight, bodyColor, additionalColor,
|
||||
rockets, wings);
|
||||
drawingEngines = new DrawingEngines();
|
||||
drawingEngines.setNumber(enginesNumber);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setPosition(int x, int y)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
|
||||
if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _PlaneWidth) || _startPosY > (_pictureHeight - _PlaneHeight))
|
||||
{
|
||||
_startPosX = 50;
|
||||
_startPosY = 50;
|
||||
}
|
||||
}
|
||||
public void moveTransport(DirectionType direction)
|
||||
{
|
||||
if (entityAirFighter == null)
|
||||
return;
|
||||
int step = entityAirFighter.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 + _PlaneWidth + step < _pictureWidth)
|
||||
_startPosX += step;
|
||||
break;
|
||||
case DOWN:
|
||||
if (_startPosY + _PlaneHeight + step < _pictureHeight)
|
||||
_startPosY += step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void drawTransport(Graphics gr)
|
||||
{
|
||||
super.paintComponent(gr);
|
||||
Graphics2D g = (Graphics2D) gr;
|
||||
if (entityAirFighter == null)
|
||||
if (!(entityAirplane instanceof EntityAirFighter))
|
||||
return;
|
||||
EntityAirFighter entityAirFighter = (EntityAirFighter) entityAirplane;
|
||||
BasicStroke pen = new BasicStroke(2);
|
||||
Color bodyColor = entityAirFighter.getBodyColor();
|
||||
|
||||
Color additionalColor = entityAirFighter.getAdditionalColor();
|
||||
g.setStroke(pen);
|
||||
|
||||
|
||||
// тело
|
||||
g.setColor(bodyColor);
|
||||
int[] pointX = new int[]{ _startPosX, _startPosX+25, _startPosX+25};
|
||||
int[] pointY = new int[]{ _startPosY + 80, _startPosY+69, _startPosY+91};
|
||||
g.fillPolygon(pointX, pointY, 3);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect( _startPosX + 25, _startPosY + 70, 135, 20);
|
||||
|
||||
//Крылья
|
||||
g.setColor(Color.BLACK);
|
||||
|
||||
pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80};
|
||||
pointY = new int[] { _startPosY+70, _startPosY, _startPosY, _startPosY+70};
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
|
||||
|
||||
pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80};
|
||||
pointY = new int[] { _startPosY+90, _startPosY+160, _startPosY+160, _startPosY+90};
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
|
||||
pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160};
|
||||
pointY = new int[] { _startPosY+70, _startPosY+50, _startPosY+30, _startPosY+70};
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
|
||||
|
||||
pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160};
|
||||
pointY = new int[] { _startPosY+90, _startPosY+110, _startPosY+130, _startPosY+90};
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
|
||||
|
||||
// кралья
|
||||
super.drawTransport(g);
|
||||
// крылья
|
||||
if (entityAirFighter.getWings())
|
||||
{
|
||||
g.setColor(additionalColor);
|
||||
|
||||
pointX = new int[]{_startPosX + 100, _startPosX + 100, _startPosX + 110, _startPosX + 120};
|
||||
pointY = new int[]{_startPosY + 70, _startPosY+30, _startPosY+30, _startPosY + 70};
|
||||
int[] pointX = new int[]{_startPosX + 100, _startPosX + 100, _startPosX + 110, _startPosX + 120};
|
||||
int[] pointY = new int[]{_startPosY + 70, _startPosY+30, _startPosY+30, _startPosY + 70};
|
||||
|
||||
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
|
||||
pointX = new int[]{_startPosX + 100, _startPosX + 100, _startPosX + 110, _startPosX + 120};
|
||||
@ -124,15 +39,13 @@ public class DrawingAirFighter extends JPanel {
|
||||
{
|
||||
g.setColor(additionalColor);
|
||||
|
||||
pointX = new int[]{_startPosX+30, _startPosX+40, _startPosX+90, _startPosX+90, _startPosX+40};
|
||||
pointY = new int[]{_startPosY+25, _startPosY+20, _startPosY+20, _startPosY+30, _startPosY+30};
|
||||
int[] pointX = new int[]{_startPosX+30, _startPosX+40, _startPosX+90, _startPosX+90, _startPosX+40};
|
||||
int[] pointY = new int[]{_startPosY+25, _startPosY+20, _startPosY+20, _startPosY+30, _startPosY+30};
|
||||
g.fillPolygon(pointX, pointY, 5);
|
||||
|
||||
pointX = new int[]{_startPosX+30, _startPosX+40, _startPosX+90, _startPosX+90, _startPosX+40};
|
||||
pointY = new int[]{_startPosY+135, _startPosY+130, _startPosY+130, _startPosY+140, _startPosY+140};
|
||||
g.fillPolygon(pointX, pointY, 5);
|
||||
}
|
||||
g.setColor(Color.BLACK);
|
||||
drawingEngines.drawEngines(g, _startPosX, _startPosY);
|
||||
}
|
||||
}
|
142
AirFighter/src/DrawingAirplane.java
Normal file
142
AirFighter/src/DrawingAirplane.java
Normal file
@ -0,0 +1,142 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingAirplane {
|
||||
protected EntityAirplane entityAirplane;
|
||||
protected void setEntityPlane(EntityAirplane entityPlane){
|
||||
this.entityAirplane = entityAirplane;
|
||||
}
|
||||
public EntityAirplane getEntityPlane() {
|
||||
return entityAirplane;
|
||||
}
|
||||
private IDrawEngines drawingEngines;
|
||||
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 _PlaneWidth = 160;
|
||||
public int getWidth() {
|
||||
return _PlaneWidth;
|
||||
}
|
||||
private final int _PlaneHeight = 160;
|
||||
public int getHeight(){
|
||||
return _PlaneHeight;
|
||||
}
|
||||
public DrawingAirplane(int speed, double weight, Color bodyColor, int width, int height, int enginesType, int enginesNumber) {
|
||||
if (width < _PlaneWidth || height < _PlaneHeight)
|
||||
return;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
entityAirplane = new EntityAirplane(speed, weight, bodyColor);
|
||||
switch (enginesType){
|
||||
case 1:
|
||||
drawingEngines = new DrawingEnginesSquare();
|
||||
break;
|
||||
case 2:
|
||||
drawingEngines = new DrawingEnginesRound();
|
||||
break;
|
||||
default:
|
||||
drawingEngines = new DrawingEnginesTriangle();
|
||||
break;
|
||||
}
|
||||
drawingEngines.setNumber(enginesNumber);
|
||||
}
|
||||
public void setPosition(int x, int y)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
|
||||
if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _PlaneWidth) || _startPosY > (_pictureHeight - _PlaneHeight))
|
||||
{
|
||||
_startPosX = 50;
|
||||
_startPosY = 50;
|
||||
}
|
||||
}
|
||||
|
||||
public void drawTransport(Graphics2D g) {
|
||||
if (entityAirplane == null)
|
||||
return;
|
||||
BasicStroke pen = new BasicStroke(2);
|
||||
Color bodyColor = entityAirplane.getBodyColor();
|
||||
g.setStroke(pen);
|
||||
g.setColor(bodyColor);
|
||||
|
||||
// тело
|
||||
g.setColor(bodyColor);
|
||||
int[] pointX = new int[]{ _startPosX, _startPosX+25, _startPosX+25};
|
||||
int[] pointY = new int[]{ _startPosY + 80, _startPosY+69, _startPosY+91};
|
||||
g.fillPolygon(pointX, pointY, 3);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect( _startPosX + 25, _startPosY + 70, 135, 20);
|
||||
|
||||
//Крылья
|
||||
g.setColor(Color.BLACK);
|
||||
|
||||
pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80};
|
||||
pointY = new int[] { _startPosY+70, _startPosY, _startPosY, _startPosY+70};
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
|
||||
|
||||
pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80};
|
||||
pointY = new int[] { _startPosY+90, _startPosY+160, _startPosY+160, _startPosY+90};
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
|
||||
pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160};
|
||||
pointY = new int[] { _startPosY+70, _startPosY+50, _startPosY+30, _startPosY+70};
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
|
||||
|
||||
pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160};
|
||||
pointY = new int[] { _startPosY+90, _startPosY+110, _startPosY+130, _startPosY+90};
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
drawingEngines.drawEngines(g, _startPosX, _startPosY);
|
||||
}
|
||||
|
||||
public boolean canMove(DirectionType direction) {
|
||||
if (entityAirplane == null) {
|
||||
return false;
|
||||
}
|
||||
switch(direction) {
|
||||
case LEFT:
|
||||
return _startPosX - entityAirplane.step.get().intValue() > 0;
|
||||
case UP:
|
||||
return _startPosY - entityAirplane.step.get().intValue() > 0;
|
||||
case RIGHT:
|
||||
return _startPosX + entityAirplane.step.get().intValue() + _PlaneWidth < _pictureWidth;
|
||||
case DOWN:
|
||||
return _startPosY + entityAirplane.step.get().intValue() + _PlaneWidth < _pictureWidth;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public void moveTransport(DirectionType direction)
|
||||
{
|
||||
if (entityAirplane == null)
|
||||
return;
|
||||
int step = entityAirplane.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 + _PlaneWidth + step < _pictureWidth)
|
||||
_startPosX += step;
|
||||
break;
|
||||
case DOWN:
|
||||
if (_startPosY + _PlaneHeight + step < _pictureHeight)
|
||||
_startPosY += step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
27
AirFighter/src/DrawingEnginesRound.java
Normal file
27
AirFighter/src/DrawingEnginesRound.java
Normal file
@ -0,0 +1,27 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingEnginesRound implements IDrawEngines{
|
||||
private EngineNumber number;
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = EngineNumber.TWO;
|
||||
if(x == 4)
|
||||
number = EngineNumber.FOUR;
|
||||
if(x >= 6)
|
||||
number = EngineNumber.SIX;
|
||||
}
|
||||
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillOval(_startX+53, _startY+35, 7, 7);
|
||||
graphics2D.fillOval(_startX+53, _startY+120, 7, 7);
|
||||
|
||||
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
|
||||
graphics2D.fillOval(_startX+53, _startY+55, 7, 7);
|
||||
graphics2D.fillOval(_startX+53, _startY+100, 7, 7);
|
||||
}
|
||||
|
||||
if (number == EngineNumber.SIX){
|
||||
graphics2D.fillOval(_startX+53, _startY+45, 7, 7);
|
||||
graphics2D.fillOval(_startX+53, _startY+110, 7, 7);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingEngines {
|
||||
public class DrawingEnginesSquare implements IDrawEngines{
|
||||
private EngineNumber number;
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
@ -11,16 +11,16 @@ public class DrawingEngines {
|
||||
number = EngineNumber.SIX;
|
||||
}
|
||||
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillRect(_startX+53, _startY+35, 7, 5);
|
||||
graphics2D.fillRect(_startX+53, _startY+120, 7, 5);
|
||||
graphics2D.fillRect(_startX+53, _startY+35, 7, 7);
|
||||
graphics2D.fillRect(_startX+53, _startY+120, 7, 7);
|
||||
|
||||
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
|
||||
graphics2D.fillRect(_startX+53, _startY+55, 7, 5);
|
||||
graphics2D.fillRect(_startX+53, _startY+100, 7, 5);
|
||||
graphics2D.fillRect(_startX+53, _startY+55, 7, 7);
|
||||
graphics2D.fillRect(_startX+53, _startY+100, 7, 7);
|
||||
}
|
||||
if (number == EngineNumber.SIX){
|
||||
graphics2D.fillRect(_startX+53, _startY+45, 7, 5);
|
||||
graphics2D.fillRect(_startX+53, _startY+110, 7, 5);
|
||||
graphics2D.fillRect(_startX+53, _startY+45, 7, 7);
|
||||
graphics2D.fillRect(_startX+53, _startY+110, 7, 7);
|
||||
}
|
||||
}
|
||||
}
|
43
AirFighter/src/DrawingEnginesTriangle.java
Normal file
43
AirFighter/src/DrawingEnginesTriangle.java
Normal file
@ -0,0 +1,43 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingEnginesTriangle implements IDrawEngines{
|
||||
private EngineNumber number;
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = EngineNumber.TWO;
|
||||
if(x == 4)
|
||||
number = EngineNumber.FOUR;
|
||||
if(x >= 6)
|
||||
number = EngineNumber.SIX;
|
||||
}
|
||||
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){
|
||||
|
||||
int[] pointX = new int[]{ _startX+45, _startX+60, _startX+60};
|
||||
int[] pointY = new int[]{ _startY + 35, _startY+30, _startY+40};
|
||||
graphics2D.fillPolygon(pointX, pointY, 3);
|
||||
|
||||
pointX = new int[]{_startX+45, _startX+60, _startX+60};
|
||||
pointY = new int[]{ _startY + 125, _startY+120, _startY+130};
|
||||
graphics2D.fillPolygon(pointX, pointY, 3);
|
||||
|
||||
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
|
||||
pointX = new int[]{ _startX+45, _startX+60, _startX+60};
|
||||
pointY = new int[]{ _startY + 55, _startY+50, _startY+60};
|
||||
graphics2D.fillPolygon(pointX, pointY, 3);
|
||||
|
||||
pointX = new int[]{_startX+45, _startX+60, _startX+60};
|
||||
pointY = new int[]{ _startY + 105, _startY+100, _startY+110};
|
||||
graphics2D.fillPolygon(pointX, pointY, 3);
|
||||
}
|
||||
|
||||
if (number == EngineNumber.SIX){
|
||||
pointX = new int[]{ _startX+45, _startX+60, _startX+60};
|
||||
pointY = new int[]{ _startY + 45, _startY+40, _startY+50};
|
||||
graphics2D.fillPolygon(pointX, pointY, 3);
|
||||
|
||||
pointX = new int[]{_startX+45, _startX+60, _startX+60};
|
||||
pointY = new int[]{ _startY + 115, _startY+110, _startY+120};
|
||||
graphics2D.fillPolygon(pointX, pointY, 3);
|
||||
}
|
||||
}
|
||||
}
|
27
AirFighter/src/DrawingObjectAirFighter.java
Normal file
27
AirFighter/src/DrawingObjectAirFighter.java
Normal file
@ -0,0 +1,27 @@
|
||||
public class DrawingObjectAirFighter implements IMoveableObject{
|
||||
private final DrawingAirplane drawingAirplane;
|
||||
|
||||
public DrawingObjectAirFighter(DrawingAirplane drawingPlane){
|
||||
this.drawingAirplane = drawingPlane;
|
||||
}
|
||||
public ObjectParameters getObjectPosition(){
|
||||
if(drawingAirplane == null || drawingAirplane.getEntityPlane() == null)
|
||||
return null;
|
||||
return new ObjectParameters(drawingAirplane.getPosX(), drawingAirplane.getPosY(),
|
||||
drawingAirplane.getWidth(), drawingAirplane.getHeight());
|
||||
}
|
||||
public int getStep(){
|
||||
if(drawingAirplane.getEntityPlane() == null)
|
||||
return 0;
|
||||
return drawingAirplane.getEntityPlane().step.get().intValue();
|
||||
}
|
||||
public boolean checkCanMove(DirectionType direction){
|
||||
if(drawingAirplane == null)
|
||||
return false;
|
||||
return drawingAirplane.canMove(direction);
|
||||
}
|
||||
public void moveObject(DirectionType direction){
|
||||
drawingAirplane.moveTransport(direction);
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,7 @@
|
||||
import java.awt.*;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public class EntityAirFighter extends EntityAirplane{
|
||||
private Color additionalColor;
|
||||
public Color getAdditionalColor(){
|
||||
return additionalColor;
|
||||
@ -26,14 +14,11 @@ public class EntityAirFighter {
|
||||
public boolean getRockets() {
|
||||
return isRockets;
|
||||
}
|
||||
public Supplier<Double> step = () -> (double) speed * 100 / weight;
|
||||
public void init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean isWings, boolean isRockets) {
|
||||
this.speed = speed;
|
||||
this.weight = weight;
|
||||
this.bodyColor = bodyColor;
|
||||
this.additionalColor = additionalColor;
|
||||
this.isWings = isWings;
|
||||
this.isRockets = isRockets;
|
||||
}
|
||||
public EntityAirFighter(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean isWings, boolean isRockets) {
|
||||
super(speed, weight, bodyColor);
|
||||
this.additionalColor = additionalColor;
|
||||
this.isWings = isWings;
|
||||
this.isRockets = isRockets;
|
||||
}
|
||||
}
|
24
AirFighter/src/EntityAirplane.java
Normal file
24
AirFighter/src/EntityAirplane.java
Normal file
@ -0,0 +1,24 @@
|
||||
import java.awt.*;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class EntityAirplane {
|
||||
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 EntityAirplane(int speed, double weight, Color bodyColor){
|
||||
this.speed = speed;
|
||||
this.weight = weight;
|
||||
this.bodyColor = bodyColor;
|
||||
}
|
||||
}
|
@ -6,7 +6,9 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
public class FrameAirFighter extends JFrame {
|
||||
private DrawingAirFighter drawingAirFighter;
|
||||
private DrawingAirplane drawingAirplane;
|
||||
private AbstractStrategy abstractStrategy;
|
||||
private JComboBox comboBoxStrategy;
|
||||
private final JComponent pictureBox;
|
||||
public FrameAirFighter() throws IOException {
|
||||
super("Истребитель");
|
||||
@ -16,17 +18,22 @@ public class FrameAirFighter extends JFrame {
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (drawingAirFighter != null) drawingAirFighter.drawTransport(graphics2D);
|
||||
if (drawingAirplane != null) drawingAirplane.drawTransport(graphics2D);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
JButton createButton = new JButton("Создать");
|
||||
comboBoxStrategy = new JComboBox<>(new String[]{"К центру", "К границе"});
|
||||
JButton stepButton = new JButton("Шаг");
|
||||
JButton createPlaneButton = new JButton("Создать самолет");
|
||||
JButton createAirBomberButton = new JButton("Создать истребитель");
|
||||
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\right.png"))));
|
||||
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\left.png"))));
|
||||
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\up.png"))));
|
||||
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\down.png"))));
|
||||
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
|
||||
createButton.addActionListener(e -> buttonCreateClick());
|
||||
createPlaneButton.addActionListener(e -> buttonCreateAirplaneClick());
|
||||
createAirBomberButton.addActionListener(e -> buttonCreateAirFighterClick());
|
||||
stepButton.addActionListener(e -> buttonStepClick());
|
||||
rightButton.setActionCommand("right");
|
||||
rightButton.addActionListener(this::buttonMoveClick);
|
||||
leftButton.setActionCommand("left");
|
||||
@ -35,20 +42,27 @@ public class FrameAirFighter extends JFrame {
|
||||
upButton.addActionListener(this::buttonMoveClick);
|
||||
downButton.setActionCommand("down");
|
||||
downButton.addActionListener(this::buttonMoveClick);
|
||||
//component addition
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
JPanel panelAirFighter = 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());
|
||||
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
||||
rightButton.setPreferredSize(new Dimension(30,30));
|
||||
JPanel leftPanel = new JPanel(new BorderLayout());
|
||||
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(createPlaneButton, constraints);
|
||||
constraints.gridx = 1;
|
||||
constraints.gridy = 0;
|
||||
createPanel.add(createAirBomberButton, constraints);
|
||||
|
||||
JPanel movementPanel = new JPanel(new GridBagLayout());
|
||||
rightButton.setPreferredSize(new Dimension(30,30));
|
||||
constraints.gridx = 2;
|
||||
constraints.gridy = 1;
|
||||
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
|
||||
|
||||
movementPanel.add(rightButton, constraints);
|
||||
leftButton.setPreferredSize(new Dimension(30,30));
|
||||
constraints.gridx = 0;
|
||||
@ -62,47 +76,101 @@ public class FrameAirFighter extends JFrame {
|
||||
constraints.gridx = 1;
|
||||
constraints.gridy = 1;
|
||||
movementPanel.add(downButton, constraints);
|
||||
//stepPanel
|
||||
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);
|
||||
//addition to frame
|
||||
add(pictureBox);
|
||||
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
||||
rightPanel.add(stepPanel, BorderLayout.NORTH);
|
||||
leftPanel.add(createPanel, BorderLayout.SOUTH);
|
||||
panelAirFighter.add(rightPanel, BorderLayout.EAST);
|
||||
panelAirFighter.add(createPanel, BorderLayout.WEST);
|
||||
panelAirFighter.add(leftPanel, BorderLayout.WEST);
|
||||
add(panelAirFighter,BorderLayout.CENTER);
|
||||
setVisible(true);
|
||||
}
|
||||
private void buttonCreateClick() {
|
||||
private void buttonCreateAirFighterClick() {
|
||||
Random random = new Random();
|
||||
drawingAirFighter = new DrawingAirFighter();
|
||||
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||
drawingAirFighter.init(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)),
|
||||
random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(),
|
||||
(random.nextInt(3)+1)*2);
|
||||
drawingAirFighter.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||
|
||||
drawingAirplane = new DrawingAirFighter(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)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
|
||||
drawingAirplane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||
draw();
|
||||
}
|
||||
private void buttonMoveClick(ActionEvent event) {
|
||||
if(drawingAirFighter == null || drawingAirFighter.getEntityAirFighter() == null)
|
||||
private void buttonCreateAirplaneClick(){
|
||||
Random random = new Random();
|
||||
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||
drawingAirplane = new DrawingAirplane(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);
|
||||
drawingAirplane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||
draw();
|
||||
}
|
||||
|
||||
private void buttonStepClick(){
|
||||
if (drawingAirplane == 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 DrawingObjectAirFighter(drawingAirplane), 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) {
|
||||
if(drawingAirplane == null || drawingAirplane.getEntityPlane() == null)
|
||||
return;
|
||||
switch (event.getActionCommand())
|
||||
{
|
||||
case "left":
|
||||
drawingAirFighter.moveTransport(DirectionType.LEFT);
|
||||
drawingAirplane.moveTransport(DirectionType.LEFT);
|
||||
break;
|
||||
case "right":
|
||||
drawingAirFighter.moveTransport(DirectionType.RIGHT);
|
||||
drawingAirplane.moveTransport(DirectionType.RIGHT);
|
||||
break;
|
||||
case "up":
|
||||
drawingAirFighter.moveTransport(DirectionType.UP);
|
||||
drawingAirplane.moveTransport(DirectionType.UP);
|
||||
break;
|
||||
case "down":
|
||||
drawingAirFighter.moveTransport(DirectionType.DOWN);
|
||||
drawingAirplane.moveTransport(DirectionType.DOWN);
|
||||
break;
|
||||
}
|
||||
draw();
|
||||
}
|
||||
private void draw() {
|
||||
if (drawingAirFighter == null)
|
||||
if (drawingAirplane == null)
|
||||
return;
|
||||
pictureBox.repaint();
|
||||
}
|
||||
|
6
AirFighter/src/IDrawEngines.java
Normal file
6
AirFighter/src/IDrawEngines.java
Normal file
@ -0,0 +1,6 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawEngines {
|
||||
public void setNumber(int x);
|
||||
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY);
|
||||
}
|
6
AirFighter/src/IMoveableObject.java
Normal file
6
AirFighter/src/IMoveableObject.java
Normal file
@ -0,0 +1,6 @@
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters getObjectPosition();
|
||||
int getStep();
|
||||
boolean checkCanMove(DirectionType direction);
|
||||
void moveObject(DirectionType direction);
|
||||
}
|
34
AirFighter/src/MoveToBorder.java
Normal file
34
AirFighter/src/MoveToBorder.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
32
AirFighter/src/MoveToCenter.java
Normal file
32
AirFighter/src/MoveToCenter.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
19
AirFighter/src/ObjectParameters.java
Normal file
19
AirFighter/src/ObjectParameters.java
Normal 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;
|
||||
}
|
||||
}
|
5
AirFighter/src/Status.java
Normal file
5
AirFighter/src/Status.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum Status {
|
||||
NOTINIT,
|
||||
INPROGRESS,
|
||||
FINISH
|
||||
}
|
Loading…
Reference in New Issue
Block a user