Compare commits

..

No commits in common. "Lab_2" and "main" have entirely different histories.
Lab_2 ... main

23 changed files with 0 additions and 1023 deletions

BIN
Down.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 562 B

BIN
Left.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 548 B

BIN
Right.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 552 B

View File

@ -1,95 +0,0 @@
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;
}
}

View File

@ -1,7 +0,0 @@
package Trolleybus;
public enum CntOfDoors {
Three,
Four,
Five
}

View File

@ -1,12 +0,0 @@
package Trolleybus;
public enum DirectionType {
Up (1),
Down (2),
Right (3),
Left (4);
private int Direction;
DirectionType(int direction) {
Direction = direction;
}
}

View File

@ -1,176 +0,0 @@
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);
}
}

View File

@ -1,56 +0,0 @@
package Trolleybus;
import java.awt.*;
public class DrawingDoors 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.drawRect(_startPosX + 2, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 67, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 132, _startPosY + 50, 16, 30);
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.drawRect(_startPosX + 2, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 48, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 86, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 132, _startPosY + 50, 16, 30);
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.drawRect(_startPosX + 2, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 48, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 67, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 86, _startPosY + 50, 16, 30);
g.drawRect(_startPosX + 132, _startPosY + 50, 16, 30);
g.setColor(Color.CYAN);
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
break;
}
}
}

View File

@ -1,37 +0,0 @@
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);
}
}

View File

@ -1,59 +0,0 @@
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;
}
}
}

View File

@ -1,83 +0,0 @@
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;
}
}
}

View File

@ -1,40 +0,0 @@
package Trolleybus;
import java.awt.*;
public class DrawingTrolleybus extends DrawingBus{
// Конструктор
public DrawingTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean batteries, int width, int height)
{
super(speed, weight, bodyColor, width, height, 150, 95);
if (EntityBus != null)
{
EntityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, horns, batteries);
}
}
// Прорисовка объекта
@Override
public void DrawTransport(Graphics g)
{
if (!(EntityBus instanceof EntityTrolleybus trolleybus))
{
return;
}
super.DrawTransport(g);
Graphics2D g2d = (Graphics2D)g;
//Опциональные "рога"
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 (trolleybus.Batteries)
{
int[] xOfBatteries = {_startPosX + 70, _startPosX + 70, _startPosX + 100, _startPosX + 110};
int[] yOfBatteries = {_startPosY + 30, _startPosY + 25, _startPosY + 25, _startPosY + 30};
g2d.fillPolygon(xOfBatteries, yOfBatteries, 4);
}
}
}

View File

@ -1,29 +0,0 @@
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;
}
}

View File

@ -1,29 +0,0 @@
package Trolleybus;
import java.awt.*;
public class EntityTrolleybus extends EntityBus{
// Дополнительный цвет (для опциональных элементов)
public Color AdditionalColor;
// Замена свойства
public Color getAdditionalColor() {return AdditionalColor;}
private void setAdditionalColor(Color additionalColor) {AdditionalColor = additionalColor;}
// Признак (опция) наличия "рогов"
public boolean Horns;
// Замена свойства
public boolean getHorns() {return Horns;}
private void setHorns(boolean horns) {Horns = horns;}
// Признак (опция) наличия отсека под электрические батареи
public boolean Batteries;
// Замена свойства
public boolean getBatteries() {return Batteries;}
private void setBatteries(boolean batteries) {Batteries = batteries;}
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns ,boolean batteries) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Horns = horns;
Batteries = batteries;
}
}

View File

@ -1,219 +0,0 @@
package Trolleybus;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormTrolleybus{
private DrawingBus _drawingBus;
private AbstractStrategy _abstractStrategy;
private JFrame frameTrolleybus;
private JPanel panelTrolleybus;
private JButton buttonCreate, buttonCreateTrolleybus, buttonUp, buttonDown, buttonRight, buttonLeft, buttonStep;
private JComboBox comboBoxStrategy;
public FormTrolleybus(){
//Само окно
frameTrolleybus = new JFrame();
frameTrolleybus.setLayout(new BorderLayout());
frameTrolleybus.setSize(900, 500);
frameTrolleybus.setTitle("Троллейбус");
frameTrolleybus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Панель, на которую добавляю всё
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"));
buttonUp.setToolTipText("buttonUp");
//Кнопка вниз
buttonDown = new JButton();
buttonDown.setIcon(new ImageIcon("Down.png"));
buttonDown.setToolTipText("buttonDown");
//Кнопка вправо
buttonRight = new JButton();
buttonRight.setIcon(new ImageIcon("Right.png"));
buttonRight.setToolTipText("buttonRight");
//Кнопка влево
buttonLeft = new JButton();
buttonLeft.setIcon(new ImageIcon("Left.png"));
buttonLeft.setToolTipText("buttonLeft");
//Кнопка шага
buttonStep = new JButton("Шаг");
//Размеры, позиция кнопок
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() {
public void actionPerformed(ActionEvent e) {
ButtonCreate_Click(e);
}
});
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);
}
});
buttonDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonMove_Click(buttonDown, e);
}
});
buttonRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonMove_Click(buttonRight, e);
}
});
buttonLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonMove_Click(buttonLeft, e);
}
});
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 (_drawingBus == null) {
return;
}
Graphics g = panelTrolleybus.getGraphics();
// Очистка перед перерисовкой
panelTrolleybus.paint(g);
_drawingBus.DrawTransport(g);
}
private void ButtonCreate_Click(ActionEvent e) {
Random random = new Random();
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 (_drawingBus == null)
{
return;
}
JButton info = (JButton)e.getSource();
String name = info.getToolTipText();
switch (name)
{
case "buttonUp":
_drawingBus.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawingBus.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawingBus.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_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;
}
}
}

View File

@ -1,8 +0,0 @@
package Trolleybus;
import java.awt.*;
public interface IDrawingDoors {
public void SetCntOfDoors(int cnt);
public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY);
}

View File

@ -1,13 +0,0 @@
package Trolleybus;
// Интерфейс для работы с перемещаемым объектом
public interface IMoveableObject {
// Получение координаты X объекта
public ObjectParameters GetObjectPosition();
// Шаг объекта
public int GetStep();
// Проверка, можно ли переместиться по нужному направлению
boolean CheckCanMove(DirectionType direction);
// Изменение направления пермещения объекта
void MoveObject(DirectionType direction);
}

View File

@ -1,7 +0,0 @@
package Trolleybus;
public class Main {
public static void main(String[] args) {
new FormTrolleybus();
}
}

View File

@ -1,50 +0,0 @@
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();
}
}
}
}

View File

@ -1,50 +0,0 @@
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();
}
}
}
}

View File

@ -1,46 +0,0 @@
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;
}
}

View File

@ -1,7 +0,0 @@
package Trolleybus;
public enum Status {
NotInit,
InProgress,
Finish
}

BIN
Up.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 549 B