diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml b/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml
new file mode 100644
index 0000000..6e2c775
--- /dev/null
+++ b/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..6f29fee
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..81fb09f
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..8306744
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/img/down.png b/img/down.png
new file mode 100644
index 0000000..50d531f
Binary files /dev/null and b/img/down.png differ
diff --git a/img/left.png b/img/left.png
new file mode 100644
index 0000000..3fd289a
Binary files /dev/null and b/img/left.png differ
diff --git a/img/right.png b/img/right.png
new file mode 100644
index 0000000..8af2e6f
Binary files /dev/null and b/img/right.png differ
diff --git a/img/up.png b/img/up.png
new file mode 100644
index 0000000..7b3dfed
Binary files /dev/null and b/img/up.png differ
diff --git a/src/DirectionType.java b/src/DirectionType.java
new file mode 100644
index 0000000..0d664bb
--- /dev/null
+++ b/src/DirectionType.java
@@ -0,0 +1,6 @@
+public enum DirectionType {
+ UP,
+ DOWN,
+ LEFT,
+ RIGHT
+}
\ No newline at end of file
diff --git a/src/DoorsNumber.java b/src/DoorsNumber.java
new file mode 100644
index 0000000..cba85c9
--- /dev/null
+++ b/src/DoorsNumber.java
@@ -0,0 +1,5 @@
+public enum DoorsNumber {
+ THREE,
+ FOUR,
+ FIVE
+}
\ No newline at end of file
diff --git a/src/DrawingDoors.java b/src/DrawingDoors.java
new file mode 100644
index 0000000..3ab1642
--- /dev/null
+++ b/src/DrawingDoors.java
@@ -0,0 +1,23 @@
+import java.awt.*;
+public class DrawingDoors {
+ private DoorsNumber number;
+ public void getNumber(int x){
+ if(x <= 3)
+ number = DoorsNumber.THREE;
+ if(x == 4)
+ number = DoorsNumber.FOUR;
+ if(x >= 5)
+ number = DoorsNumber.FIVE;
+ }
+ public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){
+ graphics2D.fillRect(_startX+52, _startY+81, 25, 40);
+ graphics2D.fillRect(_startX+85, _startY+81, 25, 40);
+ graphics2D.fillRect(_startX+118, _startY+81, 25, 40);
+ if (number == DoorsNumber.FOUR || number == DoorsNumber.FIVE){
+ graphics2D.fillRect(_startX+151, _startY+81, 25, 40);
+ }
+ if (number == DoorsNumber.FIVE){
+ graphics2D.fillRect(_startX+19, _startY+81, 25, 40);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/DrawingTrolleybus.java b/src/DrawingTrolleybus.java
new file mode 100644
index 0000000..7e4b69b
--- /dev/null
+++ b/src/DrawingTrolleybus.java
@@ -0,0 +1,125 @@
+import java.awt.*;
+public class DrawingTrolleybus {
+ private EntityTrolleybus entityTrolleybus;
+ public EntityTrolleybus getEntityTrolleybus() {
+ return entityTrolleybus;
+ }
+ private int _pictureWidth;
+ private int _pictureHeight;
+ private int _startPosX;
+ private int _startPosY;
+ private final int trolleybusWidth = 200;
+ private final int trolleybusHeight = 135;
+ private DrawingDoors drawingDoors;
+ public boolean init(int speed, double weight, Color bodyColor, Color additionalColor,
+ boolean roga, boolean battery, int width, int height, int doorsNumber)
+ {
+ if (width < trolleybusWidth || height < trolleybusHeight)
+ return false;
+ _pictureWidth = width;
+ _pictureHeight = height;
+ entityTrolleybus = new EntityTrolleybus();
+ entityTrolleybus.init(speed, weight, bodyColor, additionalColor, roga, battery);
+ drawingDoors = new DrawingDoors();
+ drawingDoors.getNumber(doorsNumber);
+ return true;
+ }
+ public void setPosition(int x, int y)
+ {
+ if (x < 0 || y < 0 || x + trolleybusWidth >= _pictureWidth || y + trolleybusHeight >= _pictureHeight) {
+ x = 0;
+ y = 0;
+ }
+ _startPosX = x;
+ _startPosY = y;
+ }
+ public void moveTransport(DirectionType direction) {
+ if (entityTrolleybus == null)
+ return;
+ switch (direction) {
+ //влево
+ case LEFT:
+ if (_startPosX - entityTrolleybus.step.get().intValue() > 0)
+ _startPosX -= entityTrolleybus.step.get().intValue();
+ break;
+ //вверх
+ case UP:
+ if (_startPosY - entityTrolleybus.step.get().intValue() > 0)
+ _startPosY -= entityTrolleybus.step.get().intValue();
+ break;
+ // вправо
+ case RIGHT:
+ if (_startPosX + trolleybusWidth + entityTrolleybus.step.get().intValue() < _pictureWidth)
+ _startPosX += entityTrolleybus.step.get().intValue();
+ break;
+ //вниз
+ case DOWN:
+ if (_startPosY + trolleybusHeight + entityTrolleybus.step.get().intValue() < _pictureHeight)
+ _startPosY += entityTrolleybus.step.get().intValue();
+ break;
+ }
+ }
+ public void drawTransport(Graphics2D graphics2D) {
+ if (entityTrolleybus == null)
+ return;
+ BasicStroke pen = new BasicStroke(2);
+ graphics2D.setStroke(pen);
+ Color bodyColor = entityTrolleybus.getBodyColor();
+ Color additionalColor = entityTrolleybus.getAdditionalColor();
+ //колеса
+ graphics2D.setPaint(Color.BLACK);
+ graphics2D.fillOval(_startPosX + 31, _startPosY + 106, 30, 30);
+ graphics2D.fillOval(_startPosX + 151, _startPosY + 106, 30, 30);
+ //кузов
+ graphics2D.setPaint(bodyColor);
+ graphics2D.fillRect(_startPosX + 6, _startPosY + 31, 200, 90);
+ //стекла
+ graphics2D.setPaint(Color.BLUE);
+ graphics2D.fillRect(_startPosX + 186, _startPosY + 40, 20, 40);
+ graphics2D.fillOval(_startPosX + 151, _startPosY + 35, 30, 40);
+ graphics2D.fillOval(_startPosX + 118, _startPosY + 35, 30, 40);
+ graphics2D.fillOval(_startPosX + 85, _startPosY + 35, 30, 40);
+ graphics2D.fillOval(_startPosX + 52, _startPosY + 35, 30, 40);
+ graphics2D.fillOval(_startPosX + 19, _startPosY + 35, 30, 40);
+ //двери
+ graphics2D.setPaint(Color.BLACK);
+ drawingDoors.drawDoors(graphics2D, _startPosX, _startPosY);
+ //рога
+ graphics2D.setPaint(Color.BLACK);
+ if (entityTrolleybus.getRoga()) {
+ graphics2D.setPaint(Color.BLACK);
+ graphics2D.drawLine(_startPosX + 186, _startPosY + 31, _startPosX + 86, _startPosY + 1);
+ graphics2D.drawLine(_startPosX + 86, _startPosY + 1, _startPosX + 126, _startPosY + 31);
+ graphics2D.drawLine(_startPosX + 146, _startPosY + 31, _startPosX + 46, _startPosY + 1);
+ graphics2D.drawLine(_startPosX + 46, _startPosY + 1, _startPosX + 86, _startPosY + 31);
+ }
+ //батарея
+ if (entityTrolleybus.getBattery()) {
+ graphics2D.setPaint(additionalColor);
+ graphics2D.fillRect(_startPosX + 176, _startPosY + 101, 15, 20);
+ graphics2D.setPaint(Color.YELLOW);
+ graphics2D.drawLine(_startPosX + 183, _startPosY + 103, _startPosX + 178, _startPosY + 111);
+ graphics2D.drawLine(_startPosX + 178, _startPosY + 111, _startPosX + 189, _startPosY + 111);
+ graphics2D.drawLine(_startPosX + 189, _startPosY + 111, _startPosX + 183, _startPosY + 119);
+ }
+ //границы троллейбуса
+ graphics2D.setPaint(Color.BLACK);
+ graphics2D.drawRect(_startPosX + 6, _startPosY + 31, 200, 90);
+ graphics2D.drawOval(_startPosX + 151, _startPosY + 35, 30, 40);
+ graphics2D.drawOval(_startPosX + 118, _startPosY + 35, 30, 40);
+ graphics2D.drawOval(_startPosX + 85, _startPosY + 35, 30, 40);
+ graphics2D.drawOval(_startPosX + 52, _startPosY + 35, 30, 40);
+ graphics2D.drawOval(_startPosX + 19, _startPosY + 35, 30, 40);
+ graphics2D.drawRect(_startPosX + 186, _startPosY + 40, 20, 40);
+ //задние фары
+ graphics2D.setPaint(Color.RED);
+ graphics2D.fillRect(_startPosX + 6, _startPosY + 91, 10, 20);
+ graphics2D.setPaint(Color.BLACK);
+ graphics2D.drawRect(_startPosX + 6, _startPosY + 91, 10, 20);
+ //передние фары
+ graphics2D.setPaint(Color.YELLOW);
+ graphics2D.fillRect(_startPosX + 196, _startPosY + 91, 10, 20);
+ graphics2D.setPaint(Color.BLACK);
+ graphics2D.drawRect(_startPosX + 196, _startPosY + 91, 10, 20);
+ }
+}
\ No newline at end of file
diff --git a/src/EntityTrolleybus.java b/src/EntityTrolleybus.java
new file mode 100644
index 0000000..c468cd9
--- /dev/null
+++ b/src/EntityTrolleybus.java
@@ -0,0 +1,37 @@
+import java.awt.*;
+import java.util.function.Supplier;
+
+public class EntityTrolleybus {
+ private int speed;
+ public int getSpeed(){
+ return speed;
+ }
+ private double weight;
+ public double getWeight(){
+ return weight;
+ }
+ private Color bodyColor;
+ public Color getBodyColor(){
+ return bodyColor;
+ }
+ private Color additionalColor;
+ public Color getAdditionalColor(){return additionalColor;}
+ private boolean roga;
+ public boolean getRoga() {
+ return roga;
+ }
+ private boolean battery;
+ public boolean getBattery() {
+ return battery;
+ }
+ public Supplier step = () -> (double) speed * 100 / weight;
+ public void init(int speed, double weight, Color bodyColor, Color
+ additionalColor, boolean roga, boolean battery) {
+ this.speed = speed;
+ this.weight = weight;
+ this.bodyColor = bodyColor;
+ this.additionalColor = additionalColor;
+ this.roga = roga;
+ this.battery = battery;
+ }
+}
\ No newline at end of file
diff --git a/src/FrameTrolleybus.java b/src/FrameTrolleybus.java
new file mode 100644
index 0000000..c684551
--- /dev/null
+++ b/src/FrameTrolleybus.java
@@ -0,0 +1,109 @@
+import javax.imageio.ImageIO;
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.io.File;
+import java.io.IOException;
+import java.util.Random;
+public class FrameTrolleybus extends JFrame {
+ private DrawingTrolleybus drawingTrolleybus;
+ private final JComponent pictureBoxTrolleybus;
+ public FrameTrolleybus() throws IOException {
+ super("Троллейбус");
+ setSize(new Dimension(900,500));
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ pictureBoxTrolleybus = new JComponent(){
+ public void paintComponent(Graphics graphics){
+ super.paintComponent(graphics);
+ Graphics2D graphics2D = (Graphics2D) graphics;
+ if (drawingTrolleybus != null) drawingTrolleybus.drawTransport(graphics2D);
+ super.repaint();
+ }
+ };
+ JButton createButton = new JButton("Создать");
+ JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("img/right.png"))));
+ JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("img/left.png"))));
+ JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("img/up.png"))));
+ JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("img/down.png"))));
+ pictureBoxTrolleybus.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
+ createButton.addActionListener(e -> buttonCreateTrolleybus());
+ rightButton.setActionCommand("right");
+ rightButton.addActionListener(this::buttonMoveClick);
+ leftButton.setActionCommand("left");
+ leftButton.addActionListener(this::buttonMoveClick);
+ upButton.setActionCommand("up");
+ upButton.addActionListener(this::buttonMoveClick);
+ downButton.setActionCommand("down");
+ downButton.addActionListener(this::buttonMoveClick);
+ setLayout(new BorderLayout());
+ JPanel panelTrolleybus = new JPanel(new BorderLayout());
+ JPanel createPanel = new JPanel(new BorderLayout());
+ createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
+ createPanel.add(createButton, BorderLayout.SOUTH);
+ JPanel movementPanel = new JPanel(new GridBagLayout());
+ JPanel rightPanel = new JPanel(new BorderLayout());
+ rightPanel.add(movementPanel, BorderLayout.SOUTH);
+ rightButton.setPreferredSize(new Dimension(30,30));
+ GridBagConstraints constraints = new GridBagConstraints();
+ constraints.gridx = 2;
+ constraints.gridy = 1;
+ constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
+ movementPanel.add(rightButton, constraints);
+ leftButton.setPreferredSize(new Dimension(30,30));
+ constraints.gridx = 0;
+ constraints.gridy = 1;
+ movementPanel.add(leftButton, constraints);
+ upButton.setPreferredSize(new Dimension(30,30));
+ constraints.gridx = 1;
+ constraints.gridy = 0;
+ movementPanel.add(upButton, constraints);
+ downButton.setPreferredSize(new Dimension(30,30));
+ constraints.gridx = 1;
+ constraints.gridy = 1;
+ movementPanel.add(downButton, constraints);
+ add(pictureBoxTrolleybus);
+ panelTrolleybus.add(rightPanel, BorderLayout.EAST);
+ panelTrolleybus.add(createPanel, BorderLayout.WEST);
+ add(panelTrolleybus,BorderLayout.CENTER);
+ setVisible(true);
+ }
+ private void buttonCreateTrolleybus() {
+ Random random = new Random();
+ drawingTrolleybus = new DrawingTrolleybus();
+ pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
+ drawingTrolleybus.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
+ 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(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(),
+ (random.nextInt(3)+1)*2);
+ drawingTrolleybus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
+ draw();
+ }
+ private void buttonMoveClick(ActionEvent event) {
+ if(drawingTrolleybus == null || drawingTrolleybus.getEntityTrolleybus() == null)
+ return;
+ switch (event.getActionCommand())
+ {
+ case "left":
+ drawingTrolleybus.moveTransport(DirectionType.LEFT);
+ break;
+ case "right":
+ drawingTrolleybus.moveTransport(DirectionType.RIGHT);
+ break;
+ case "up":
+ drawingTrolleybus.moveTransport(DirectionType.UP);
+ break;
+ case "down":
+ drawingTrolleybus.moveTransport(DirectionType.DOWN);
+ break;
+ }
+ draw();
+ }
+ private void draw() {
+ if (drawingTrolleybus == null)
+ {
+ return;
+ }
+ pictureBoxTrolleybus.repaint();
+ }
+}
\ No newline at end of file
diff --git a/src/Main.java b/src/Main.java
new file mode 100644
index 0000000..dabb840
--- /dev/null
+++ b/src/Main.java
@@ -0,0 +1,4 @@
+import java.io.IOException;
+public class Main {
+ public static void main(String[] args) throws IOException { new FrameTrolleybus(); }
+}