This commit is contained in:
Alenka 2023-12-08 23:20:53 +04:00
parent bab381dabe
commit d90af74620
18 changed files with 843 additions and 192 deletions

106
AbstractStrategy.java Normal file
View File

@ -0,0 +1,106 @@
public abstract class AbstractStrategy {
/// <summary>
/// Перемещаемый объект
/// </summary>
private IMoveableObject _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private Status _state = Status.NotInit;
/// <summary>
/// Ширина поля
/// </summary>
protected int FieldWidth;
/// <summary>
/// Высота поля
/// </summary>
protected int FieldHeight;
/// <summary>
/// Статус перемещения
/// </summary>
public Status GetStatus() { return _state; }
/// <summary>
/// Установка данных
/// </summary>
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
public void SetData(IMoveableObject moveableObject, int width, int
height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
/// <summary>
/// Шаг перемещения
/// </summary>
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Перемещение влево
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected boolean MoveLeft() { return MoveTo(Direction.Left);};
protected boolean MoveRight(){return MoveTo(Direction.Right);}
protected boolean MoveUp(){return MoveTo(Direction.Up);}
protected boolean MoveDown(){return MoveTo(Direction.Down);}
protected ObjectParameters GetObjectParameters(){ return _moveableObject.GetObjectPosition();}
protected int GetStep()
{
if (_state != Status.InProgress)
{
return Integer.parseInt(null);
}
return _moveableObject.GetStep();
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract boolean IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private boolean MoveTo(Direction directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject.CheckCanMove(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
else {
return false;
}
}
}

42
DopClassRect.java Normal file
View File

@ -0,0 +1,42 @@
import java.awt.*;
public class DopClassRect implements IDop{
public int numOfWheels;
public DopClassRect(int num){
numOfWheels = num;
}
public void setNumOfWheels(String num){
switch (Integer.valueOf(num)){
case 2:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
break;
case 3:
numOfWheels = NumberOfWheelsEnum.wheel_3.value;
break;
case 4:
numOfWheels = NumberOfWheelsEnum.wheel_4.value;
break;
default:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
}
}
public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) {
g.setColor(c);
System.out.println("WHITE");
if (numOfWheels >= 2) {
g.fillRect(_startPosX, _startPosY + 35 + 10, 15, 15);
g.fillRect(_startPosX + 95, _startPosY + 35 + 10, 15, 15);
} if (numOfWheels >= 3) {
g.fillRect(_startPosX, _startPosY + 35 + 10, 15, 15);
g.fillRect(_startPosX + 15, _startPosY + 35 + 10, 15, 15);
} if (numOfWheels >= 4) {
g.fillRect(_startPosX + 95 - 20, _startPosY + 35 + 10, 15, 15);
}
}
}

39
DopClassTriangle.java Normal file
View File

@ -0,0 +1,39 @@
import java.awt.*;
public class DopClassTriangle implements IDop{
public int numOfWheels;
public DopClassTriangle(int num){
numOfWheels = num;
}
public void setNumOfWheels(String num){
switch (Integer.valueOf(num)){
case 2:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
break;
case 3:
numOfWheels = NumberOfWheelsEnum.wheel_3.value;
break;
case 4:
numOfWheels = NumberOfWheelsEnum.wheel_4.value;
break;
default:
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
}
}
public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) {
g.setColor(c);
if (numOfWheels >= 2) {
g.fillPolygon(new int[]{_startPosX, _startPosX + 15/2, _startPosX + 15}, new int[]{_startPosY + 35 + 10, _startPosY + 35 + 10 + 15/2, _startPosY + 35 + 10}, 3);
g.fillPolygon( new int[]{_startPosX + 95, _startPosX + 95 + 15/2, _startPosX + 95 + 15}, new int[]{_startPosY + 35 + 10, _startPosY + 35 + 10 + 15/2, _startPosY + 35 + 10}, 3);
} if (numOfWheels >= 3) {
g.fillPolygon(new int[]{_startPosX + 15, _startPosX + 15 + 15/2, _startPosX + 15 + 15}, new int[]{_startPosY + 35 + 10, _startPosY + 35 + 10 + 15/2, _startPosY + 35 + 10}, 3);
} if (numOfWheels >= 4) {
g.fillPolygon(new int[]{_startPosX + 95 - 20, _startPosX + 95 - 20 + 15/2, _startPosX + 95 - 20 + 15}, new int[]{_startPosY + 35 + 10, _startPosY + 35 + 10 + 15/2, _startPosY + 35 + 10}, 3);
}
}
}

View File

@ -0,0 +1,77 @@
import java.awt.*;
public class DrawingAdvancedCruiser extends DrawingCruiser{
EntityAdvancedCruiser EntityCar;
public DrawingAdvancedCruiser(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean tent, boolean ornamentWheels, int width, int height) {
super(speed, weight, bodyColor, width, height, 110, 60);
{
EntityCar = new EntityAdvancedCruiser(speed, weight, bodyColor, additionalColor, bodyKit, tent);
}
}
public DrawingAdvancedCruiser(EntityAdvancedCruiser entityDumpTruck, IDop wheels)
{
super(entityDumpTruck, wheels);
{
EntityCar = entityDumpTruck;
this.wheels = wheels;
}
}
public EntityAdvancedCruiser getEntityDumpTruck(){
return EntityCar;
}
public void DrawTransport(Graphics g)
{
EntityAdvancedCruiser dumpTruck;
if (EntityCar == null )
{
return;
}
Color addBrush = EntityCar.AdditionalColor;
Color brush = EntityCar.BodyColor;
super.DrawTransport(g);
if (EntityCar.HelicopterPad)
{
g.setColor(addBrush);
int[] pointsX = new int[3];
int[] pointsY = new int[3];
pointsX[0] = _startPosX; pointsY[0] = _startPosY + 35;
pointsX[1] = _startPosX + 85; pointsY[1] = _startPosY;
pointsX[2] = _startPosX + 85; pointsY[2] = _startPosY + 35;
g.fillPolygon(pointsX, pointsY, 3);
}
if (EntityCar.Coating)
{
g.setColor(addBrush);
int[] pointsX = new int[4];
int[] pointsY = new int[4];
pointsX[0] = _startPosX; pointsY[0] = _startPosY + 35;
pointsX[1] = _startPosX; pointsY[1] = _startPosY;
pointsX[2] = _startPosX + 85; pointsY[2] = _startPosY;
pointsX[3] = _startPosX + 85; pointsY[3] = _startPosY + 35;
g.fillPolygon(pointsX, pointsY, 4);
}
if (EntityCar.HelicopterPad && EntityCar.Coating)
{
g.setColor(brush);
int x = _startPosX;
int y = _startPosY - 8;
g.fillRect(_startPosX, _startPosY, 95, 3);
}
if (wheels == null){return;}
wheels.drawWheels(g, _startPosX, _startPosY, brush);
}
}

170
DrawingCruiser.java Normal file
View File

@ -0,0 +1,170 @@
import java.awt.*;
public class DrawingCruiser {
public EntityCruiser EntityCruiser;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private int _carWidth = 110;
private int _carHeight = 60;
/// <summary>
/// РљРѕРѕСЂРґРёРЅР°СР° X объекСР°
/// </summary>
public int GetPosX(){return _startPosX;};
/// <summary>
/// РљРѕРѕСЂРґРёРЅР°СР° Y объекСР°
/// </summary>
public int GetPosY(){return _startPosY;}
/// <summary>
/// РЁРёСЂРёРЅР° объекСР°
/// </summary>
public int GetWidth(){return _carWidth;};
/// <summary>
/// РССЃРѕСР° объекСР°
/// </summary>
public int GetHeight(){return _carHeight;}
public IDop wheels;
public DrawingCruiser(int speed, double weight, Color bodyColor, int width, int height)
{
if(width < _carWidth || height < _carHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
public EntityCruiser getEntity(){
return EntityCruiser;
}
protected DrawingCruiser(int speed, double weight, Color bodyColor, int
width, int height, int carWidth, int carHeight)
{
if (width <= _carWidth || height <= _carHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_carWidth = carWidth;
_carHeight = carHeight;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
public DrawingCruiser(EntityCruiser entityCar, IDop wheels)
{
EntityCruiser = entityCar;
this.wheels = wheels;
_pictureWidth = 1000;
_pictureHeight = 1000;
_carWidth = 110;
_carHeight = 60;
}
public void SetPosition(int x, int y)
{
if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight)
{
_startPosX = 0;
_startPosY = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction)
{
if (!CanMove(direction) || EntityCruiser == null)
{
return;
}
switch (direction)
{
//влево
case Left:
_startPosX -= (int)EntityCruiser.Step();
break;
//вверС
case Up:
_startPosY -= (int)EntityCruiser.Step();
break;
// вправо
case Right:
_startPosX += (int)EntityCruiser.Step();
break;
//РІРЅРёР·
case Down:
_startPosY += (int)EntityCruiser.Step();
break;
}
}
public boolean CanMove(Direction direction)
{
if (EntityCruiser == null)
{
return false;
}
switch (direction)
{
//влево
case Left:
return (_startPosX - EntityCruiser.Step()) > 0;
//вверС
case Up:
return _startPosY - EntityCruiser.Step() > 0;
// вправо
case Right:
return _startPosX + EntityCruiser.Step() + _carWidth < _pictureWidth;
//РІРЅРёР·
case Down:
return _startPosY + EntityCruiser.Step() + _carHeight < _pictureHeight;
default:
return false;
}
}
public void DrawTransport(Graphics g)
{
if (EntityCruiser == null)
{
return;
}
g.setColor(EntityCruiser.BodyColor);
//граниСС Р°РІСомобиля
g.fillRect(_startPosX, _startPosY + 35, 110, 10);
g.fillRect( _startPosX + 85, _startPosY, 25, 35);
/*g.fillOval( _startPosX, _startPosY + 35 + 10, 15, 15);
g.fillOval( _startPosX + 15, _startPosY + 35 + 10, 15, 15);
g.fillOval( _startPosX + 95, _startPosY + 35 + 10, 15, 15);*/
if (wheels == null){return;}
wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor);
}
public IMoveableObject GetMoveableObject(){return new DrawingObjectCruiser(this);}
}

29
DrawingObjectCruiser.java Normal file
View File

@ -0,0 +1,29 @@
public class DrawingObjectCruiser implements IMoveableObject {
private DrawingCruiser _drawningCruiser = null;
public DrawingObjectCruiser(DrawingCruiser drawningCruiser) {
_drawningCruiser = drawningCruiser;
}
public ObjectParameters GetObjectPosition() {
if (_drawningCruiser == null || _drawningCruiser.EntityCruiser == null) {
return null;
}
return new ObjectParameters(_drawningCruiser.GetPosX(), _drawningCruiser.GetPosY(), _drawningCruiser.GetWidth(), _drawningCruiser.GetHeight());
}
public int GetStep() {
return (int) (_drawningCruiser.EntityCruiser.Step());
}
public boolean CheckCanMove(Direction direction) {
if (_drawningCruiser.CanMove(direction)){
return true;
}
return false;
}
public void MoveObject(Direction direction) {
_drawningCruiser.MoveTransport(direction);
}
}

View File

@ -1,108 +0,0 @@
import java.awt.*;
public class DrawningCruiser {
public EntityCruiser EntityCruiser;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
final private int _cruiserWidth = 110;
final private int _cruiserHeight = 60;
private NumberOfWheels wheels = new NumberOfWheels();
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean helicopterPad, boolean coating, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
EntityCruiser = new EntityCruiser();
EntityCruiser.Init(speed, weight, bodyColor, additionalColor, helicopterPad, coating);
return true;
}
public void SetPosition(int x, int y)
{
if (EntityCruiser == null) {
return;
}
_startPosX = x;
_startPosY = y;
if (x + _cruiserWidth <= _pictureWidth || y + _cruiserHeight <= _pictureHeight) {
_startPosX = 0;
_startPosY = 0;
}
}
public void MoveTransport(Direction direction)
{
if (EntityCruiser == null)
{
return;
}
switch (direction)
{
case Left:
if (_startPosX - EntityCruiser.Step() > 0)
{
_startPosX -= (int)EntityCruiser.Step();
}
break;
case Up:
if (_startPosY - EntityCruiser.Step() > 0)
{
_startPosY -= (int)EntityCruiser.Step();
}
break;
case Right:
if (_startPosX + EntityCruiser.Step() + _cruiserWidth < _pictureWidth)
{
_startPosX += (int)EntityCruiser.Step();
}
break;
case Down:
if (_startPosY + EntityCruiser.Step() + _cruiserHeight < _pictureHeight)
{
_startPosY += (int)EntityCruiser.Step();
}
break;
}
}
public void setWheels(int num){
wheels.setNumOfWheels(Integer.toString(num));
}
public void DrawTransport(Graphics g)
{
if (EntityCruiser == null)
{
return;
}
g.setColor(EntityCruiser.BodyColor());
g.drawRect( _startPosX + 9, _startPosY + 15, 10, 30);
g.drawRect( _startPosX + 90, _startPosY + 15, 10,
30);
g.drawRect( _startPosX + 20, _startPosY + 4, 70, 52);
g.fillRect( _startPosX + 10, _startPosY + 15, 10, 30);
g.fillRect( _startPosX + 90, _startPosY + 15, 10, 30);
g.fillRect( _startPosX + 20, _startPosY + 5, 70, 50);
Point[] points1 = new Point[3];
points1[0] = new Point(_startPosX + 100, _startPosY + 5);
points1[1] = new Point(_startPosX + 100, _startPosY + 55);
points1[2] = new Point(_startPosX + 150, _startPosY + 30);
g.fillPolygon(new int[] {points1[0].x, points1[1].x, points1[2].x},
new int[] {points1[0].y, points1[1].y, points1[2].y}, 3);
g.fillRect( _startPosX + 5, _startPosY + 15, 10, 10);
g.fillRect( _startPosX + 5, _startPosY + 35, 10, 10);
if (EntityCruiser.HelicopterPad())
{
g.setColor(EntityCruiser.AdditionalColor());
g.drawRect(_startPosX + 35,
_startPosY + 23, 15, 15);
g.drawRect(_startPosX + 50,
_startPosY + 19, 30, 25);
}
if (EntityCruiser.Coating())
{
g.setColor(EntityCruiser.AdditionalColor());
g.fillRect(_startPosX + 35,
_startPosY + 23, 15, 15);
g.fillRect(_startPosX + 50,
_startPosY + 19, 30, 25);
}
wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor());
}
}

View File

@ -0,0 +1,31 @@
import java.awt.*;
public class EntityAdvancedCruiser extends EntityCruiser{
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor;
/// <summary>
/// Признак (опция) наличия кузова
/// </summary>
public boolean HelicopterPad ;
/// <summary>
/// Признак (опция) наличия tent
/// </summary>
public boolean Coating ;
public EntityAdvancedCruiser(int speed, double weight, Color bodyColor, Color
additionalColor, boolean helicopterPad, boolean coating)
{
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
HelicopterPad = helicopterPad;
Coating = coating;
}
}

View File

@ -1,47 +1,16 @@
import java.awt.*;
public class EntityCruiser {
private int Speed;
private double Weight;
private Color BodyColor;
private Color AdditionalColor;
private boolean HelicopterPad;
private boolean Coating;
private double Step;
public int Speed() {
return Speed;
}
public double Weight() {
return Weight;
}
public Color BodyColor() {
return BodyColor;
}
public Color AdditionalColor() {
return AdditionalColor;
}
public boolean HelicopterPad() {
return HelicopterPad;
}
public boolean Coating() {
return Coating;
}
public double Step() {
return Step;
}
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean helicopterPad, boolean coating)
public int Speed;
public double Weight;
public Color BodyColor;
public double Step() {return (double)Speed * 100 / Weight;};
public boolean OrnamentWheels;
public EntityCruiser(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
HelicopterPad = helicopterPad;
Coating = coating;
Step = Speed * 100 / Weight;
}
}

View File

@ -6,39 +6,164 @@ import java.awt.event.ActionListener;
import java.util.Random;
public class GameFrame extends JFrame {
GameFrame() {
this.setSize(710, 535);
this.setTitle("Cruiser");
this.setTitle("DumpTruck");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
GamePanel panel = new GamePanel();
this.add(panel);
this.setVisible(true);
}
private boolean intToBoolean(int input) {
return input != 0;
}
public class GamePanel extends JPanel implements ActionListener {
public class GamePanel extends JPanel {
static final int SCREEN_W = 700;
static final int SCREEN_H = 500;
int xx = 0;
int yy = 0;
private DrawningCruiser _drawningCruiser;
private DrawingAdvancedCruiser _drawningDumpTruck;
private DrawingCruiser _drawningCar;
private AbstractStrategy _abstractStrategy;
GamePanel() {
this.setLayout(null);
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
GridBagConstraints layers = new GridBagConstraints();
JButton button = new JButton("Создать") ;
button.setBounds(20, 390, 90, 30);
this.add(button);
button.addActionListener(this);
JButton buttonCar = new JButton("Создать грузовик");
buttonCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_drawningCar = new DrawingCruiser(random.nextInt(100, 300),
random.nextInt(1000, 3000),
Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)),
GamePanel.SCREEN_W, GamePanel.SCREEN_H);
_drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10,
100));
int ornament = random.nextInt(1, 4);
switch (ornament){
case 1:
_drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5));
break;
case 2:
_drawningCar.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCar.wheels = new DopClassTriangle(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonCar.setBounds(20 + 150, 390, 120, 30);
this.add(buttonCar);
JButton buttonDumpCar = new JButton("Создать самосвал");
buttonDumpCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
System.out.println("F");
_drawningCar = new DrawingAdvancedCruiser(random.nextInt(100, 300),
random.nextInt(1000, 3000),
Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)), Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)), random.nextBoolean(),random.nextBoolean(),random.nextBoolean(),
GamePanel.SCREEN_W, GamePanel.SCREEN_H); //TODO
_drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10,
100));
int ornament = random.nextInt(1, 4);
switch (ornament){
case 1:
_drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5));
break;
case 2:
_drawningCar.wheels = new DopClassRect(random.nextInt(2, 5));
break;
default:
_drawningCar.wheels = new DopClassTriangle(random.nextInt(2, 5));
break;
}
repaint();
}
});
buttonDumpCar.setBounds( 20, 390, 120, 30);
this.add(buttonDumpCar);
JTextField textStrategy = new JTextField();
textStrategy.setBounds(550, 20, 120, 30);
this.add(textStrategy);
JButton buttonStep = new JButton("Шаг");
buttonStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawningCar == null)
{
return;
}
if (textStrategy.getText() != null)
{
switch(textStrategy.getText())
{
case "center":
_abstractStrategy = new MoveToCenter();
break;
case "border":
_abstractStrategy = new MoveToBorder();
break;
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawingObjectCruiser(_drawningCar), SCREEN_W, SCREEN_H);
//textStrategy.setText();
}
if (_abstractStrategy == null)
{
return;
}
System.out.println("step");
_abstractStrategy.MakeStep();
repaint();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
//comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
});
buttonStep.setBounds(550, 60, 70, 30);
this.add(buttonStep);
JPanel panel = new JPanel(new GridBagLayout());
JButton up = new BasicArrowButton(BasicArrowButton.NORTH);
up.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Up);
_drawningCar.MoveTransport(Direction.Up);
repaint();
}
});
@ -46,7 +171,7 @@ public class GameFrame extends JFrame {
left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Left);
_drawningCar.MoveTransport(Direction.Left);
repaint();
}
});
@ -54,7 +179,7 @@ public class GameFrame extends JFrame {
down.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Down);
_drawningCar.MoveTransport(Direction.Down);
repaint();
}
});
@ -62,45 +187,53 @@ public class GameFrame extends JFrame {
right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningCruiser.MoveTransport(Direction.Right);
_drawningCar.MoveTransport(Direction.Right);
repaint();
}
});
up.setBounds(570, 350, 30, 30);
this.add(up);
down.setBounds(570, 350 + 40, 30, 30);
this.add(down);
left.setBounds(570 - 40, 350 + 40, 30, 30);
this.add(left);
right.setBounds(570 + 40, 350 + 40, 30, 30);
this.add(right);
}
void setTruck(DrawingAdvancedCruiser _truck) {
_drawningDumpTruck = _truck;
}
private void Draw()
{
if (_drawningCar == null)
{
return;
}
Graphics gr =new DebugGraphics();
_drawningCar.DrawTransport(gr);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g){
if (_drawningCruiser != null){
_drawningCruiser.DrawTransport(g);
public void draw(Graphics g) {
if (_drawningCar != null) {
_drawningCar.DrawTransport(g);
}
}
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_drawningCruiser = new DrawningCruiser();
_drawningCruiser.Init(random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
intToBoolean(random.nextInt(0, 2)),
intToBoolean(random.nextInt(0, 2)),
GamePanel.SCREEN_W, GamePanel.SCREEN_H);
_drawningCruiser.SetPosition(random.nextInt(10, 100),
random.nextInt(10, 100));
_drawningCruiser.setWheels(random.nextInt(1, 5));
repaint();
};
}
}
}

