Залил лаб2
This commit is contained in:
parent
f681dfddc4
commit
07e04299a6
95
Trolleybus/AbstractStrategy.java
Normal file
95
Trolleybus/AbstractStrategy.java
Normal file
@ -0,0 +1,95 @@
|
||||
package Trolleybus;
|
||||
|
||||
// Класс-стратегия перемещения объекта
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _moveableObject;
|
||||
|
||||
private Status _state = Status.NotInit;
|
||||
|
||||
private int FieldWidth;
|
||||
|
||||
protected int FieldWidth() {
|
||||
return FieldWidth;
|
||||
}
|
||||
|
||||
private int FieldHeight;
|
||||
|
||||
protected int FieldHeight() {
|
||||
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;
|
||||
_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 ObjectParameters GetObjectParameters(){
|
||||
if (_moveableObject != null) {
|
||||
return _moveableObject.GetObjectPosition();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected Integer GetStep() {
|
||||
if (_state != Status.InProgress) {
|
||||
return null;
|
||||
}
|
||||
return _moveableObject.GetStep();
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
protected abstract boolean IsTargetDestination();
|
||||
|
||||
private boolean MoveTo(DirectionType directionType) {
|
||||
if (_state != Status.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject.CheckCanMove(directionType)) {
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
176
Trolleybus/DrawingBus.java
Normal file
176
Trolleybus/DrawingBus.java
Normal file
@ -0,0 +1,176 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingBus {
|
||||
public EntityBus EntityBus;
|
||||
public EntityBus getEntityBus() {return EntityBus;}
|
||||
private void setEntityBus(EntityBus entityBus) {EntityBus = entityBus;}
|
||||
public IDrawingDoors Doors;
|
||||
// Ширина окна
|
||||
private int _pictureWidth;
|
||||
// Высота окна
|
||||
private int _pictureHeight;
|
||||
// Левая координата прорисовки автобуса
|
||||
protected int _startPosX;
|
||||
// Верхняя координата прорисовки автобуса
|
||||
protected int _startPosY;
|
||||
// Ширина прорисовки автобуса
|
||||
protected int _busWidth = 150;
|
||||
// Высота прорисовки автобуса
|
||||
protected int _busHeight = 95;
|
||||
|
||||
public int GetPosX() {
|
||||
return _startPosX;
|
||||
}
|
||||
|
||||
public int GetPosY() {
|
||||
return _startPosY;
|
||||
}
|
||||
|
||||
public int GetWidth() {
|
||||
return _busWidth;
|
||||
}
|
||||
|
||||
public int GetHeight() {
|
||||
return _busHeight;
|
||||
}
|
||||
|
||||
public DrawingBus(int speed, double weight, Color bodyColor, int width, int height) {
|
||||
if (width < _busWidth || height < _busHeight) {
|
||||
return;
|
||||
}
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityBus = new EntityBus(speed, weight, bodyColor);
|
||||
Random random = new Random();
|
||||
|
||||
//Форма дверей
|
||||
int shape = random.nextInt(1,4);
|
||||
switch (shape) {
|
||||
case 1:
|
||||
Doors = new DrawingDoors();
|
||||
break;
|
||||
case 2:
|
||||
Doors = new DrawingOvalDoors();
|
||||
break;
|
||||
case 3:
|
||||
Doors = new DrawingTriangleDoors();
|
||||
break;
|
||||
}
|
||||
//Количество дверей
|
||||
Doors.SetCntOfDoors(random.nextInt(3, 6));
|
||||
}
|
||||
protected DrawingBus(int speed, double weight, Color bodyColor, int width, int height, int busWidth, int busHeight) {
|
||||
if (width <= _busWidth || height <= _busHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_busWidth = busWidth;
|
||||
_busHeight = busHeight;
|
||||
EntityBus = new EntityBus(speed, weight, bodyColor);
|
||||
Random random = new Random();
|
||||
|
||||
//Форма дверей
|
||||
int shape = random.nextInt(1,4);
|
||||
switch (shape) {
|
||||
case 1:
|
||||
Doors = new DrawingDoors();
|
||||
break;
|
||||
case 2:
|
||||
Doors = new DrawingOvalDoors();
|
||||
break;
|
||||
case 3:
|
||||
Doors = new DrawingTriangleDoors();
|
||||
break;
|
||||
}
|
||||
//Количество дверей
|
||||
Doors.SetCntOfDoors(random.nextInt(3, 6));
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y){
|
||||
_startPosX = Math.min(Math.max(x, 0), _pictureWidth - _busWidth);
|
||||
_startPosY = Math.min(Math.max(y, 0), _pictureHeight - _busHeight);
|
||||
}
|
||||
public boolean CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityBus == null)
|
||||
return false;
|
||||
boolean can = false;
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
can = _startPosX - EntityBus.Step >= 0;
|
||||
break;
|
||||
case Right:
|
||||
can = _startPosX + EntityBus.Step + _busWidth < _pictureWidth;
|
||||
break;
|
||||
case Down:
|
||||
can = _startPosY + EntityBus.Step + _busHeight < _pictureHeight;
|
||||
break;
|
||||
case Up:
|
||||
can = _startPosY - EntityBus.Step >= 0;
|
||||
break;
|
||||
};
|
||||
return can;
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction){
|
||||
if (!CanMove(direction) || EntityBus == null) {
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
if (_startPosX - EntityBus.Step >= 0) {
|
||||
_startPosX -= (int) EntityBus.Step;
|
||||
}
|
||||
break;
|
||||
case Up:
|
||||
if (_startPosY - EntityBus.Step >= 0) {
|
||||
_startPosY -= (int) EntityBus.Step;
|
||||
}
|
||||
break;
|
||||
case Right:
|
||||
if (_startPosX + EntityBus.Step + _busWidth <= _pictureWidth) {
|
||||
_startPosX += (int) EntityBus.Step;
|
||||
}
|
||||
break;
|
||||
case Down:
|
||||
if (_startPosY + EntityBus.Step + _busHeight <= _pictureHeight) {
|
||||
_startPosY += (int) EntityBus.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g) {
|
||||
|
||||
if (EntityBus == null) {
|
||||
return;
|
||||
}
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
//Корпус
|
||||
g2d.setColor(EntityBus.getBodyColor());
|
||||
g2d.drawLine(_startPosX, _startPosY + 30, _startPosX, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX, _startPosY + 80, _startPosX + 20, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX + 45, _startPosY + 80, _startPosX + 105, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX + 130, _startPosY + 80, _startPosX + 150, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX + 150, _startPosY + 80, _startPosX + 150, _startPosY + 30);
|
||||
g2d.drawLine(_startPosX + 150, _startPosY + 30, _startPosX, _startPosY + 30);
|
||||
//Колёса
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(_startPosX + 20, _startPosY + 70, 25, 25);
|
||||
g2d.drawOval(_startPosX + 105, _startPosY + 70, 25, 25);
|
||||
//Двери
|
||||
Doors.DrawDoors(g2d, EntityBus.getBodyColor(), _startPosX, _startPosY);
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
public class DrawingDoors {
|
||||
public class DrawingDoors implements IDrawingDoors{
|
||||
private CntOfDoors _cntOfDoors;
|
||||
public void SetCntOfDoors(int cnt) {
|
||||
if (cnt <= 3) {
|
||||
|
37
Trolleybus/DrawingObjectBus.java
Normal file
37
Trolleybus/DrawingObjectBus.java
Normal file
@ -0,0 +1,37 @@
|
||||
package Trolleybus;
|
||||
|
||||
public class DrawingObjectBus implements IMoveableObject {
|
||||
private final DrawingBus _drawingBus;
|
||||
|
||||
public DrawingObjectBus(DrawingBus drawingBus){
|
||||
_drawingBus = drawingBus;
|
||||
}
|
||||
|
||||
public ObjectParameters GetObjectPosition(){
|
||||
if (_drawingBus == null || _drawingBus.EntityBus == null) {
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawingBus.GetPosX(), _drawingBus.GetPosY(), _drawingBus.GetWidth(), _drawingBus.GetHeight());
|
||||
}
|
||||
|
||||
public int GetStep(){
|
||||
if (_drawingBus.EntityBus == null) {
|
||||
return 0;
|
||||
}
|
||||
return (int)_drawingBus.EntityBus.Step;
|
||||
}
|
||||
|
||||
public boolean CheckCanMove(DirectionType direction){
|
||||
if (_drawingBus == null) {
|
||||
return false;
|
||||
}
|
||||
return _drawingBus.CanMove(direction);
|
||||
}
|
||||
|
||||
public void MoveObject(DirectionType direction){
|
||||
if (_drawingBus == null) {
|
||||
return;
|
||||
}
|
||||
_drawingBus.MoveTransport(direction);
|
||||
}
|
||||
}
|
59
Trolleybus/DrawingOvalDoors.java
Normal file
59
Trolleybus/DrawingOvalDoors.java
Normal file
@ -0,0 +1,59 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingOvalDoors implements IDrawingDoors {
|
||||
private CntOfDoors _cntOfDoors;
|
||||
public void SetCntOfDoors(int cnt) {
|
||||
if (cnt <= 3) {
|
||||
_cntOfDoors = CntOfDoors.Three;
|
||||
}
|
||||
if (cnt == 4) {
|
||||
_cntOfDoors = CntOfDoors.Four;
|
||||
}
|
||||
if (cnt >= 5) {
|
||||
_cntOfDoors = CntOfDoors.Five;
|
||||
}
|
||||
}
|
||||
public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY) {
|
||||
switch (_cntOfDoors)
|
||||
{
|
||||
case Three:
|
||||
g.setColor(BodyColor);
|
||||
g.drawArc(_startPosX + 2, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 67, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 132, _startPosY + 50, 16, 60, 0, 180);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 22, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 47, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 87, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 112, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Four:
|
||||
g.setColor(BodyColor);
|
||||
g.drawArc(_startPosX + 2, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 48, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 86, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 132, _startPosY + 50, 16, 60, 0, 180);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 67, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Five:
|
||||
g.setColor(BodyColor);
|
||||
g.drawArc(_startPosX + 2, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 48, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 67, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 86, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 132, _startPosY + 50, 16, 60, 0, 180);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
83
Trolleybus/DrawingTriangleDoors.java
Normal file
83
Trolleybus/DrawingTriangleDoors.java
Normal file
@ -0,0 +1,83 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingTriangleDoors implements IDrawingDoors{
|
||||
private CntOfDoors _cntOfDoors;
|
||||
public void SetCntOfDoors(int cnt) {
|
||||
if (cnt <= 3) {
|
||||
_cntOfDoors = CntOfDoors.Three;
|
||||
}
|
||||
if (cnt == 4) {
|
||||
_cntOfDoors = CntOfDoors.Four;
|
||||
}
|
||||
if (cnt >= 5) {
|
||||
_cntOfDoors = CntOfDoors.Five;
|
||||
}
|
||||
}
|
||||
public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY) {
|
||||
switch (_cntOfDoors)
|
||||
{
|
||||
case Three:
|
||||
g.setColor(BodyColor);
|
||||
//Первая дверь
|
||||
g.drawLine(_startPosX + 2, _startPosY + 80, _startPosX + 10, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 10, _startPosY + 50, _startPosX + 18, _startPosY + 80);
|
||||
//Вторая дверь
|
||||
g.drawLine(_startPosX + 67, _startPosY + 80, _startPosX + 75, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 75, _startPosY + 50, _startPosX + 83, _startPosY + 80);
|
||||
//Третья дверь
|
||||
g.drawLine(_startPosX + 132, _startPosY + 80, _startPosX + 140, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 140, _startPosY + 50, _startPosX + 148, _startPosY + 80);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 22, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 47, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 87, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 112, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Four:
|
||||
g.setColor(BodyColor);
|
||||
//Первая дверь
|
||||
g.drawLine(_startPosX + 2, _startPosY + 80, _startPosX + 10, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 10, _startPosY + 50, _startPosX + 18, _startPosY + 80);
|
||||
//Вторая дверь
|
||||
g.drawLine(_startPosX + 48, _startPosY + 80, _startPosX + 56, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 56, _startPosY + 50, _startPosX + 64, _startPosY + 80);
|
||||
//Третья дверь
|
||||
g.drawLine(_startPosX + 86, _startPosY + 80, _startPosX + 94, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 94, _startPosY + 50, _startPosX + 102, _startPosY + 80);
|
||||
//Четвёртая дверь
|
||||
g.drawLine(_startPosX + 132, _startPosY + 80, _startPosX + 140, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 140, _startPosY + 50, _startPosX + 148, _startPosY + 80);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 67, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Five:
|
||||
g.setColor(BodyColor);
|
||||
//Первая дверь
|
||||
g.drawLine(_startPosX + 2, _startPosY + 80, _startPosX + 10, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 10, _startPosY + 50, _startPosX + 18, _startPosY + 80);
|
||||
//Вторая дверь
|
||||
g.drawLine(_startPosX + 48, _startPosY + 80, _startPosX + 56, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 56, _startPosY + 50, _startPosX + 64, _startPosY + 80);
|
||||
//Третья дверь
|
||||
g.drawLine(_startPosX + 67, _startPosY + 80, _startPosX + 75, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 75, _startPosY + 50, _startPosX + 83, _startPosY + 80);
|
||||
//Четвёртая дверь
|
||||
g.drawLine(_startPosX + 86, _startPosY + 80, _startPosX + 94, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 94, _startPosY + 50, _startPosX + 102, _startPosY + 80);
|
||||
//Пятая дверь
|
||||
g.drawLine(_startPosX + 132, _startPosY + 80, _startPosX + 140, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 140, _startPosY + 50, _startPosX + 148, _startPosY + 80);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,126 +1,36 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
public class DrawingTrolleybus {
|
||||
// Класс-сущность
|
||||
public EntityTrolleybus EntityTrolleybus;
|
||||
// Замена свойства
|
||||
public EntityTrolleybus getEntityTrolleybus() {return EntityTrolleybus;}
|
||||
private void setEntityTrolleybus(EntityTrolleybus entityTrolleybus) {EntityTrolleybus = entityTrolleybus;}
|
||||
public DrawingDoors Doors;
|
||||
// Ширина окна
|
||||
private int _pictureWidth;
|
||||
// Высота окна
|
||||
private int _pictureHeight;
|
||||
// Левая координата прорисовки троллейбуса
|
||||
private int _startPosX;
|
||||
// Верхняя координата прорисовки троллейбуса
|
||||
private int _startPosY;
|
||||
// Ширина прорисовки троллейбуса
|
||||
private final int _trolleybusWidth = 150;
|
||||
// Высота прорисовки троллейбуса
|
||||
private final int _trolleybusHeight = 95;
|
||||
// Инициализация свойств
|
||||
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean batteries, int width, int height)
|
||||
{
|
||||
if (width < _trolleybusWidth || height < _trolleybusHeight)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityTrolleybus = new EntityTrolleybus();
|
||||
EntityTrolleybus.Init(speed, weight, bodyColor, additionalColor, horns, batteries);
|
||||
//Кол-во дверей
|
||||
Random random = new Random();
|
||||
Doors = new DrawingDoors();
|
||||
Doors.SetCntOfDoors(random.nextInt(3, 6));
|
||||
return true;
|
||||
}
|
||||
|
||||
// Установка позиции
|
||||
public void SetPosition(int x, int y)
|
||||
public class DrawingTrolleybus extends DrawingBus{
|
||||
// Конструктор
|
||||
public DrawingTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean batteries, int width, int height)
|
||||
{
|
||||
if (x > _pictureWidth || y > _pictureHeight) {
|
||||
return;
|
||||
}
|
||||
_startPosX = Math.min(Math.max(x, 0), _pictureWidth - _trolleybusWidth);
|
||||
_startPosY = Math.min(Math.max(y, 0), _pictureHeight - _trolleybusHeight);
|
||||
}
|
||||
|
||||
// Изменение направления перемещения
|
||||
public void MoveTransport(DirectionType directionType)
|
||||
{
|
||||
if (EntityTrolleybus == null)
|
||||
super(speed, weight, bodyColor, width, height, 150, 95);
|
||||
if (EntityBus != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (directionType)
|
||||
{
|
||||
//влево
|
||||
case Left:
|
||||
if (_startPosX - EntityTrolleybus.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityTrolleybus.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case Up:
|
||||
if (_startPosY - EntityTrolleybus.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityTrolleybus.Step;
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case Right:
|
||||
if (_startPosX + _trolleybusWidth + EntityTrolleybus.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityTrolleybus.Step;
|
||||
}
|
||||
break;
|
||||
// вниз
|
||||
case Down:
|
||||
if (_startPosY + _trolleybusHeight + EntityTrolleybus.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityTrolleybus.Step;
|
||||
}
|
||||
break;
|
||||
EntityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, horns, batteries);
|
||||
}
|
||||
}
|
||||
// Прорисовка объекта
|
||||
@Override
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityTrolleybus == null)
|
||||
if (!(EntityBus instanceof EntityTrolleybus trolleybus))
|
||||
{
|
||||
return;
|
||||
}
|
||||
super.DrawTransport(g);
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
//РИСОВАНИЕ САМОГО ТРРОЛЛЕЙБУСА
|
||||
//Корпус
|
||||
g2d.setColor(EntityTrolleybus.getBodyColor());
|
||||
g2d.drawLine(_startPosX, _startPosY + 30, _startPosX, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX, _startPosY + 80, _startPosX + 20, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX + 45, _startPosY + 80, _startPosX + 105, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX + 130, _startPosY + 80, _startPosX + 150, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX + 150, _startPosY + 80, _startPosX + 150, _startPosY + 30);
|
||||
g2d.drawLine(_startPosX + 150, _startPosY + 30, _startPosX, _startPosY + 30);
|
||||
//Колёса
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(_startPosX + 20, _startPosY + 70, 25, 25);
|
||||
g2d.drawOval(_startPosX + 105, _startPosY + 70, 25, 25);
|
||||
//Двери
|
||||
Doors.DrawDoors(g2d, EntityTrolleybus.getBodyColor(), _startPosX, _startPosY);
|
||||
//Опциональные "рога"
|
||||
g2d.setColor(EntityTrolleybus.getAdditionalColor());
|
||||
if (EntityTrolleybus.Horns)
|
||||
g2d.setColor(trolleybus.getAdditionalColor());
|
||||
if (trolleybus.Horns)
|
||||
{
|
||||
g2d.drawLine(_startPosX + 70, _startPosY + 30, _startPosX + 40, _startPosY);
|
||||
g2d.drawLine(_startPosX + 70, _startPosY + 30, _startPosX + 60, _startPosY);
|
||||
}
|
||||
//Опциональный отсек для батареи
|
||||
if (EntityTrolleybus.Batteries)
|
||||
if (trolleybus.Batteries)
|
||||
{
|
||||
int[] xOfBatteries = {_startPosX + 70, _startPosX + 70, _startPosX + 100, _startPosX + 110};
|
||||
int[] yOfBatteries = {_startPosY + 30, _startPosY + 25, _startPosY + 25, _startPosY + 30};
|
||||
|
29
Trolleybus/EntityBus.java
Normal file
29
Trolleybus/EntityBus.java
Normal file
@ -0,0 +1,29 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityBus {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public double Step;
|
||||
//геттеры
|
||||
public int getSpeed() {
|
||||
return Speed;
|
||||
}
|
||||
public double getWeight() {
|
||||
return Weight;
|
||||
}
|
||||
public Color getBodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
public double getStep() {
|
||||
return Step;
|
||||
}
|
||||
public EntityBus(int speed, double weight, Color bodyColor) {
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
Step = (double) Speed * 100 / Weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
@ -1,25 +1,7 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
public class EntityTrolleybus {
|
||||
//Скорость
|
||||
public int Speed;
|
||||
// Замена свойства
|
||||
public int getSpeed() {return Speed;}
|
||||
private void setSpeed(int speed) {Speed = speed;}
|
||||
|
||||
// Вес
|
||||
public double Weight;
|
||||
// Замена свойства
|
||||
public double getWeight() {return Weight;}
|
||||
private void setWeight(double weight) {Weight = weight;}
|
||||
|
||||
// Основной цвет
|
||||
public Color BodyColor;
|
||||
// Замена свойства
|
||||
public Color getBodyColor() {return BodyColor;}
|
||||
private void setBodyColor(Color bodyColor) {BodyColor = bodyColor;}
|
||||
|
||||
public class EntityTrolleybus extends EntityBus{
|
||||
// Дополнительный цвет (для опциональных элементов)
|
||||
public Color AdditionalColor;
|
||||
// Замена свойства
|
||||
@ -38,16 +20,8 @@ public class EntityTrolleybus {
|
||||
public boolean getBatteries() {return Batteries;}
|
||||
private void setBatteries(boolean batteries) {Batteries = batteries;}
|
||||
|
||||
// Шаг перемещения троллейбуса
|
||||
public double Step;
|
||||
|
||||
// Инициализация полей объекта-класса троллейбуса
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean batteries)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
Step = (double)Speed * 100 / Weight;
|
||||
BodyColor = bodyColor;
|
||||
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns ,boolean batteries) {
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor = additionalColor;
|
||||
Horns = horns;
|
||||
Batteries = batteries;
|
||||
|
@ -7,10 +7,12 @@ import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormTrolleybus{
|
||||
private DrawingTrolleybus _drawingTrolleybus;
|
||||
private DrawingBus _drawingBus;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
private JFrame frameTrolleybus;
|
||||
private JPanel panelTrolleybus;
|
||||
private JButton buttonCreate, buttonUp, buttonDown, buttonRight, buttonLeft;
|
||||
private JButton buttonCreate, buttonCreateTrolleybus, buttonUp, buttonDown, buttonRight, buttonLeft, buttonStep;
|
||||
private JComboBox comboBoxStrategy;
|
||||
|
||||
public FormTrolleybus(){
|
||||
//Само окно
|
||||
@ -24,8 +26,17 @@ public class FormTrolleybus{
|
||||
panelTrolleybus = new JPanel();
|
||||
panelTrolleybus.setLayout(null);
|
||||
|
||||
//Кнопка создания
|
||||
//ComboBox
|
||||
String[] strings = {"В центр", "В край"};
|
||||
comboBoxStrategy = new JComboBox(strings);
|
||||
comboBoxStrategy.setBounds(750, 20, 90, 30);
|
||||
|
||||
//Кнопка создания автобуса
|
||||
buttonCreate = new JButton("Создать");
|
||||
buttonCreate.setToolTipText("buttonCreate");
|
||||
//Кнопка создания троллейбуса
|
||||
buttonCreateTrolleybus = new JButton("Создать троллейбус");
|
||||
buttonCreateTrolleybus.setToolTipText("buttonCreateTrolleybus");
|
||||
//Кнопка вверх
|
||||
buttonUp = new JButton();
|
||||
buttonUp.setIcon(new ImageIcon("Up.png"));
|
||||
@ -42,12 +53,16 @@ public class FormTrolleybus{
|
||||
buttonLeft = new JButton();
|
||||
buttonLeft.setIcon(new ImageIcon("Left.png"));
|
||||
buttonLeft.setToolTipText("buttonLeft");
|
||||
//Кнопка шага
|
||||
buttonStep = new JButton("Шаг");
|
||||
//Размеры, позиция кнопок
|
||||
buttonCreate.setBounds(10,400,90,30);
|
||||
buttonCreate.setBounds(10,400,150,30);
|
||||
buttonCreateTrolleybus.setBounds(170, 400, 150, 30);
|
||||
buttonUp.setBounds(800,380,30,30);
|
||||
buttonDown.setBounds(800,420,30,30);
|
||||
buttonLeft.setBounds(760,420,30,30);
|
||||
buttonRight.setBounds(840,420,30,30);
|
||||
buttonStep.setBounds(750, 70, 90, 30);
|
||||
|
||||
//Добавление листенеров к кнопкам
|
||||
buttonCreate.addActionListener(new ActionListener() {
|
||||
@ -56,6 +71,12 @@ public class FormTrolleybus{
|
||||
}
|
||||
});
|
||||
|
||||
buttonCreateTrolleybus.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonCreate_Click(e);
|
||||
}
|
||||
});
|
||||
|
||||
buttonUp.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonMove_Click(buttonUp, e);
|
||||
@ -80,40 +101,60 @@ public class FormTrolleybus{
|
||||
}
|
||||
});
|
||||
|
||||
buttonStep.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonStep_Click(buttonStep, e);
|
||||
}
|
||||
});
|
||||
|
||||
panelTrolleybus.add(buttonCreate);
|
||||
panelTrolleybus.add(buttonCreateTrolleybus);
|
||||
panelTrolleybus.add(buttonUp);
|
||||
panelTrolleybus.add(buttonDown);
|
||||
panelTrolleybus.add(buttonLeft);
|
||||
panelTrolleybus.add(buttonRight);
|
||||
panelTrolleybus.add(buttonStep);
|
||||
panelTrolleybus.add(comboBoxStrategy);
|
||||
frameTrolleybus.add(panelTrolleybus, BorderLayout.CENTER);
|
||||
|
||||
frameTrolleybus.setVisible(true);
|
||||
}
|
||||
// Метод прорисовки троллейбуса
|
||||
private void Draw(){
|
||||
if (_drawingTrolleybus == null) {
|
||||
if (_drawingBus == null) {
|
||||
return;
|
||||
}
|
||||
Graphics g = panelTrolleybus.getGraphics();
|
||||
// Очистка перед перерисовкой
|
||||
panelTrolleybus.paint(g);
|
||||
_drawingTrolleybus.DrawTransport(g);
|
||||
_drawingBus.DrawTransport(g);
|
||||
}
|
||||
private void ButtonCreate_Click(ActionEvent e) {
|
||||
Random random = new Random();
|
||||
_drawingTrolleybus = new DrawingTrolleybus();
|
||||
_drawingTrolleybus.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)),
|
||||
random.nextBoolean(),
|
||||
random.nextBoolean(),
|
||||
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
||||
_drawingTrolleybus.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
JButton info = (JButton)e.getSource();
|
||||
String name = info.getToolTipText();
|
||||
switch (name) {
|
||||
case "buttonCreate":
|
||||
_drawingBus = new DrawingBus(random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
||||
break;
|
||||
case "buttonCreateTrolleybus":
|
||||
_drawingBus = new DrawingTrolleybus(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)),
|
||||
random.nextBoolean(),
|
||||
random.nextBoolean(),
|
||||
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
||||
break;
|
||||
}
|
||||
_drawingBus.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
}
|
||||
protected void ButtonMove_Click(Object sender, ActionEvent e) {
|
||||
if (_drawingTrolleybus == null)
|
||||
if (_drawingBus == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -122,18 +163,57 @@ public class FormTrolleybus{
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
_drawingTrolleybus.MoveTransport(DirectionType.Up);
|
||||
_drawingBus.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
_drawingTrolleybus.MoveTransport(DirectionType.Down);
|
||||
_drawingBus.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
_drawingTrolleybus.MoveTransport(DirectionType.Left);
|
||||
_drawingBus.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
_drawingTrolleybus.MoveTransport(DirectionType.Right);
|
||||
_drawingBus.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
private void ButtonStep_Click(Object sender, ActionEvent e)
|
||||
{
|
||||
if (_drawingBus == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (comboBoxStrategy.isEnabled())
|
||||
{
|
||||
|
||||
if (comboBoxStrategy.getSelectedIndex() == 0) {
|
||||
_abstractStrategy = new MoveToCenter();
|
||||
}
|
||||
else if (comboBoxStrategy.getSelectedIndex() == 1) {
|
||||
_abstractStrategy = new MoveToBorder();
|
||||
}
|
||||
else {
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new DrawingObjectBus(_drawingBus), panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
8
Trolleybus/IDrawingDoors.java
Normal file
8
Trolleybus/IDrawingDoors.java
Normal file
@ -0,0 +1,8 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingDoors {
|
||||
public void SetCntOfDoors(int cnt);
|
||||
public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY);
|
||||
}
|
13
Trolleybus/IMoveableObject.java
Normal file
13
Trolleybus/IMoveableObject.java
Normal file
@ -0,0 +1,13 @@
|
||||
package Trolleybus;
|
||||
|
||||
// Интерфейс для работы с перемещаемым объектом
|
||||
public interface IMoveableObject {
|
||||
// Получение координаты X объекта
|
||||
public ObjectParameters GetObjectPosition();
|
||||
// Шаг объекта
|
||||
public int GetStep();
|
||||
// Проверка, можно ли переместиться по нужному направлению
|
||||
boolean CheckCanMove(DirectionType direction);
|
||||
// Изменение направления пермещения объекта
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
50
Trolleybus/MoveToBorder.java
Normal file
50
Trolleybus/MoveToBorder.java
Normal file
@ -0,0 +1,50 @@
|
||||
package Trolleybus;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy{
|
||||
@Override
|
||||
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();
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.RightBorder() - FieldWidth();
|
||||
if (Math.abs(diffX) >= GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.DownBorder() - FieldHeight();
|
||||
if (Math.abs(diffY) >= GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
50
Trolleybus/MoveToCenter.java
Normal file
50
Trolleybus/MoveToCenter.java
Normal file
@ -0,0 +1,50 @@
|
||||
package Trolleybus;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal() <= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleVertical() <= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight() / 2;
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth() / 2;
|
||||
if (Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical() - FieldHeight() / 2;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
46
Trolleybus/ObjectParameters.java
Normal file
46
Trolleybus/ObjectParameters.java
Normal file
@ -0,0 +1,46 @@
|
||||
package Trolleybus;
|
||||
|
||||
// Параметры-координаты объекта
|
||||
public class ObjectParameters {
|
||||
private final int _x;
|
||||
private final int _y;
|
||||
private final int _width;
|
||||
private final 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 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 + _width / 2;
|
||||
}
|
||||
|
||||
public int getObjectMiddleVertical() {
|
||||
return _y + _height / 2;
|
||||
}
|
||||
// Конструктор
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
7
Trolleybus/Status.java
Normal file
7
Trolleybus/Status.java
Normal file
@ -0,0 +1,7 @@
|
||||
package Trolleybus;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user