From 53b492ccc79d5da7d6f5b2ed06fd027d84196fbc Mon Sep 17 00:00:00 2001 From: MayDayR Date: Fri, 15 Dec 2023 19:15:30 +0400 Subject: [PATCH] Initial commit --- .idea/.gitignore | 3 + .idea/gitjava.iml | 9 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ DoubleDeckerBus.iml | 11 ++ images/KeyDown.png | Bin 0 -> 398 bytes images/KeyLeft.png | Bin 0 -> 407 bytes images/KeyRight.png | Bin 0 -> 385 bytes images/KeyUp.png | Bin 0 -> 387 bytes src/Drawnings/DrawingBus.java | 144 ++++++++++++++++++++++++++ src/Drawnings/DrawingDoors.java | 56 +++++++++++ src/Entities/CountDoors.java | 6 ++ src/Entities/EntityBus.java | 47 +++++++++ src/FormBus.java | 151 ++++++++++++++++++++++++++++ src/Main.java | 5 + src/MovementStrategy/Direction.java | 7 ++ 17 files changed, 459 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/gitjava.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 DoubleDeckerBus.iml create mode 100644 images/KeyDown.png create mode 100644 images/KeyLeft.png create mode 100644 images/KeyRight.png create mode 100644 images/KeyUp.png create mode 100644 src/Drawnings/DrawingBus.java create mode 100644 src/Drawnings/DrawingDoors.java create mode 100644 src/Entities/CountDoors.java create mode 100644 src/Entities/EntityBus.java create mode 100644 src/FormBus.java create mode 100644 src/Main.java create mode 100644 src/MovementStrategy/Direction.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/gitjava.iml b/.idea/gitjava.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/gitjava.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..03f397c --- /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..3ac4fda --- /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..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DoubleDeckerBus.iml b/DoubleDeckerBus.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/DoubleDeckerBus.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/images/KeyDown.png b/images/KeyDown.png new file mode 100644 index 0000000000000000000000000000000000000000..a967fa6d8ed3ecfc15035877f86bc290f7a557f0 GIT binary patch literal 398 zcmeAS@N?(olHy`uVBq!ia0vp^+CZ$!!3HGFSti{DQjEnx?oJHr&dIz4a#+$GeH|GX zHuiJ>Nn{1`ISV`@iy0XBj({-ZRBb+Kpax}67srqY_qR8#`3@QIuq7-HnbOhOX|15g zAl@k2dD&>g0mT~=9c-pdP%yrzBy-zYwVr1O_us-`ml$#37oA-9s}CN^5{%v)e*N#> zTAqlHH;gVMC2oy~&=m{oZoP`i{Gz3kS^hhlJ-TbtXVtlXH$^3@9=4w!@weT^e8)~H zr>a6;g+mi8HYvnkS6h~6aW7_xB$H)E*cOi32d!)KQp1=1=v%q!$~>msCq=ZauB}=9 z3@OP&9_3X fuQB0X&HRAXM=40ySC_977={d-w@w(^S^=w`%fK%O{Ysv-ag8)E9H`=M*&;F%8#t=B4(kT6J_0&K3Up! zGS2yeX!E4!HS9|te=>WizkQeX4tdMi4KZFlRmPeNJ^oDD);;Y>v*Bh(WB9?NtbJWa&Nr=;jI(&<7v^~)ZodA{v<;7WwjOYK8>;;%y!@`s zzVl&=Z#}ZVm~d;6-hO{K&#W^xy6+`+rfyl1IJNTRD-+KnTVLpS_KNM5`zCQS_0q!a z+Px#1ZP1_K>z@;j|==^1poj532;bRa{vGmbN~PnbOGLGA9w%&0TxL_K~z{r?UvmR zf*=%t52Rc6Qr-2?-Srf0UH1ZAG;25&7BVa}31hJ`U$#h%z8!uMb4 zeJO&zgb{q)Wm|zD2X%|&x)Hnfv=`&0Y&!=jyM|~kB}xQ+B%^IHms}zmO>n?fGP zgDqy&&6iYW~%C>g3a{?hwdqwiFD^+_XenHb!R5~A_@A3)x_Md z2q$_3cMfu#Xc62iF-Z-*QGJn?e@`LAjH&rs>T&sib#Ds5Y5# z)#f!DiXFeG%e-`6x>WG@(exEL@l)oUEs$xQAersBNorj7sTDw0dO{{G9DxT!n_F+oR{Hko znB&8PQ{MeZ>QMfmaWdIAOEM(! _pictureWidth || y + _busHeight > _pictureHeight) { + _startPosX = _pictureWidth - _busWidth; + _startPosY = _pictureHeight - _busHeight; + } + else + { + _startPosX = x; + _startPosY = y; + } + } + + public void MoveTransport(Direction direction){ + if (entityBus == null) { + return; + } + + switch (direction) { + case Left: + if (_startPosX - entityBus.Step > 0) + { + _startPosX -= entityBus.Step; + } + break; + case Right: + if (_startPosX + _busWidth + entityBus.Step < _pictureWidth) + { + _startPosX += entityBus.Step; + } + break; + case Up: + if (_startPosY - entityBus.Step > 0) + { + _startPosY -= entityBus.Step; + } + break; + case Down: + if (_startPosY + _busHeight + entityBus.Step < _pictureHeight) + { + _startPosY += entityBus.Step; + } + break; + } + } + + public void DrawTransport(Graphics2D g) { + + if (entityBus == null) { + return; + } + + //тело + g.setColor(entityBus.getBodyColor()); + g.fillRect(_startPosX + 147, _startPosY + 52, 21, 20); + g.fillRect(_startPosX + 147, _startPosY + 71, 30, 27); + g.fillRect(_startPosX + 10, _startPosY + 52, 137, 46); + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 147, _startPosY + 52, 21, 20); + g.drawRect(_startPosX + 147, _startPosY + 71, 30, 27); + g.drawRect(_startPosX + 10, _startPosY + 52, 137, 46); + + g.setColor(Color.blue); + g.fillRect(_startPosX + 150, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 42, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 69, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 96, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 123, _startPosY + 55, 15, 15); + + g.setColor(Color.BLACK); + g.drawLine(_startPosX + 30, _startPosY + 52, _startPosX + 30, _startPosY + 98); + g.drawLine(_startPosX + 35, _startPosY + 52, _startPosX + 35, _startPosY + 98); + + //колёса + g.fillOval(_startPosX + 23, _startPosY + 88, 25, 25); + g.fillOval(_startPosX + 123, _startPosY + 88, 25, 25); + g.setColor(Color.gray); + g.fillOval(_startPosX + 25, _startPosY + 90, 21, 21); + g.fillOval(_startPosX + 125, _startPosY + 90, 21, 21); + + // двери + _door.Draw(g, _startPosX, _startPosY); + + // второй этаж + if (entityBus.IsSecondFloor()) + { + g.setColor(entityBus.getAdditionalColor()); + g.fillRect(_startPosX + 7, _startPosY + 12, 172, 40); //большой прямоугольник + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 7, _startPosY + 12, 172, 40); + g.drawLine(_startPosX + 7, _startPosY + 36, _startPosX + 178, _startPosY + 36); + + g.setColor(Color.blue); + g.fillRect(_startPosX + 15, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 42, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 69, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 96, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 123, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 150, _startPosY + 15, 15, 15); + } + + // лестница + if (entityBus.IsStairs()) + { + g.setColor(Color.BLACK); + g.drawLine(_startPosX + 10, _startPosY + 55, _startPosX + 34, _startPosY + 55); + g.drawLine(_startPosX + 10, _startPosY + 58, _startPosX + 34, _startPosY + 58); + g.drawLine(_startPosX + 10, _startPosY + 64, _startPosX + 34, _startPosY + 64); + g.drawLine(_startPosX + 10, _startPosY + 72, _startPosX + 34, _startPosY + 72); + g.drawLine(_startPosX + 10, _startPosY + 80, _startPosX + 34, _startPosY + 80); + g.drawLine(_startPosX + 10, _startPosY + 88, _startPosX + 34, _startPosY + 88); + g.drawLine(_startPosX + 10, _startPosY + 94, _startPosX + 34, _startPosY + 94); + } + } +} diff --git a/src/Drawnings/DrawingDoors.java b/src/Drawnings/DrawingDoors.java new file mode 100644 index 0000000..cac08a6 --- /dev/null +++ b/src/Drawnings/DrawingDoors.java @@ -0,0 +1,56 @@ +package Drawnings; + +import java.awt.*; +import Entities.*; + +public class DrawingDoors { + private CountDoors _door; + private int Count; + + public CountDoors getCount() + { + return _door; + } + public void SetCount (int count) { + Count = count; + switch (Count) { + case 3: + _door = CountDoors.Three; + break; + case 4: + _door = CountDoors.Four; + break; + case 5: + _door = CountDoors.Five; + break; + default: + _door = CountDoors.Three; + break; + } + } + + public void Draw (Graphics2D g, int _startPosX, int _startPosY) { + g.setColor(Color.BLACK); + if (_door == CountDoors.Three) { + g.setColor(Color.gray); + g.fillRect(_startPosX + 40, _startPosY + 75, 12, 20); + g.fillRect(_startPosX + 60, _startPosY + 75, 12, 20); + g.fillRect(_startPosX + 80, _startPosY + 75, 12, 20); + } + if (_door == CountDoors.Four) { + g.setColor(Color.gray); + g.fillRect(_startPosX + 40, _startPosY + 75, 12, 20); + g.fillRect(_startPosX + 60, _startPosY + 75, 12, 20); + g.fillRect(_startPosX + 80, _startPosY + 75, 12, 20); + g.fillRect(_startPosX + 100, _startPosY + 75, 12, 20); + } + if (_door == CountDoors.Five){ + g.setColor(Color.gray); + g.fillRect(_startPosX + 40, _startPosY + 75, 12, 20); + g.fillRect(_startPosX + 60, _startPosY + 75, 12, 20); + g.fillRect(_startPosX + 80, _startPosY + 75, 12, 20); + g.fillRect(_startPosX + 100, _startPosY + 75, 12, 20); + g.fillRect(_startPosX + 120, _startPosY + 75, 12, 20); + } + } +} \ No newline at end of file diff --git a/src/Entities/CountDoors.java b/src/Entities/CountDoors.java new file mode 100644 index 0000000..0460764 --- /dev/null +++ b/src/Entities/CountDoors.java @@ -0,0 +1,6 @@ +package Entities; +public enum CountDoors { + Three, + Four, + Five; +} \ No newline at end of file diff --git a/src/Entities/EntityBus.java b/src/Entities/EntityBus.java new file mode 100644 index 0000000..509e9cd --- /dev/null +++ b/src/Entities/EntityBus.java @@ -0,0 +1,47 @@ +package Entities; + +import java.awt.*; + +public class EntityBus { + private int Speed; + private float Weight; + private Color BodyColor; + private Color AdditionalColor; + private boolean IsSecondFloor; + private boolean IsStairs; + + public int Step; + + public int getSpeed() { + return Speed; + } + public float getWeight() { + return Weight; + } + public Color getBodyColor() { + return BodyColor; + } + public Color getAdditionalColor() { + return AdditionalColor; + } + public boolean IsSecondFloor() { + return IsSecondFloor; + } + public boolean IsStairs() { + return IsStairs; + } + + + public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isSecondFloor, boolean isStairs) + { + + Weight = weight; + Speed = speed; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + IsSecondFloor = isSecondFloor; + IsStairs = isStairs; + + Step = Speed * 100 / (int) Weight; + } +} diff --git a/src/FormBus.java b/src/FormBus.java new file mode 100644 index 0000000..3018d1b --- /dev/null +++ b/src/FormBus.java @@ -0,0 +1,151 @@ +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; +import java.util.Random; +import Drawnings.*; +import MovementStrategy.*; + +public class FormBus extends JFrame { + + private DrawingBus _drawingBus; + private Canvas canvas = new Canvas(); + + JLabel labelCount = new JLabel("Введите число дверей:"); + private JTextField fieldCount = new JTextField(); + private JButton buttonCreate; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonRight; + private JButton buttonLeft; + + public FormBus() { + super("Создание автобуса"); + InitializeComponent(); + setVisible(true); + } + + private void InitializeComponent() + { + buttonCreate = new JButton("Создать автобус"); + + buttonUp = new JButton(); + buttonUp.setName("up"); + buttonUp.setIcon(new ImageIcon("images/KeyUp.png")); + buttonUp.setSize(48, 44); + + buttonRight = new JButton(); + buttonRight.setName("right"); + buttonRight.setIcon(new ImageIcon("images/KeyRight.png")); + buttonRight.setSize(48, 44); + + buttonLeft = new JButton(); + + buttonLeft.setName("left"); + buttonLeft.setIcon(new ImageIcon("images/KeyLeft.png")); + buttonLeft.setSize(48, 44); + + buttonDown = new JButton(); + buttonDown.setName("down"); + buttonDown.setIcon(new ImageIcon("images/KeyDown.png")); + buttonDown.setSize(48, 44); + + setSize(800,500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(null); + + buttonCreate.setBounds(12, 375, 170, 44); + buttonUp.setBounds(679, 363, 48, 44); + buttonRight.setBounds( 728, 408, 48, 44); + buttonLeft.setBounds(630, 408, 48, 44); + buttonDown.setBounds( 679, 408, 48, 44); + labelCount.setBounds(12, 435, 240, 20); + fieldCount.setBounds(160, 437, 48, 20); + canvas.setBounds(0,0,800, 460); + + add(buttonCreate); + add(buttonUp); + add(buttonRight); + add(buttonDown); + add(buttonLeft); + add(labelCount); + add(fieldCount); + add(canvas); + + buttonCreate.addActionListener(buttonCreateListener); + buttonUp.addActionListener(buttonsMoveListener); + buttonRight.addActionListener(buttonsMoveListener); + buttonDown.addActionListener(buttonsMoveListener); + buttonLeft.addActionListener(buttonsMoveListener); + } + + ActionListener buttonCreateListener = new ActionListener() { + public void actionPerformed(ActionEvent e) { + int countDoors; + try + { + countDoors = Integer.parseInt(fieldCount.getText()); + } + catch (Exception ex) + { + countDoors = 0; + } + if (countDoors != 3 && countDoors != 4 && countDoors != 5) + { + JOptionPane.showMessageDialog(null, "Число должно быть равно 3, 4 или 5.\nКол-во дверей приравнено к 3"); + countDoors = 3; + } + + Random rand = new Random(); + _drawingBus = new DrawingBus(); + _drawingBus.Init(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), + new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), + rand.nextBoolean(), rand.nextBoolean(), + countDoors, + canvas.getWidth(), canvas.getHeight()); + + _drawingBus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); + canvas.repaint(); + } + }; + + ActionListener buttonsMoveListener = new ActionListener() { + // реакция на нажатие + public void actionPerformed(ActionEvent e) { + if (_drawingBus == null) + { + return; + } + String command = ((JButton)(e.getSource())).getName(); + switch (command) { + case "up": + _drawingBus.MoveTransport(Direction.Up); + break; + case "down": + _drawingBus.MoveTransport(Direction.Down); + break; + case "right": + _drawingBus.MoveTransport(Direction.Right); + break; + case "left": + _drawingBus.MoveTransport(Direction.Left); + break; + } + canvas.repaint(); + } + }; + + class Canvas extends JComponent{ + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingBus == null){ + return; + } + super.paintComponents (g); + Graphics2D g2d = (Graphics2D)g; + _drawingBus.DrawTransport(g2d); + super.repaint(); + } + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..0805921 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + new FormBus(); + } +} \ No newline at end of file diff --git a/src/MovementStrategy/Direction.java b/src/MovementStrategy/Direction.java new file mode 100644 index 0000000..3441376 --- /dev/null +++ b/src/MovementStrategy/Direction.java @@ -0,0 +1,7 @@ +package MovementStrategy; +public enum Direction { + Up, + Down, + Left, + Right; +} \ No newline at end of file