7
IDop.java Normal file
View File

@ -0,0 +1,7 @@
import java.awt.*;
public interface IDop {
public void setNumOfWheels(String num);
public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c);
}

17
IMoveableObject.java Normal file
View File

@ -0,0 +1,17 @@
public interface IMoveableObject {
/// <summary>
/// Получение координаты X объекта
/// </summary>
public ObjectParameters GetObjectPosition();
/// <summary>
/// Шаг объекта
/// </summary>
public int GetStep();
boolean CheckCanMove(Direction direction);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(Direction direction);
}

37
MoveToBorder.java Normal file
View File

@ -0,0 +1,37 @@
public class MoveToBorder extends AbstractStrategy {
protected boolean IsTargetDestinaion()
{
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;
}
var diffX = FieldWidth - objParams.ObjectMiddleHorizontal();
if (Math.abs(diffX) > GetStep()) {
MoveRight();
}
var diffY = FieldHeight - objParams.ObjectMiddleVertical();
if (Math.abs(diffY) > GetStep()) {
MoveDown();
}
}
}

44
MoveToCenter.java Normal file
View File

@ -0,0 +1,44 @@
public class MoveToCenter extends AbstractStrategy {
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return (objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2);
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0) {
MoveLeft();
}
else {
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
}
else {
MoveDown();
}
}
}
}

