i dont know
This commit is contained in:
parent
3d6983c22a
commit
67ad38f49a
@ -1,3 +1,4 @@
|
||||
package SelfPropelledArtilleryUnit;
|
||||
public enum DirectionType
|
||||
{
|
||||
Up(1),
|
@ -1,8 +1,12 @@
|
||||
package SelfPropelledArtilleryUnit.DrawningObjects;
|
||||
import SelfPropelledArtilleryUnit.NumbeRollers;
|
||||
import SelfPropelledArtilleryUnit.SelfPropelledArtilleryUnit.Entities;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
|
||||
public class DrawingRollers {
|
||||
public class DrawningRollers {
|
||||
Graphics2D g2d;
|
||||
int startPosX;
|
||||
int startPosY;
|
||||
@ -18,7 +22,7 @@ public class DrawingRollers {
|
||||
_numbeRollers = NumbeRollers.fromNumberToEnum(numRollers);
|
||||
}
|
||||
|
||||
public DrawingRollers(Graphics2D g2d, int startPosX, int startPosY, Color color, int numRollers){
|
||||
public DrawningRollers(Graphics2D g2d, int startPosX, int startPosY, Color color, int numRollers){
|
||||
this.color = color;
|
||||
this.g2d = g2d;
|
||||
this.startPosX = startPosX;
|
@ -1,36 +1,59 @@
|
||||
package SelfPropelledArtilleryUnit.DrawningObjects;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Line2D;
|
||||
import java.awt.geom.Path2D;
|
||||
|
||||
public class DrawingSPAU {
|
||||
import SelfPropelledArtilleryUnit.DirectionType;
|
||||
import SelfPropelledArtilleryUnit.EntitySPAU;
|
||||
import SelfPropelledArtilleryUnit.SelfPropelledArtilleryUnit.Entities;
|
||||
|
||||
|
||||
public class DrawningSPAU {
|
||||
private EntitySPAU EntitySPAU;
|
||||
private int pictureWidth;
|
||||
private int pictureHeight;
|
||||
private int startPosX;
|
||||
private int startPosY;
|
||||
protected int startPosX;
|
||||
protected int startPosY;
|
||||
private int _numbeRollers;
|
||||
private final int carWidth = 135;
|
||||
private final int carHeight = 75;
|
||||
|
||||
public void setEntitySPAU(EntitySPAU entitySPAU) {
|
||||
protected void setEntitySPAU(EntitySPAU entitySPAU) {
|
||||
this.EntitySPAU = entitySPAU;
|
||||
}
|
||||
public boolean Init(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height)
|
||||
|
||||
public DrawningSPAU(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, int width, int height)
|
||||
{
|
||||
this._numbeRollers = _numbeRollers;
|
||||
this.pictureWidth = width;
|
||||
this.pictureHeight = height;
|
||||
if (this.carHeight >= height)
|
||||
{
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
if (this.carWidth >= width)
|
||||
{
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
EntitySPAU = new EntitySPAU();
|
||||
EntitySPAU.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
|
||||
return true;
|
||||
EntitySPAU = new EntitySPAU(_numbeRollers, speed, weight, bodyColor, additionalColor, bodyKit);
|
||||
}
|
||||
|
||||
public DrawningSPAU(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, int width, int height, int carWidth, int carHeight)
|
||||
{
|
||||
this._numbeRollers = _numbeRollers;
|
||||
this.pictureWidth = width;
|
||||
this.pictureHeight = height;
|
||||
this.carWidth = carWidth;
|
||||
this.carHeight = carHeight;
|
||||
if (this.carHeight >= height)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (this.carWidth >= width)
|
||||
{
|
||||
return;
|
||||
}
|
||||
EntitySPAU = new EntitySPAU(_numbeRollers, speed, weight, bodyColor, additionalColor, bodyKit);
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
@ -48,7 +71,7 @@ public class DrawingSPAU {
|
||||
}
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntitySPAU == null)
|
||||
if (!CanMove(direction) || EntitySPAU == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -56,31 +79,19 @@ public class DrawingSPAU {
|
||||
{
|
||||
//влево
|
||||
case Left:
|
||||
if (this.startPosX - EntitySPAU.Step > 0)
|
||||
{
|
||||
this.startPosX -= (int)EntitySPAU.Step;
|
||||
}
|
||||
this.startPosX -= (int)EntitySPAU.Step;
|
||||
break;
|
||||
//вверх
|
||||
case Up:
|
||||
if (this.startPosY - EntitySPAU.Step > 0)
|
||||
{
|
||||
this.startPosY -= (int)EntitySPAU.Step;
|
||||
}
|
||||
this.startPosY -= (int)EntitySPAU.Step;
|
||||
break;
|
||||
// вправо
|
||||
case Right:
|
||||
if (this.startPosX + EntitySPAU.Step < this.pictureWidth - this.carWidth)
|
||||
{
|
||||
this.startPosX += (int)EntitySPAU.Step;
|
||||
}
|
||||
this.startPosX += (int)EntitySPAU.Step;
|
||||
break;
|
||||
//вниз
|
||||
case Down:
|
||||
if (this.startPosY + EntitySPAU.Step < this.pictureHeight - this.carHeight)
|
||||
{
|
||||
this.startPosY += (int)EntitySPAU.Step;
|
||||
}
|
||||
this.startPosY += (int)EntitySPAU.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -93,13 +104,13 @@ public class DrawingSPAU {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
BasicStroke penBlack = new BasicStroke(1);
|
||||
Color additionalColor = EntitySPAU.AdditionalColor;
|
||||
//Color additionalColor = EntitySPAU.AdditionalColor;
|
||||
Color bodyColor = EntitySPAU.BodyColor;
|
||||
boolean hasBodyKit = EntitySPAU.BodyKit;
|
||||
|
||||
Path2D.Double path = new Path2D.Double();
|
||||
|
||||
// Обвесы
|
||||
/* Обвесы
|
||||
if (hasBodyKit) {
|
||||
path.moveTo(startPosX + 15, startPosY + 20);
|
||||
path.lineTo(startPosX + 35, startPosY + 20);
|
||||
@ -118,9 +129,10 @@ public class DrawingSPAU {
|
||||
Line2D line = new Line2D.Double(startPosX + 5, startPosY + 20, startPosX + 15, startPosY + 25);
|
||||
g2d.draw(line);
|
||||
}
|
||||
*/
|
||||
|
||||
//гусеницы
|
||||
DrawingRollers Rollers = new DrawingRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers);
|
||||
DrawningRollers Rollers = new DrawningRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers);
|
||||
Rollers.Draw();
|
||||
|
||||
// пушка
|
||||
@ -158,4 +170,64 @@ public class DrawingSPAU {
|
||||
g2d.fill(path);
|
||||
g2d.draw(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Координата X объекта
|
||||
*
|
||||
* @return значение координаты X
|
||||
*/
|
||||
public int getPosX() {
|
||||
return this.startPosX;
|
||||
}
|
||||
|
||||
/**
|
||||
* Координата Y объекта
|
||||
*
|
||||
* @return значение координаты Y
|
||||
*/
|
||||
public int getPosY() {
|
||||
return this.startPosY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ширина объекта
|
||||
*
|
||||
* @return значение ширины
|
||||
*/
|
||||
public int getWidth() {
|
||||
return this.carWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Высота объекта
|
||||
*
|
||||
* @return значение высоты
|
||||
*/
|
||||
public int getHeight() {
|
||||
return this.carHeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверка, что объект может переместиться в указанном направлении
|
||||
*
|
||||
* @param direction Направление
|
||||
* @return true - можно переместиться в указанном направлении
|
||||
*/
|
||||
public boolean canMove(DirectionType direction) {
|
||||
if (EntitySPAU == null) {
|
||||
return false;
|
||||
}
|
||||
switch (direction) {
|
||||
case Left: // влево
|
||||
return startPosX - EntitySPAU.getStep() > 0;
|
||||
case Up: // вверх
|
||||
return startPosY - EntitySPAU.getStep() > 0;
|
||||
case Right: // вправо
|
||||
return startPosX + EntitySPAU.getStep() < _pictureWidth - _carWidth;
|
||||
case Down: // вниз
|
||||
return startPosY + EntitySPAU.getStep() < _pictureHeight - _carHeight;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
package SelfPropelledArtilleryUnit;
|
||||
import java.awt.Color;
|
||||
|
||||
public class EntitySPAU {
|
@ -1,4 +1,6 @@
|
||||
package SelfPropelledArtilleryUnit;
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
@ -14,7 +16,7 @@ public class Form extends JFrame {
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonRight;
|
||||
private JLabel pictureBoxSPAU;
|
||||
private DrawingSPAU drawingSPAU;
|
||||
private DrawningSPAU drawingSPAU;
|
||||
private JTextField wheelsTextField;
|
||||
private JLabel wheelsLabel;
|
||||
|
||||
@ -125,7 +127,7 @@ public class Form extends JFrame {
|
||||
|
||||
if (wheelCount > 0) {
|
||||
Random random = new Random();
|
||||
drawingSPAU = new DrawingSPAU();
|
||||
drawingSPAU = new DrawningSPAU();
|
||||
drawingSPAU.Init(wheelCount, random.nextInt(200) + 100,
|
||||
random.nextInt(2000) + 1000,
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
@ -0,0 +1,151 @@
|
||||
package SelfPropelledArtilleryUnit.MovementStrategy;
|
||||
|
||||
/**
|
||||
* Класс-стратегия перемещения объекта
|
||||
*/
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
/**
|
||||
* Перемещаемый объект
|
||||
*/
|
||||
private IMoveableObject moveableObject;
|
||||
|
||||
/**
|
||||
* Статус перемещения
|
||||
*/
|
||||
private Status state = Status.NotInit;
|
||||
|
||||
/**
|
||||
* Ширина поля
|
||||
*/
|
||||
private int fieldWidth;
|
||||
|
||||
/**
|
||||
* Высота поля
|
||||
*/
|
||||
private int fieldHeight;
|
||||
|
||||
/**
|
||||
* Статус перемещения
|
||||
*/
|
||||
public Status getStatus() { return state; }
|
||||
|
||||
/**
|
||||
* Получить ширину поля
|
||||
*/
|
||||
protected int getFieldWidth() { return fieldWidth; }
|
||||
|
||||
/**
|
||||
* Получить высоту поля
|
||||
*/
|
||||
protected int getFieldHeight() { return fieldHeight; }
|
||||
|
||||
/**
|
||||
* Установка данных
|
||||
* @param moveableObject Перемещаемый объект
|
||||
* @param width Ширина поля
|
||||
* @param height Высота поля
|
||||
*/
|
||||
public void setData(IMoveableObject moveableObject, int width, int height)
|
||||
{
|
||||
if (moveableObject == null)
|
||||
{
|
||||
state = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
state = Status.InProgress;
|
||||
this.moveableObject = moveableObject;
|
||||
this.fieldWidth = width;
|
||||
this.fieldHeight = height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Шаг перемещения
|
||||
*/
|
||||
public void makeStep()
|
||||
{
|
||||
if (state != Status.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (isTargetDestination())
|
||||
{
|
||||
state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
moveToTarget();
|
||||
}
|
||||
|
||||
/**
|
||||
* Перемещение влево
|
||||
* @return Результат перемещения (true - удалось переместиться, false - неудача)
|
||||
*/
|
||||
protected boolean moveLeft() { return moveTo(DirectionType.Left); }
|
||||
|
||||
/**
|
||||
* Перемещение вправо
|
||||
* @return Результат перемещения (true - удалось переместиться, false - неудача)
|
||||
*/
|
||||
protected boolean moveRight() { return moveTo(DirectionType.Right); }
|
||||
|
||||
/**
|
||||
* Перемещение вверх
|
||||
* @return Результат перемещения (true - удалось переместиться, false - неудача)
|
||||
*/
|
||||
protected boolean moveUp() { return moveTo(DirectionType.Up); }
|
||||
|
||||
/**
|
||||
* Перемещение вниз
|
||||
* @return Результат перемещения (true - удалось переместиться, false - неудача)
|
||||
*/
|
||||
protected boolean moveDown() { return moveTo(DirectionType.Down); }
|
||||
|
||||
/**
|
||||
* Параметры объекта
|
||||
* @return Объект ObjectParameters
|
||||
*/
|
||||
protected ObjectParameters getObjectParameters() { return moveableObject != null ? moveableObject.getObjectPosition() : null; }
|
||||
|
||||
/**
|
||||
* Шаг объекта
|
||||
* @return Шаг объекта
|
||||
*/
|
||||
protected Integer getStep()
|
||||
{
|
||||
if (state != Status.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return moveableObject != null ? moveableObject.getStep() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Перемещение к цели
|
||||
*/
|
||||
protected abstract void moveToTarget();
|
||||
|
||||
/**
|
||||
* Достигнута ли цель
|
||||
* @return true если цель достигнута
|
||||
*/
|
||||
protected abstract boolean isTargetDestination();
|
||||
|
||||
/**
|
||||
* Попытка перемещения в требуемом направлении
|
||||
* @param directionType Направление
|
||||
* @return Результат попытки (true - удалось переместиться, false - неудача)
|
||||
*/
|
||||
private boolean moveTo(DirectionType directionType)
|
||||
{
|
||||
if (state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (moveableObject != null && moveableObject.checkCanMove(directionType))
|
||||
{
|
||||
moveableObject.moveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package SelfPropelledArtilleryUnit.MovementStrategy;
|
||||
|
||||
import SelfPropelledArtilleryUnit.DirectionType;
|
||||
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
|
||||
|
||||
/**
|
||||
* Класс-стратегия перемещения объекта, реализация интерфейса IMoveableObject для работы с объектом DrawningCar (паттерн Adapter)
|
||||
*/
|
||||
public class DrawningObjectSPAU implements IMoveableObject {
|
||||
|
||||
private final DrawningSPAU drawningCar;
|
||||
|
||||
public DrawningObjectSPAU(DrawningSPAU drawningCar) {
|
||||
this.drawningCar = drawningCar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectParameters getObjectPosition() {
|
||||
if (drawningCar == null || drawningCar.getEntitySPAU() == null) {
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(drawningCar.getPosX(),
|
||||
drawningCar.getPosY(),
|
||||
drawningCar.getWidth(),
|
||||
drawningCar.getHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStep() {
|
||||
return drawningCar != null && drawningCar.getEntitySPAU() != null
|
||||
? (int) drawningCar.getEntitySPAU().getStep() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCanMove(DirectionType direction) {
|
||||
return drawningCar != null && drawningCar.canMove(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveObject(DirectionType direction) {
|
||||
if (drawningCar != null) {
|
||||
drawningCar.moveTransport(direction);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
|
||||
package SelfPropelledArtilleryUnit;
|
||||
public enum NumbeRollers {
|
||||
Min(4),
|
||||
Mid(5),
|
@ -1,3 +1,4 @@
|
||||
package SelfPropelledArtilleryUnit;
|
||||
import javax.swing.*;
|
||||
|
||||
public class SelfPropelledArtilleryUnit {
|
Loading…
Reference in New Issue
Block a user