PIbd-21_MasenkinMS_LabWork02

This commit is contained in:
parent 5da451e1b5
commit b664cc97ff
20 changed files with 817 additions and 170 deletions

View File

@ -0,0 +1,86 @@
package AccordionBus;
// Класс-стратегия перемещения объекта
public abstract class AbstractStrategy {
// Перемещаемый объект
private IMoveableObject _moveableObject;
// Статус перемещения
private Status _state = Status.NotInit;
// Ширина поля
protected int FieldWidth;
// Высота поля
protected int 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() { return _moveableObject.GetObjectPosition(); }
// Шаг объекта
protected int GetStep() {
if (_state != Status.InProgress)
{
return 0;
}
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

@ -3,12 +3,12 @@
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
<xy x="20" y="20" width="638" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="b336e" binding="createPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="b336e" binding="createPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false"/>
@ -24,6 +24,14 @@
<text value="Создать автобус-гармошку"/>
</properties>
</component>
<component id="5708d" class="javax.swing.JButton" binding="buttonCreateBus">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать автобус"/>
</properties>
</component>
</children>
</grid>
<vspacer id="b9759">
@ -31,7 +39,7 @@
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="5a846" binding="MovePanel" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="5a846" binding="movePanel" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="6" fill="0" indent="0" use-parent-layout="false"/>
@ -85,6 +93,35 @@
</component>
</children>
</grid>
<grid id="da58b" binding="strategyPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="5" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="2959" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="5" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<vspacer id="5e742">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="8c54d" class="javax.swing.JButton" binding="buttonStep">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -6,19 +6,25 @@ import java.awt.event.ActionListener;
import java.util.Random;
public class AccordionBusForm {
private DrawingAccordionBus drawingAccordionBus;
private DrawingBus drawingBus;
private AbstractStrategy abstractStrategy;
private JPanel pictureBox;
public JPanel getPictureBox() {
return pictureBox;
}
private JPanel createPanel;
private JButton buttonCreateAccordionBus;
private JButton buttonCreate;
private JPanel movePanel;
private JButton buttonLeft;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonUp;
private JPanel MovePanel;
private JPanel strategyPanel;
private JPanel controlPanel;
private JButton buttonCreateBus;
private JComboBox comboBoxStrategy;
private JButton buttonStep;
public AccordionBusForm() {
buttonUp.setName("buttonUp");
@ -26,10 +32,12 @@ public class AccordionBusForm {
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
comboBoxStrategy.addItem("MoveToCenter");
comboBoxStrategy.addItem("MoveToBorder");
buttonCreateAccordionBus.addActionListener(e -> {
Random random = new Random();
drawingAccordionBus = new DrawingAccordionBus();
drawingAccordionBus.Init(random.nextInt(100, 300),
drawingBus = new DrawingAccordionBus(random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
@ -37,8 +45,19 @@ public class AccordionBusForm {
random.nextBoolean(),
pictureBox.getWidth(),
pictureBox.getHeight());
drawingAccordionBus.SetNumDoors(random.nextInt(3, 6));
drawingAccordionBus.SetPosition(random.nextInt(10, 100),
drawingBus.SetPosition(random.nextInt(10, 100),
random.nextInt(10, 100));
Draw();
});
buttonCreateBus.addActionListener(e -> {
Random random = new Random();
drawingBus = new DrawingBus(random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
pictureBox.getWidth(),
pictureBox.getHeight());
drawingBus.SetPosition(random.nextInt(10, 100),
random.nextInt(10, 100));
Draw();
});
@ -47,21 +66,61 @@ public class AccordionBusForm {
String name = ((JButton)e.getSource()).getName();
switch (name) {
case "buttonUp" -> {
drawingAccordionBus.MoveTransport(DirectionType.Up);
drawingBus.MoveTransport(DirectionType.Up);
}
case "buttonDown" -> {
drawingAccordionBus.MoveTransport(DirectionType.Down);
drawingBus.MoveTransport(DirectionType.Down);
}
case "buttonLeft" -> {
drawingAccordionBus.MoveTransport(DirectionType.Left);
drawingBus.MoveTransport(DirectionType.Left);
}
case "buttonRight" -> {
drawingAccordionBus.MoveTransport(DirectionType.Right);
drawingBus.MoveTransport(DirectionType.Right);
}
}
Draw();
};
buttonStep.addActionListener(e -> {
if (drawingBus == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) {
case 0:
abstractStrategy = new MoveToCenter();
break;
case 1:
abstractStrategy = new MoveToBorder();
break;
default:
abstractStrategy = null;
break;
}
if (abstractStrategy == null) {
return;
}
abstractStrategy.SetData(new DrawingObjectBus(drawingBus),
pictureBox.getWidth(),
pictureBox.getHeight());
comboBoxStrategy.setEditable(false);
}
if (abstractStrategy == null) {
return;
}
abstractStrategy.MakeStep();
Draw();
if (abstractStrategy.GetStatus() == Status.Finish) {
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
});
buttonUp.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
@ -69,12 +128,12 @@ public class AccordionBusForm {
}
public void Draw() {
if (drawingAccordionBus.AccordionBus == null) {
if (drawingBus == null) {
return;
}
Graphics g = pictureBox.getGraphics();
pictureBox.paint(g);
drawingAccordionBus.DrawTransport(g);
drawingBus.DrawTransport(g);
}
}

View File

@ -1,145 +1,38 @@
package AccordionBus;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
// Класс, отвечающий за прорисовку объекта
public class DrawingAccordionBus {
// Класс-сущность
public EntityAccordionBus AccordionBus;
// Прорисовка дверей
public DrawingDoors drawingDoors;
// Ширина окна
private int _pictureWidth;
// Высота окна
private int _pictureHeight;
// Левая координата прорисовки автобуса
private int _startPosX;
// Верхняя координата прорисовки автобуса
private int _startPosY;
// Ширина прорисовки автобуса
private int _busWidth = 100;
// Высота прорисовки автобуса
private final int _busHeight = 50;
// Инициализация свойста
public boolean Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean additionalBody, boolean additionalDoor, int width, int height) {
// Проверка на вместимость объекта в размеры картинки
if ((_busWidth >= width) || (_busHeight >= height)) {
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (additionalBody) {
_busWidth = 215;
}
AccordionBus = new EntityAccordionBus();
AccordionBus.Init(speed, weight,
bodyColor, additionalColor,
additionalBody, additionalDoor);
drawingDoors = new DrawingDoors();
return true;
}
// Количество дополнительных дверей
public void SetNumDoors(int numDoors) {
drawingDoors.setNumDoors(numDoors);
}
// Установка позиции
public void SetPosition(int x, int y) {
if (x < 0 || y < 0) {
_startPosX = 0;
_startPosY = 0;
}
else if ((x + _busWidth > _pictureWidth) || (y + _busHeight > _pictureHeight)) {
_startPosX = _pictureWidth - _busWidth;
_startPosY = _pictureHeight - _busHeight;
}
else {
_startPosX = x;
_startPosY = y;
}
}
// Изменение направления движения
public void MoveTransport(DirectionType direction) {
if (AccordionBus == null) {
return;
}
switch (direction) {
case Left:
if (_startPosX - AccordionBus.Step > 0) {
_startPosX -= (int)AccordionBus.Step;
}
break;
case Up:
if (_startPosY - AccordionBus.Step > 0) {
_startPosY -= (int)AccordionBus.Step;
}
break;
case Right:
if (_startPosX + AccordionBus.Step < _pictureWidth - _busWidth) {
_startPosX += (int)AccordionBus.Step;
}
break;
case Down:
if (_startPosY + AccordionBus.Step < _pictureHeight - _busHeight) {
_startPosY += (int)AccordionBus.Step;
}
break;
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
public class DrawingAccordionBus extends DrawingBus {
// Конструктор
public DrawingAccordionBus(int speed, double weight, Color bodyColor, Color additionalColor, boolean additionalBody, boolean additionalDoor, int width, int height) {
super(speed, weight, bodyColor, width, height, 215, 50);
if (EntityBus != null) {
EntityBus = new EntityAccordionBus(speed, weight, bodyColor, additionalColor, additionalBody, additionalDoor);
}
}
// Прорисовка объекта
public void DrawTransport(Graphics g) {
if (AccordionBus == null) {
if (EntityBus == null) {
return;
}
Graphics2D g2d = (Graphics2D)g;
Color bodyColor = AccordionBus.getBodyColor();
Color additionalColor = AccordionBus.getAdditionalColor();
super.DrawTransport(g2d);
Color bodyColor = EntityBus.getBodyColor();
Color additionalColor = ((EntityAccordionBus)EntityBus).getAdditionalColor();
Color blackColor = Color.BLACK;
Color whiteColor = Color.WHITE;
// Граница главного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(3));
g2d.drawRect(_startPosX, _startPosY, 100, 40);
// Кузов главного отсека
g2d.setColor(bodyColor);
g2d.fillRect(_startPosX + 1, _startPosY + 1, 99, 39);
// Окна главного отсека
g2d.setColor(Color.CYAN);
g2d.fillRect(_startPosX, _startPosY + 5, 100, 15);
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(_startPosX, _startPosY + 5, _startPosX + 100, _startPosY + 5);
g2d.drawLine(_startPosX, _startPosY + 20, _startPosX + 100, _startPosY + 20);
// Дверь главного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 10, _startPosY + 10, 15, 30);
g2d.setColor(whiteColor);
g2d.fillRect(_startPosX + 11, _startPosY + 11, 14, 29);
// Дополнительный отсек
if (AccordionBus.isAdditionalBody()) {
if (((EntityAccordionBus)EntityBus).isAdditionalBody()) {
// Граница дополнительного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(3));
@ -158,7 +51,7 @@ public class DrawingAccordionBus {
g2d.drawLine(_startPosX + 115, _startPosY + 20, _startPosX + 215, _startPosY + 20);
// Дверь дополнительного отсека
if (AccordionBus.isAdditionalDoor()) {
if (((EntityAccordionBus)EntityBus).isAdditionalDoor()) {
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 175, _startPosY + 10, 15, 30);
@ -175,7 +68,7 @@ public class DrawingAccordionBus {
}
// Дополнительные двери
drawingDoors.DrawDoors(g2d, AccordionBus, additionalColor, _startPosX, _startPosY);
drawingDoors.DrawDoors(g2d, additionalColor, ((EntityAccordionBus) EntityBus).isAdditionalBody(), _startPosX, _startPosY);
// Колеса главного отсека
g2d.setColor(blackColor);
@ -187,7 +80,7 @@ public class DrawingAccordionBus {
g2d.fillOval(_startPosX + 70, _startPosY + 33, 15, 15);
// Колеса дополнительного отсека
if (AccordionBus.isAdditionalBody()) {
if (((EntityAccordionBus)EntityBus).isAdditionalBody()) {
g2d.setColor(blackColor);
g2d.drawOval(_startPosX + 130, _startPosY + 33, 15, 15);
g2d.drawOval(_startPosX + 185, _startPosY + 33, 15, 15);

View File

@ -0,0 +1,221 @@
package AccordionBus;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
public class DrawingBus {
// Класс-сущность
public EntityBus EntityBus;
// Прорисовка дверей
public IDrawingDoors drawingDoors;
// Ширина окна
private int _pictureWidth;
// Высота окна
private int _pictureHeight;
// Левая координата прорисовки автобуса
protected int _startPosX;
// Координата X объекта
public int GetPosX() { return _startPosX; }
// Верхняя координата прорисовки автобуса
protected int _startPosY;
// Координата Y объекта
public int GetPosY() { return _startPosY; }
// Ширина прорисовки автобуса
private int _busWidth = 100;
// Ширина объекта
public int GetWidth() { return _busWidth; }
// Высота прорисовки автобуса
private int _busHeight = 50;
// Высота объекта
public int GetHeigth() { return _busHeight; }
// Проверка, что объект может перемещаться по указанному направлению
public boolean CanMove(DirectionType direction) {
if (EntityBus == null) {
return false;
}
switch (direction) {
// Влево
case Left:
return _startPosX - EntityBus.Step() > 0;
// Вверх
case Up:
return _startPosY - EntityBus.Step() > 0;
// Вправо
case Right:
return _startPosX + EntityBus.Step() < _pictureWidth - _busWidth;
// Вниз
case Down:
return _startPosY + EntityBus.Step() < _pictureHeight - _busHeight;
default:
return false;
}
}
// Конструктор
public DrawingBus(int speed, int weight, Color bodyColor, int width, int height) {
// Проверка на вместимость объекта в размеры картинки
if ((_busWidth >= width) || (_busHeight >= height)) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityBus = new EntityBus(speed, weight, bodyColor);
Random random = new Random();
switch (random.nextInt(0, 3)) {
case 0:
drawingDoors = new DrawingDoorsTypeOne();
break;
case 1:
drawingDoors = new DrawingDoorsTypeTwo();
break;
case 2:
drawingDoors = new DrawingDoorsTypeThree();
break;
default:
drawingDoors = new DrawingDoorsTypeOne();
break;
}
drawingDoors.setNumDoors(random.nextInt(3, 6));
}
// Конструктор
protected DrawingBus(int speed, double weight, Color bodyColor, int width, int height, int busWidth, int busHeight) {
// Проверка на вместимость объекта в размеры картинки
if ((_busWidth >= width) || (_busHeight >= height)) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
_busWidth = busWidth;
_busHeight = busHeight;
EntityBus = new EntityBus(speed, weight, bodyColor);
Random random = new Random();
switch (random.nextInt(0, 3)) {
case 0:
drawingDoors = new DrawingDoorsTypeOne();
break;
case 1:
drawingDoors = new DrawingDoorsTypeTwo();
break;
case 2:
drawingDoors = new DrawingDoorsTypeThree();
break;
default:
drawingDoors = new DrawingDoorsTypeOne();
break;
}
drawingDoors.setNumDoors(random.nextInt(3, 6));
}
// Установка позиции
public void SetPosition(int x, int y) {
if (x < 0 || y < 0) {
x = 0;
y = 0;
}
else if ((x + _busWidth > _pictureWidth) || (y + _busHeight > _pictureHeight)) {
_startPosX = _pictureWidth - _busWidth;
_startPosY = _pictureHeight - _busHeight;
}
else {
_startPosX = x;
_startPosY = y;
}
}
// Изменение направления перемещения
public void MoveTransport(DirectionType direction) {
if (!CanMove(direction) || EntityBus == null) {
return;
}
switch (direction) {
// Влево
case Left:
_startPosX -= (int)EntityBus.Step();
break;
// Вверх
case Up:
_startPosY -= (int)EntityBus.Step();
break;
// Вправо
case Right:
_startPosX += (int)EntityBus.Step();
break;
// Вниз
case Down:
_startPosY += (int)EntityBus.Step();
break;
}
}
// Прорисовка объекта
public void DrawTransport(Graphics g) {
if (EntityBus == null)
{
return;
}
Graphics2D g2d = (Graphics2D)g;
Color bodyColor = EntityBus.getBodyColor();
Color blackColor = Color.BLACK;
Color whiteColor = Color.WHITE;
// Граница главного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(3));
g2d.drawRect(_startPosX, _startPosY, 100, 40);
// Кузов главного отсека
g2d.setColor(bodyColor);
g2d.fillRect(_startPosX + 1, _startPosY + 1, 99, 39);
// Окна главного отсека
g2d.setColor(Color.CYAN);
g2d.fillRect(_startPosX, _startPosY + 5, 100, 15);
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(_startPosX, _startPosY + 5, _startPosX + 100, _startPosY + 5);
g2d.drawLine(_startPosX, _startPosY + 20, _startPosX + 100, _startPosY + 20);
// Дверь главного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 10, _startPosY + 10, 15, 30);
g2d.setColor(whiteColor);
g2d.fillRect(_startPosX + 11, _startPosY + 11, 14, 29);
// Дополнительные двери
drawingDoors.DrawDoors(g2d, bodyColor, false, _startPosX, _startPosY);
// Колеса главного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(3));
g2d.drawOval(_startPosX + 15, _startPosY + 33, 15, 15);
g2d.drawOval(_startPosX + 70, _startPosY + 33, 15, 15);
g2d.setColor(whiteColor);
g2d.fillOval(_startPosX + 15, _startPosY + 33, 15, 15);
g2d.fillOval(_startPosX + 70, _startPosY + 33, 15, 15);
}
}

View File

@ -2,6 +2,7 @@ package AccordionBus;
import java.awt.*;
// Класс, отвечающий за прорисовку дополнительных дверей
public class DrawingDoors {
private NumDoors numDoors;

View File

@ -0,0 +1,42 @@
package AccordionBus;
import java.awt.*;
// Класс, отвечающий за первый тип орнамента отрисовки дополнительных дверей
public class DrawingDoorsTypeOne implements IDrawingDoors {
private NumDoors numDoors;
public void setNumDoors(int num) {
for (NumDoors temp : numDoors.values()) {
if (temp.getNumDoors() == num) {
numDoors = temp;
return;
}
}
}
public void DrawDoors(Graphics2D g2d, Color color, boolean isAdditionalBody, int _startPosX, int _startPosY) {
int value = numDoors.getNumDoors();
if (value >= 3) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 40, _startPosY + 10, 15, 30);
g2d.setColor(color);
g2d.fillRect(_startPosX + 41, _startPosY + 11, 14, 29);
}
if (value >= 4) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 75, _startPosY + 10, 15, 30);
g2d.setColor(color);
g2d.fillRect(_startPosX + 76, _startPosY + 11, 14, 29);
}
if (value >= 5 && isAdditionalBody) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 145, _startPosY + 10, 15, 30);
g2d.setColor(color);
g2d.fillRect(_startPosX + 146, _startPosY + 11, 14, 29);
}
}
}

View File

@ -0,0 +1,51 @@
package AccordionBus;
import java.awt.*;
// Класс, отвечающий за третий тип орнамента отрисовки дополнительных дверей
public class DrawingDoorsTypeThree implements IDrawingDoors {
private NumDoors numDoors;
public void setNumDoors(int num) {
for (NumDoors temp : numDoors.values()) {
if (temp.getNumDoors() == num) {
numDoors = temp;
return;
}
}
}
public void DrawDoors(Graphics2D g2d, Color color, boolean isAdditionalBody, int _startPosX, int _startPosY) {
int value = numDoors.getNumDoors();
if (value >= 3) {
g2d.setStroke(new BasicStroke(2));
Polygon firstDoor = new Polygon(new int[] { _startPosX + 45, _startPosX + 50, _startPosX + 55, _startPosX + 50, _startPosX + 55, _startPosX + 50, _startPosX + 55, _startPosX + 50, _startPosX + 45, _startPosX + 40, _startPosX + 45, _startPosX + 40, _startPosX + 45,_startPosX + 40 },
new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 15, _startPosY + 20, _startPosY + 25, _startPosY + 30, _startPosY + 35, _startPosY + 40, _startPosY + 40, _startPosY + 35, _startPosY + 30, _startPosY + 25, _startPosY + 20, _startPosY + 15 },
14);
g2d.setColor(color);
g2d.fillPolygon(firstDoor);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(firstDoor);
}
if (value >= 4) {
g2d.setStroke(new BasicStroke(2));
Polygon secondDoor = new Polygon(new int[] { _startPosX + 80, _startPosX + 85, _startPosX + 90, _startPosX + 85, _startPosX + 90, _startPosX + 85, _startPosX + 90, _startPosX + 85, _startPosX + 80, _startPosX + 75, _startPosX + 80, _startPosX + 75, _startPosX + 80, _startPosX + 75 },
new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 15, _startPosY + 20, _startPosY + 25, _startPosY + 30, _startPosY + 35, _startPosY + 40, _startPosY + 40, _startPosY + 35, _startPosY + 30, _startPosY + 25, _startPosY + 20, _startPosY + 15 },
14);
g2d.setColor(color);
g2d.fillPolygon(secondDoor);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(secondDoor);
}
if (value >= 5 && isAdditionalBody) {
g2d.setStroke(new BasicStroke(2));
Polygon thirdDoor = new Polygon(new int[] { _startPosX + 150, _startPosX + 155, _startPosX + 160, _startPosX + 155, _startPosX + 160, _startPosX + 155, _startPosX + 160, _startPosX + 155, _startPosX + 150, _startPosX + 145, _startPosX + 150, _startPosX + 145, _startPosX + 150, _startPosX + 145 },
new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 15, _startPosY + 20, _startPosY + 25, _startPosY + 30, _startPosY + 35, _startPosY + 40, _startPosY + 40, _startPosY + 35, _startPosY + 30, _startPosY + 25, _startPosY + 20, _startPosY + 15 },
14);
g2d.setColor(color);
g2d.fillPolygon(thirdDoor);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(thirdDoor);
}
}
}

View File

@ -0,0 +1,53 @@
package AccordionBus;
import java.awt.*;
// Класс, отвечающий за второй тип орнамента отрисовки дополнительных дверей
public class DrawingDoorsTypeTwo implements IDrawingDoors {
private NumDoors numDoors;
public void setNumDoors(int num) {
for (NumDoors temp : numDoors.values()) {
if (temp.getNumDoors() == num) {
numDoors = temp;
return;
}
}
}
public void DrawDoors(Graphics2D g2d, Color color, boolean isAdditionalBody, int _startPosX, int _startPosY) {
int value = numDoors.getNumDoors();
if (value >= 3) {
g2d.setStroke(new BasicStroke(2));
Polygon firstDoor = new Polygon(new int[] { _startPosX + 45, _startPosX + 50, _startPosX + 55, _startPosX + 55, _startPosX + 50, _startPosX + 45, _startPosX + 40, _startPosX + 40 },
new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 20, _startPosY + 30, _startPosY + 40, _startPosY + 40, _startPosY + 30, _startPosY + 20 },
8);
g2d.setColor(color);
g2d.fillPolygon(firstDoor);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(firstDoor);
}
if (value >= 4) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
Polygon secondDoor = new Polygon(new int[] { _startPosX + 80, _startPosX + 85, _startPosX + 90, _startPosX + 90, _startPosX + 85, _startPosX + 80, _startPosX + 75, _startPosX + 75 },
new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 20, _startPosY + 30, _startPosY + 40, _startPosY + 40, _startPosY + 30, _startPosY + 20 },
8);
g2d.setColor(color);
g2d.fillPolygon(secondDoor);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(secondDoor);
}
if (value >= 5 && isAdditionalBody) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
Polygon thirdDoor = new Polygon(new int[] { _startPosX + 150, _startPosX + 155, _startPosX + 160, _startPosX + 160, _startPosX + 155, _startPosX + 150, _startPosX + 145, _startPosX + 145 },
new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 20, _startPosY + 30, _startPosY + 40, _startPosY + 40, _startPosY + 30, _startPosY + 20 },
8);
g2d.setColor(color);
g2d.fillPolygon(thirdDoor);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(thirdDoor);
}
}
}

View File

@ -0,0 +1,23 @@
package AccordionBus;
// Реализация интерфейса IMoveableObject для работы с объектом DrawingBus (паттерн Adapter)
public class DrawingObjectBus implements IMoveableObject {
private DrawingBus _drawingBus = null;
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.GetHeigth());
}
public int GetStep() { return (int)(_drawingBus.EntityBus.Step()); }
public boolean CheckCanMove(DirectionType direction) { return _drawingBus.CanMove(direction); }
public void MoveObject(DirectionType direction) { _drawingBus.MoveTransport(direction); }
}

View File

@ -1,24 +1,11 @@
package AccordionBus;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EntityAccordionBus {
// Скорость
private int Speed;
public int getSpeed() { return Speed; }
// Вес
private float Weight;
public float getWeight() {
return Weight;
}
// Основной цвет
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
// Класс-сущность "Автобус-гармошка"
public class EntityAccordionBus extends EntityBus {
// Дополнительный цвет
private Color AdditionalColor;;
public Color getAdditionalColor() {
@ -37,19 +24,11 @@ public class EntityAccordionBus {
return AdditionalDoor;
}
// Шаг перемещения автобуса
public double Step;
// Инициализация полей
public void Init(int speed, float weight,
Color bodyColor, Color additionalColor,
boolean additionalBody, boolean additionalDoor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
// Конструктор с параметрами
public EntityAccordionBus(int speed, double weight, Color bodyColor, Color additionalColor, boolean additionalBody, boolean additionalDoor) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
AdditionalBody = additionalBody;
AdditionalDoor = additionalDoor;
Step = (double)Speed * 100 / Weight;
}
}

View File

@ -0,0 +1,34 @@
package AccordionBus;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Класс-сущность "Автобус"
public class EntityBus {
// Скорость
public int Speed;
public int getSpeed() { return Speed; }
// Вес
public double Weight;
public double getWeight() {
return Weight;
}
// Основной цвет
public Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
// Шаг перемещения автобуса
public double Step() { return (double)Speed * 100 / Weight; }
// Конструктор с параметрами
public EntityBus(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,10 @@
package AccordionBus;
import java.awt.*;
// Интерфейс для отрисовки дополнительных дверей
public interface IDrawingDoors {
public void setNumDoors(int num);
public void DrawDoors(Graphics2D g2d, Color additionalColor, boolean isAdditionalBody, int _startPosX, int _startPosY);
}

View File

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

View File

@ -2,6 +2,6 @@ package AccordionBus;
public class Main {
public static void main(String[] args) {
new FrameAccordionBus();
new MainFrameAccordionBus();
}
}

View File

@ -0,0 +1,21 @@
package AccordionBus;
import javax.swing.*;
import java.awt.*;
public class MainFrameAccordionBus extends JFrame {
private AccordionBusForm accordionBusForm;
public MainFrameAccordionBus() {
super();
setTitle("AccordionBus");
setDefaultCloseOperation(EXIT_ON_CLOSE);
accordionBusForm = new AccordionBusForm();
setContentPane(accordionBusForm.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setPreferredSize(new Dimension(900, 500));
setLocation(500, 500);
pack();
setVisible(true);
}
}

View File

@ -0,0 +1,33 @@
package AccordionBus;
// Стратегия перемещения объекта в правый нижний угол экрана
public class MoveToBorder extends AbstractStrategy {
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;
}
protected void MoveToTarget() {
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.RightBorder() - FieldWidth;
if (Math.abs(diffX) > GetStep()) {
MoveRight();
}
var diffY = objParams.ObjectMiddleVertical() - FieldWidth;
if (Math.abs(diffY) > GetStep()) {
MoveDown();
}
}
}

View File

@ -0,0 +1,43 @@
package AccordionBus;
// Стратегия перемещения объекта в центр экрана
public class MoveToCenter extends AbstractStrategy {
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;
}
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

@ -0,0 +1,35 @@
package AccordionBus;
// Параметры-координаты рбъекта
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private 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 ObjectParameters(int x, int y, int width, int height) {
_x = x;
_y = y;
_width = width;
_height = height;
}
}

10
AccordionBus/Status.java Normal file
View File

@ -0,0 +1,10 @@
package AccordionBus;
// Статус выполнения операции перемещения
public enum Status {
NotInit,
InProgress,
Finish
}