View File

@ -1,6 +1,10 @@
import java.awt.*;
public class NumberOfWheels {
public class NumberOfWheels implements IDop{
public int numOfWheels;
public NumberOfWheels(int num){
numOfWheels = num;
}
public void setNumOfWheels(String num){
switch (Integer.valueOf(num)){
case 2:
@ -16,20 +20,16 @@ public class NumberOfWheels {
numOfWheels = NumberOfWheelsEnum.wheel_2.value;
}
}
public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) {
g.setColor(Color.black);
if (numOfWheels == 2) {
g.fillOval(_startPosX+20, _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 45, _startPosY + 20, 15, 15);
} else if (numOfWheels == 3) {
g.fillOval(_startPosX, _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 25, _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 55, _startPosY + 20, 15, 15);
} else {
g.fillOval(_startPosX, _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 15, _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 65 , _startPosY + 20, 15, 15);
g.fillOval(_startPosX + 85, _startPosY + 20, 15, 15);
g.setColor(c);
if (numOfWheels >= 2) {
g.fillOval(_startPosX, _startPosY + 35 + 10, 15, 15);
g.fillOval(_startPosX + 95, _startPosY + 35 + 10, 15, 15);
} if (numOfWheels >= 3) {
g.fillOval(_startPosX + 15, _startPosY + 35 + 10, 15, 15);
} if(numOfWheels >= 4){
g.fillOval(_startPosX + 95 - 20, _startPosY + 35 + 10, 15, 15);
}
}
}
}

View File

@ -3,6 +3,7 @@ public enum NumberOfWheelsEnum {
wheel_3(3),
wheel_4(4);
public int value;
NumberOfWheelsEnum(int value) {
this.value = value;
}

49
ObjectParameters.java Normal file
View File

@ -0,0 +1,49 @@
public class ObjectParameters {
private final int _x;
private final int _y;
private final int _width;
private final int _height;
/// <summary>
/// Левая граница
/// </summary>
public int LeftBorder(){return _x;}
/// <summary>
/// Верхняя граница
/// </summary>
public int TopBorder(){return _y;}
/// <summary>
/// Правая граница
/// </summary>
public int RightBorder(){return _x + _width;}
/// <summary>
/// Нижняя граница
/// </summary>
public int DownBorder(){return _y + _height;}
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleHorizontal(){return _x + _width / 2;}
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleVertical(){return _y + _height / 2;}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}

8
Status.java Normal file
View File

@ -0,0 +1,8 @@
public enum Status {
NotInit,
InProgress,
Finish
}