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 0000000..a967fa6 Binary files /dev/null and b/images/KeyDown.png differ diff --git a/images/KeyLeft.png b/images/KeyLeft.png new file mode 100644 index 0000000..3001b69 Binary files /dev/null and b/images/KeyLeft.png differ diff --git a/images/KeyRight.png b/images/KeyRight.png new file mode 100644 index 0000000..472c3b3 Binary files /dev/null and b/images/KeyRight.png differ diff --git a/images/KeyUp.png b/images/KeyUp.png new file mode 100644 index 0000000..ad88add Binary files /dev/null and b/images/KeyUp.png differ diff --git a/src/Drawnings/DrawingBus.java b/src/Drawnings/DrawingBus.java new file mode 100644 index 0000000..754c877 --- /dev/null +++ b/src/Drawnings/DrawingBus.java @@ -0,0 +1,144 @@ +package Drawnings; + +import java.awt.*; + +import Entities.EntityBus; +import MovementStrategy.*; + +public class DrawingBus { + + public EntityBus entityBus; + public DrawingDoors _door; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private int _busWidth = 190; + private int _busHeight = 115; + + public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isSecondFloor, boolean isStairs, int countDoors, int width, int height) { + if (width < _busHeight || height < _busWidth) + return; + _pictureWidth = width; + _pictureHeight = height; + entityBus = new EntityBus(); + entityBus.Init(speed, weight, bodyColor, additionalColor, isSecondFloor, isStairs); + + _door = new DrawingDoors(); + _door.SetCount(countDoors); + } + + public void SetPosition (int x, int y) { + if (x + _busWidth > _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