diff --git a/AccordionBus/AccordionBusForm.form b/AccordionBus/AccordionBusForm.form new file mode 100644 index 0000000..56443d0 --- /dev/null +++ b/AccordionBus/AccordionBusForm.form @@ -0,0 +1,90 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/AccordionBus/AccordionBusForm.java b/AccordionBus/AccordionBusForm.java new file mode 100644 index 0000000..e2f60cd --- /dev/null +++ b/AccordionBus/AccordionBusForm.java @@ -0,0 +1,80 @@ +package AccordionBus; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionListener; +import java.util.Random; + +public class AccordionBusForm { + private DrawingAccordionBus drawingAccordionBus; + private JPanel pictureBox; + public JPanel getPictureBox() { + return pictureBox; + } + private JPanel createPanel; + private JButton buttonCreateAccordionBus; + private JButton buttonCreate; + private JButton buttonLeft; + private JButton buttonDown; + private JButton buttonRight; + private JButton buttonUp; + private JPanel MovePanel; + + public AccordionBusForm() { + buttonUp.setName("buttonUp"); + buttonDown.setName("buttonDown"); + buttonLeft.setName("buttonLeft"); + buttonRight.setName("buttonRight"); + + buttonCreateAccordionBus.addActionListener(e -> { + Random random = new Random(); + drawingAccordionBus = new DrawingAccordionBus(); + drawingAccordionBus.Init(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)), + random.nextBoolean(), + random.nextBoolean(), + pictureBox.getWidth(), + pictureBox.getHeight()); + drawingAccordionBus.SetNumDoors(random.nextInt(3, 6)); + drawingAccordionBus.SetPosition(random.nextInt(10, 100), + random.nextInt(10, 100)); + Draw(); + }); + + ActionListener buttonMoveClickedListener = e -> { + String name = ((JButton)e.getSource()).getName(); + switch (name) { + case "buttonUp" -> { + drawingAccordionBus.MoveTransport(DirectionType.Up); + } + case "buttonDown" -> { + drawingAccordionBus.MoveTransport(DirectionType.Down); + } + case "buttonLeft" -> { + drawingAccordionBus.MoveTransport(DirectionType.Left); + } + case "buttonRight" -> { + drawingAccordionBus.MoveTransport(DirectionType.Right); + } + } + Draw(); + }; + + buttonUp.addActionListener(buttonMoveClickedListener); + buttonDown.addActionListener(buttonMoveClickedListener); + buttonLeft.addActionListener(buttonMoveClickedListener); + buttonRight.addActionListener(buttonMoveClickedListener); + } + + public void Draw() { + if (drawingAccordionBus.AccordionBus == null) { + return; + } + + Graphics g = pictureBox.getGraphics(); + pictureBox.paint(g); + drawingAccordionBus.DrawTransport(g); + } +} diff --git a/AccordionBus/DirectionType.java b/AccordionBus/DirectionType.java new file mode 100644 index 0000000..a4022a1 --- /dev/null +++ b/AccordionBus/DirectionType.java @@ -0,0 +1,13 @@ +package AccordionBus; + +// Направление перемещения +public enum DirectionType { + // Вверх + Up, + // Вниз + Down, + // Влево + Left, + // Вправо + Right, +} diff --git a/AccordionBus/DrawingAccordionBus.java b/AccordionBus/DrawingAccordionBus.java new file mode 100644 index 0000000..0f9d010 --- /dev/null +++ b/AccordionBus/DrawingAccordionBus.java @@ -0,0 +1,199 @@ +package AccordionBus; + +import java.awt.*; +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 void DrawTransport(Graphics g) { + if (AccordionBus == null) { + return; + } + + Graphics2D g2d = (Graphics2D)g; + + Color bodyColor = AccordionBus.getBodyColor(); + Color additionalColor = AccordionBus.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()) { + // Граница дополнительного отсека + g2d.setColor(blackColor); + g2d.setStroke(new BasicStroke(3)); + g2d.drawRect(_startPosX + 115, _startPosY, 100, 40); + + // Кузов дополнительного отсека + g2d.setColor(bodyColor); + g2d.fillRect(_startPosX + 116, _startPosY + 1, 99, 39); + + // Окна главного отсека + g2d.setColor(Color.CYAN); + g2d.fillRect(_startPosX + 115, _startPosY + 5, 100, 15); + g2d.setColor(blackColor); + g2d.setStroke(new BasicStroke(2)); + g2d.drawLine(_startPosX + 115, _startPosY + 5, _startPosX + 215, _startPosY + 5); + g2d.drawLine(_startPosX + 115, _startPosY + 20, _startPosX + 215, _startPosY + 20); + + // Дверь дополнительного отсека + if (AccordionBus.isAdditionalDoor()) { + g2d.setColor(blackColor); + g2d.setStroke(new BasicStroke(2)); + g2d.drawRect(_startPosX + 175, _startPosY + 10, 15, 30); + g2d.setColor(whiteColor); + g2d.fillRect(_startPosX + 176, _startPosY + 11, 14, 29); + } + + // Гармошка + g2d.setColor(blackColor); + g2d.setStroke(new BasicStroke(2)); + for (int i = 0; i < 5; i++) { + g2d.drawLine(_startPosX + 100 + i * 3, _startPosY, _startPosX + 100 + i * 3, _startPosY + 40); + } + } + + // Дополнительные двери + drawingDoors.DrawDoors(g2d, AccordionBus, additionalColor, _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); + + // Колеса дополнительного отсека + if (AccordionBus.isAdditionalBody()) { + g2d.setColor(blackColor); + g2d.drawOval(_startPosX + 130, _startPosY + 33, 15, 15); + g2d.drawOval(_startPosX + 185, _startPosY + 33, 15, 15); + g2d.setColor(whiteColor); + g2d.fillOval(_startPosX + 131, _startPosY + 33, 15, 15); + g2d.fillOval(_startPosX + 186, _startPosY + 33, 15, 15); + } + } +} diff --git a/AccordionBus/DrawingDoors.java b/AccordionBus/DrawingDoors.java new file mode 100644 index 0000000..837b78e --- /dev/null +++ b/AccordionBus/DrawingDoors.java @@ -0,0 +1,42 @@ +package AccordionBus; + +import java.awt.*; + +public class DrawingDoors { + 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, EntityAccordionBus AccordionBus, Color additionalColor, int _startPosX, int _startPosY) { + g2d.setColor(additionalColor); + 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.WHITE); + 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.WHITE); + g2d.fillRect(_startPosX + 76, _startPosY + 11, 14, 29); + } + if (value >= 5 && AccordionBus.isAdditionalBody()) { + g2d.setColor(Color.BLACK); + g2d.setStroke(new BasicStroke(2)); + g2d.drawRect(_startPosX + 145, _startPosY + 10, 15, 30); + g2d.setColor(Color.WHITE); + g2d.fillRect(_startPosX + 146, _startPosY + 11, 14, 29); + } + } +} diff --git a/AccordionBus/EntityAccordionBus.java b/AccordionBus/EntityAccordionBus.java new file mode 100644 index 0000000..c58b201 --- /dev/null +++ b/AccordionBus/EntityAccordionBus.java @@ -0,0 +1,55 @@ +package AccordionBus; + +import java.awt.*; + +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; + } + + // Дополнительный цвет + private Color AdditionalColor;; + public Color getAdditionalColor() { + return AdditionalColor; + } + + // Дополнительный отсек + private boolean AdditionalBody; + public boolean isAdditionalBody() { + return AdditionalBody; + } + + // Дополнительная дверь + private boolean AdditionalDoor; + public boolean isAdditionalDoor() { + 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; + AdditionalColor = additionalColor; + AdditionalBody = additionalBody; + AdditionalDoor = additionalDoor; + Step = (double)Speed * 100 / Weight; + } +} diff --git a/AccordionBus/FrameAccordionBus.java b/AccordionBus/FrameAccordionBus.java new file mode 100644 index 0000000..4e5e3a8 --- /dev/null +++ b/AccordionBus/FrameAccordionBus.java @@ -0,0 +1,21 @@ +package AccordionBus; + +import javax.swing.*; +import java.awt.*; + +public class FrameAccordionBus extends JFrame { + private AccordionBusForm accordionBusForm; + + public FrameAccordionBus() { + 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); + } +} diff --git a/AccordionBus/Main.java b/AccordionBus/Main.java index 486e3be..95f5f8e 100644 --- a/AccordionBus/Main.java +++ b/AccordionBus/Main.java @@ -2,6 +2,6 @@ package AccordionBus; public class Main { public static void main(String[] args) { - + new FrameAccordionBus(); } } \ No newline at end of file diff --git a/AccordionBus/NumDoors.java b/AccordionBus/NumDoors.java new file mode 100644 index 0000000..492b809 --- /dev/null +++ b/AccordionBus/NumDoors.java @@ -0,0 +1,22 @@ +package AccordionBus; + +// Количество дополнительных дверей +public enum NumDoors { + // 3 двери + Three(3), + + // 4 двери + Four(4), + + // 5 дверей + Five(5); + + // Количество дверей + private final int Value; + public int getNumDoors() { return Value; } + + // Конструктор + NumDoors(int value) { + Value = value; + } +} \ No newline at end of file diff --git a/AccordionBus/img/Down.png b/AccordionBus/img/Down.png new file mode 100644 index 0000000..67f0753 Binary files /dev/null and b/AccordionBus/img/Down.png differ diff --git a/AccordionBus/img/Left.png b/AccordionBus/img/Left.png new file mode 100644 index 0000000..b0c5a86 Binary files /dev/null and b/AccordionBus/img/Left.png differ diff --git a/AccordionBus/img/Right.png b/AccordionBus/img/Right.png new file mode 100644 index 0000000..fda82ab Binary files /dev/null and b/AccordionBus/img/Right.png differ diff --git a/AccordionBus/img/Up.png b/AccordionBus/img/Up.png new file mode 100644 index 0000000..df0a580 Binary files /dev/null and b/AccordionBus/img/Up.png differ