diff --git a/.gitignore b/.gitignore index 9154f4c..0acf39d 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,5 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* replay_pid* +.idea diff --git a/DirectionType.java b/DirectionType.java new file mode 100644 index 0000000..d3a2058 --- /dev/null +++ b/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} \ No newline at end of file diff --git a/DoorNumberType.java b/DoorNumberType.java new file mode 100644 index 0000000..d3aa9f3 --- /dev/null +++ b/DoorNumberType.java @@ -0,0 +1,5 @@ +public enum DoorNumberType { + Three, + Four, + Five +} \ No newline at end of file diff --git a/DrawningDoor.java b/DrawningDoor.java new file mode 100644 index 0000000..abe1346 --- /dev/null +++ b/DrawningDoor.java @@ -0,0 +1,57 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawningDoor { + JPanel DoubleDeckerBusPanel; + private DoorNumberType DoorNumberType; + private Color blackColor; + private int Width; + private int Height; + public int CurX; + public int CurY; + + public boolean Init(int width, int height, int curX, int curY, JPanel doubleDeckerBusPanel) { + Width = width; + Height = height; + CurX = curX; + CurY = curY; + blackColor = Color.BLACK; + DoubleDeckerBusPanel = doubleDeckerBusPanel; + return true; + } + + public void ChangeDoorsNumber(int x) { + if (x <= 3) { + DoorNumberType = DoorNumberType.Three; + } + if (x == 4) { + DoorNumberType = DoorNumberType.Four; + } + if (x >= 5) { + DoorNumberType = DoorNumberType.Five; + } + } + + public DoorNumberType DoorNumberType() { + return DoorNumberType; + } + + public void DrawDoors() { + Graphics2D g2d = (Graphics2D) DoubleDeckerBusPanel.getGraphics(); + g2d.setColor(blackColor); + g2d.fillRect(CurX + 55, CurY + 65, 10, 15); + g2d.fillRect(CurX + 70, CurY + 65, 10, 15); + + if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) { + g2d.fillRect(CurX + 85, CurY + 65, 10, 15); + } + + if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) { + g2d.fillRect(CurX + 100, CurY + 65, 10, 15); + } + + if (DoorNumberType() == DoorNumberType.Five) { + g2d.fillRect(CurX + 115, CurY + 65, 10, 15); + } + } +} diff --git a/DrawningDoubleDeckerBus.java b/DrawningDoubleDeckerBus.java new file mode 100644 index 0000000..87b1cb6 --- /dev/null +++ b/DrawningDoubleDeckerBus.java @@ -0,0 +1,137 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawningDoubleDeckerBus { + JPanel DoubleDeckerBusPanel; + private EntityDoubleDeckerBus EntityDoubleDeckerBus; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX = 0; + private int _startPosY = 0; + private final int _busWidth = 160; + private final int _busHeight = 100; + private DrawningDoor DrawningDoor; + + public EntityDoubleDeckerBus EntityDoubleDeckerBus() { + return EntityDoubleDeckerBus; + } + + public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, int doorNumber, + int width, int height, boolean secondFloor, boolean tailpipe, JPanel doubleDeckerBusPanel) { + if (width <= _busWidth || height <= _busHeight) { + return false; + } + + _startPosX = 0; + _startPosY = 0; + doubleDeckerBusPanel.setSize(width, height); + DoubleDeckerBusPanel = doubleDeckerBusPanel; + doubleDeckerBusPanel.paint(DoubleDeckerBusPanel.getGraphics()); + _pictureWidth = width; + _pictureHeight = height; + EntityDoubleDeckerBus = new EntityDoubleDeckerBus(); + EntityDoubleDeckerBus.Init(speed, weight, bodyColor, additionalColor, doorNumber, secondFloor, tailpipe); + DrawningDoor = new DrawningDoor(); + DrawningDoor.Init(_busWidth, _busHeight, _startPosX, _startPosY, doubleDeckerBusPanel); + Random random = new Random(); + DrawningDoor.ChangeDoorsNumber(random.nextInt(5) + 2); + return true; + } + + public void SetPosition(int x, int y){ + if (EntityDoubleDeckerBus == null) { + return; + } + + _startPosX = x; + _startPosY = y; + + if (x + _busWidth <= _pictureWidth || y + _busHeight <= _pictureHeight) { + _startPosX = 0; + _startPosY = 0; + } + } + + public void MoveTransport(DirectionType direction){ + if (EntityDoubleDeckerBus == null) + return; + DoubleDeckerBusPanel.paint(DoubleDeckerBusPanel.getGraphics()); + switch (direction) + { + case Left: + if (_startPosX - EntityDoubleDeckerBus.Step() >= 0) + _startPosX -= (int)EntityDoubleDeckerBus.Step(); + else + _startPosX = 0; + break; + case Up: + if (_startPosY - EntityDoubleDeckerBus.Step() >= 0) + _startPosY -= (int)EntityDoubleDeckerBus.Step(); + else + _startPosY = 0; + break; + case Right: + if (_startPosX + EntityDoubleDeckerBus.Step() + _busWidth < _pictureWidth) + _startPosX += (int)EntityDoubleDeckerBus.Step(); + else + _startPosX = _pictureWidth - _busWidth; + break; + case Down: + if (_startPosY + EntityDoubleDeckerBus.Step() + _busHeight < _pictureHeight) + _startPosY += (int)EntityDoubleDeckerBus.Step(); + else + _startPosY = _pictureHeight - _busHeight; + break; + } + DrawningDoor.CurX = _startPosX; + DrawningDoor.CurY = _startPosY; + } + + public void DrawTransport() { + Graphics2D g2d = (Graphics2D) DoubleDeckerBusPanel.getGraphics(); + + if (EntityDoubleDeckerBus == null) { + return; + } + + // Границы первого этажа автобуса + g2d.setColor(EntityDoubleDeckerBus.BodyColor()); + g2d.fillRect(_startPosX + 40, _startPosY + 40, 120, 40); + + // Колеса + g2d.setColor(Color.black); + g2d.fillOval(_startPosX + 45, _startPosY + 75, 20, 20); + g2d.fillOval(_startPosX + 127, _startPosY + 75, 20, 20); + + // Окна + g2d.setColor(Color.blue); + g2d.fillOval(_startPosX + 50, _startPosY + 45, 20, 20); + g2d.fillOval(_startPosX + 75, _startPosY + 45, 20, 20); + g2d.fillOval(_startPosX + 100, _startPosY + 45, 20, 20); + g2d.fillOval(_startPosX + 125, _startPosY + 45, 20, 20); + + // Двери + DrawningDoor.DrawDoors(); + + // Второй этаж + if (EntityDoubleDeckerBus.SecondFloor()) { + g2d.setColor(EntityDoubleDeckerBus.AdditionalColor()); + // Границы второго этажа автобуса + g2d.fillRect(_startPosX + 40, _startPosY, 120, 40); + + // Окна второго этажа + g2d.setColor(Color.blue); + g2d.fillOval(_startPosX + 50, _startPosY +10,20, 20); + g2d.fillOval(_startPosX + 75, _startPosY +10, 20, 20); + g2d.fillOval(_startPosX + 100, _startPosY +10,20, 20); + g2d.fillOval(_startPosX + 125, _startPosY +10,20,20); + } + + if (EntityDoubleDeckerBus.Tailpipe()) { + g2d.setColor(EntityDoubleDeckerBus.AdditionalColor()); + g2d.fillRect(_startPosX, _startPosY + 65, 40, 15); + } + } + +} diff --git a/EntityDoubleDeckerBus.java b/EntityDoubleDeckerBus.java new file mode 100644 index 0000000..4f8d9ff --- /dev/null +++ b/EntityDoubleDeckerBus.java @@ -0,0 +1,55 @@ +import java.awt.*; + +public class EntityDoubleDeckerBus { + private int Speed; + private int DoorNumber; + private double Weight; + private Color BodyColor; + private Color AdditionalColor; + private boolean SecondFloor; + private boolean Tailpipe; + private double Step; + + public int Speed() { + return Speed; + } + + public int DoorNumber() { + return DoorNumber; + } + + public double Weight() { + return Weight; + } + + public Color BodyColor() { + return BodyColor; + } + + public Color AdditionalColor() { + return AdditionalColor; + } + + public boolean SecondFloor() { + return SecondFloor; + } + + public boolean Tailpipe() { + return Tailpipe; + } + public double Step() { + return Step; + } + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, + int doorNumber, boolean secondFloor ,boolean tailpipe) { + Speed = speed; + Weight = weight; + Step = (double) Speed * 100 / Weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + SecondFloor = secondFloor; + DoorNumber = doorNumber; + Tailpipe = tailpipe; + } +} diff --git a/FormDoubleDeckerBus.java b/FormDoubleDeckerBus.java new file mode 100644 index 0000000..cea8ab1 --- /dev/null +++ b/FormDoubleDeckerBus.java @@ -0,0 +1,94 @@ +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.Random; +import javax.imageio.ImageIO; +import javax.swing.*; + +public class FormDoubleDeckerBus { + public static void main(String[] args) throws IOException { + JFrame DoubleDeckerBusFrame = new JFrame(); + JPanel DoubleDeckerBusPanel = new JPanel(); + DoubleDeckerBusFrame.setLayout(new BorderLayout()); + DoubleDeckerBusFrame.setSize(900, 500); + DoubleDeckerBusFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + DoubleDeckerBusFrame.setLayout(new BorderLayout(1,1)); + DrawningDoubleDeckerBus DrawningDoubleDeckerBus = new DrawningDoubleDeckerBus(); + DoubleDeckerBusPanel.setLayout(null); + BufferedImage RightIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/right.png")); + BufferedImage LeftIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/left.png")); + BufferedImage UpIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/up.png")); + BufferedImage DownIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/down.png")); + + JButton RightButton = new JButton(new ImageIcon(RightIcon)); + JButton LeftButton = new JButton(new ImageIcon(LeftIcon)); + JButton UpButton = new JButton(new ImageIcon(UpIcon)); + JButton DownButton = new JButton(new ImageIcon(DownIcon)); + JButton CreateButton = new JButton(); + CreateButton.setText("Create"); + CreateButton.setBounds(12, 401, 90, 40); + RightButton.setBounds(840,411,30,30); + LeftButton.setBounds(768,411,30,30); + UpButton.setBounds(804,375,30,30); + DownButton.setBounds(804,411,30,30); + DoubleDeckerBusPanel.add(CreateButton); + DoubleDeckerBusPanel.add(RightButton); + DoubleDeckerBusPanel.add(LeftButton); + DoubleDeckerBusPanel.add(UpButton); + DoubleDeckerBusPanel.add(DownButton); + DoubleDeckerBusFrame.add(DoubleDeckerBusPanel, BorderLayout.CENTER); + Random random = new Random(); + CreateButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + DrawningDoubleDeckerBus.Init(random.nextInt(201) + 100, random.nextDouble() * 2000 + 1000, + Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), + Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), + random.nextInt(4) + 2, DoubleDeckerBusPanel.getWidth(), DoubleDeckerBusPanel.getHeight(), + random.nextBoolean(), random.nextBoolean(), DoubleDeckerBusPanel); + DrawningDoubleDeckerBus.DrawTransport(); + } + }); + RightButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null) + return; + DrawningDoubleDeckerBus.MoveTransport(DirectionType.Right); + DrawningDoubleDeckerBus.DrawTransport(); + } + }); + LeftButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null) + return; + DrawningDoubleDeckerBus.MoveTransport(DirectionType.Left); + DrawningDoubleDeckerBus.DrawTransport(); + } + }); + UpButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null) + return; + DrawningDoubleDeckerBus.MoveTransport(DirectionType.Up); + DrawningDoubleDeckerBus.DrawTransport(); + } + }); + DownButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null) + return; + DrawningDoubleDeckerBus.MoveTransport(DirectionType.Down); + DrawningDoubleDeckerBus.DrawTransport(); + } + }); + + DoubleDeckerBusFrame.setVisible(true); + } +} diff --git a/Resources/down.png b/Resources/down.png new file mode 100644 index 0000000..870965a Binary files /dev/null and b/Resources/down.png differ diff --git a/Resources/left.png b/Resources/left.png new file mode 100644 index 0000000..3636498 Binary files /dev/null and b/Resources/left.png differ diff --git a/Resources/right.png b/Resources/right.png new file mode 100644 index 0000000..b3ce576 Binary files /dev/null and b/Resources/right.png differ diff --git a/Resources/up.png b/Resources/up.png new file mode 100644 index 0000000..84db55c Binary files /dev/null and b/Resources/up.png differ diff --git a/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/.gitignore b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/.gitignore new file mode 100644 index 0000000..a8c58f8 --- /dev/null +++ b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/.gitignore @@ -0,0 +1,27 @@ +# ---> Java +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* + + diff --git a/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/README.md b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/README.md new file mode 100644 index 0000000..4a4ec21 --- /dev/null +++ b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/README.md @@ -0,0 +1,2 @@ +# PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard + diff --git a/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/down.png b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/down.png new file mode 100644 index 0000000..870965a Binary files /dev/null and b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/down.png differ diff --git a/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/left.png b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/left.png new file mode 100644 index 0000000..3636498 Binary files /dev/null and b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/left.png differ diff --git a/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/right.png b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/right.png new file mode 100644 index 0000000..b3ce576 Binary files /dev/null and b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/right.png differ diff --git a/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/up.png b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/up.png new file mode 100644 index 0000000..84db55c Binary files /dev/null and b/out/production/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/up.png differ