diff --git a/Down.png b/Down.png new file mode 100644 index 0000000..7e28ff1 Binary files /dev/null and b/Down.png differ diff --git a/Left.png b/Left.png new file mode 100644 index 0000000..ed616b2 Binary files /dev/null and b/Left.png differ diff --git a/Right.png b/Right.png new file mode 100644 index 0000000..84b5bb3 Binary files /dev/null and b/Right.png differ diff --git a/Trolleybus/CntOfDoors.java b/Trolleybus/CntOfDoors.java new file mode 100644 index 0000000..31bb1d5 --- /dev/null +++ b/Trolleybus/CntOfDoors.java @@ -0,0 +1,14 @@ +package Trolleybus; + +public enum CntOfDoors { + Three (3), + Four (4), + Five (5); + private int Cnt; + CntOfDoors(int cnt) { + Cnt = cnt; + } + public int getCnt(){ + return Cnt; + } +} \ No newline at end of file diff --git a/Trolleybus/DirectionType.java b/Trolleybus/DirectionType.java new file mode 100644 index 0000000..b7b0b90 --- /dev/null +++ b/Trolleybus/DirectionType.java @@ -0,0 +1,12 @@ +package Trolleybus; + +public enum DirectionType { + Up (1), + Down (2), + Right (3), + Left (4); + private int Direction; + DirectionType(int direction) { + Direction = direction; + } +} \ No newline at end of file diff --git a/Trolleybus/DrawingDoors.java b/Trolleybus/DrawingDoors.java new file mode 100644 index 0000000..f66c3b0 --- /dev/null +++ b/Trolleybus/DrawingDoors.java @@ -0,0 +1,57 @@ +package Trolleybus; + +import java.awt.*; +public class DrawingDoors { + private CntOfDoors _cntOfDoors; + public void SetCntOfDoors(int cnt) { + if (cnt < 3 || cnt > 5) { + cnt = 3; + } + //Проход по перечислению + for (CntOfDoors temp : CntOfDoors.values()) { + if (temp.getCnt() == cnt) { + _cntOfDoors = temp; + return; + } + } + } + + public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY) { + switch (_cntOfDoors.getCnt()) + { + case 3: + 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 4: + 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 5: + 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; + } + } +} \ No newline at end of file diff --git a/Trolleybus/DrawingTrolleybus.java b/Trolleybus/DrawingTrolleybus.java new file mode 100644 index 0000000..4582035 --- /dev/null +++ b/Trolleybus/DrawingTrolleybus.java @@ -0,0 +1,130 @@ +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) + { + 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) + { + 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; + } + } + // Прорисовка объекта + public void DrawTransport(Graphics g) + { + if (EntityTrolleybus == null) + { + return; + } + 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.drawLine(_startPosX + 70, _startPosY + 30, _startPosX + 40, _startPosY); + g2d.drawLine(_startPosX + 70, _startPosY + 30, _startPosX + 60, _startPosY); + } + //Опциональный отсек для батареи + if (EntityTrolleybus.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); + } + } +} \ No newline at end of file diff --git a/Trolleybus/EntityTrolleybus.java b/Trolleybus/EntityTrolleybus.java new file mode 100644 index 0000000..b327199 --- /dev/null +++ b/Trolleybus/EntityTrolleybus.java @@ -0,0 +1,55 @@ +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 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 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; + AdditionalColor = additionalColor; + Horns = horns; + Batteries = batteries; + } +} \ No newline at end of file diff --git a/Trolleybus/FormTrolleybus.java b/Trolleybus/FormTrolleybus.java new file mode 100644 index 0000000..e71c892 --- /dev/null +++ b/Trolleybus/FormTrolleybus.java @@ -0,0 +1,139 @@ +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 DrawingTrolleybus _drawingTrolleybus; + private JFrame frameTrolleybus; + private JPanel panelTrolleybus; + private JButton buttonCreate, buttonUp, buttonDown, buttonRight, buttonLeft; + + 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); + + //Кнопка создания + buttonCreate = new JButton("Создать"); + //Кнопка вверх + 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"); + //Размеры, позиция кнопок + buttonCreate.setBounds(10,400,90,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); + + //Добавление листенеров к кнопкам + buttonCreate.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); + } + }); + + panelTrolleybus.add(buttonCreate); + panelTrolleybus.add(buttonUp); + panelTrolleybus.add(buttonDown); + panelTrolleybus.add(buttonLeft); + panelTrolleybus.add(buttonRight); + frameTrolleybus.add(panelTrolleybus, BorderLayout.CENTER); + + frameTrolleybus.setVisible(true); + } + // Метод прорисовки троллейбуса + private void Draw(){ + if (_drawingTrolleybus == null) { + return; + } + Graphics g = panelTrolleybus.getGraphics(); + // Очистка перед перерисовкой + panelTrolleybus.paint(g); + _drawingTrolleybus.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)); + Draw(); + } + protected void ButtonMove_Click(Object sender, ActionEvent e) { + if (_drawingTrolleybus == null) + { + return; + } + JButton info = (JButton)e.getSource(); + String name = info.getToolTipText(); + switch (name) + { + case "buttonUp": + _drawingTrolleybus.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + _drawingTrolleybus.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + _drawingTrolleybus.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + _drawingTrolleybus.MoveTransport(DirectionType.Right); + break; + } + Draw(); + } +} \ No newline at end of file diff --git a/Trolleybus/Main.java b/Trolleybus/Main.java new file mode 100644 index 0000000..5af78d5 --- /dev/null +++ b/Trolleybus/Main.java @@ -0,0 +1,7 @@ +package Trolleybus; + +public class Main { + public static void main(String[] args) { + new FormTrolleybus(); + } +} \ No newline at end of file diff --git a/Up.png b/Up.png new file mode 100644 index 0000000..4a14c56 Binary files /dev/null and b/Up.png differ