From 3ab0eaf8c8b0c994ed1d606f345a3365d823e54f Mon Sep 17 00:00:00 2001 From: qkrlnt Date: Mon, 6 May 2024 23:06:10 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectMonorail/.idea/libraries/lib.xml | 10 - ProjectMonorail/.idea/misc.xml | 5 + ProjectMonorail/.idea/uiDesigner.xml | 124 +++++++++++ ProjectMonorail/ProjectMonorail.iml | 1 - ProjectMonorail/src/DirectionType.java | 6 + ProjectMonorail/src/DrawingMonorail.java | 192 ++++++++++++++++++ .../src/DrawingMonorailWheels.java | 44 ++++ ProjectMonorail/src/EntityMonorail.java | 32 +++ ProjectMonorail/src/FormMonorail.form | 91 +++++++++ ProjectMonorail/src/FormMonorail.java | 114 +++++++++++ ProjectMonorail/src/Main.java | 10 + ProjectMonorail/src/WheelsCount.java | 14 ++ 12 files changed, 632 insertions(+), 11 deletions(-) delete mode 100644 ProjectMonorail/.idea/libraries/lib.xml create mode 100644 ProjectMonorail/.idea/uiDesigner.xml create mode 100644 ProjectMonorail/src/DirectionType.java create mode 100644 ProjectMonorail/src/DrawingMonorail.java create mode 100644 ProjectMonorail/src/DrawingMonorailWheels.java create mode 100644 ProjectMonorail/src/EntityMonorail.java create mode 100644 ProjectMonorail/src/FormMonorail.form create mode 100644 ProjectMonorail/src/FormMonorail.java create mode 100644 ProjectMonorail/src/WheelsCount.java diff --git a/ProjectMonorail/.idea/libraries/lib.xml b/ProjectMonorail/.idea/libraries/lib.xml deleted file mode 100644 index 5cded49..0000000 --- a/ProjectMonorail/.idea/libraries/lib.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/ProjectMonorail/.idea/misc.xml b/ProjectMonorail/.idea/misc.xml index 69ace3f..4817050 100644 --- a/ProjectMonorail/.idea/misc.xml +++ b/ProjectMonorail/.idea/misc.xml @@ -1,5 +1,10 @@ + + + + + diff --git a/ProjectMonorail/.idea/uiDesigner.xml b/ProjectMonorail/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/ProjectMonorail/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ProjectMonorail/ProjectMonorail.iml b/ProjectMonorail/ProjectMonorail.iml index fb8e866..c90834f 100644 --- a/ProjectMonorail/ProjectMonorail.iml +++ b/ProjectMonorail/ProjectMonorail.iml @@ -7,6 +7,5 @@ - \ No newline at end of file diff --git a/ProjectMonorail/src/DirectionType.java b/ProjectMonorail/src/DirectionType.java new file mode 100644 index 0000000..35657f0 --- /dev/null +++ b/ProjectMonorail/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} diff --git a/ProjectMonorail/src/DrawingMonorail.java b/ProjectMonorail/src/DrawingMonorail.java new file mode 100644 index 0000000..6e7a58f --- /dev/null +++ b/ProjectMonorail/src/DrawingMonorail.java @@ -0,0 +1,192 @@ +import java.awt.*; +import java.util.Random; + +public class DrawingMonorail { + private EntityMonorail entityMonorail; + public EntityMonorail getEntityMonorail(){return entityMonorail;} + private DrawingMonorailWheels drawingMonorailWheels; + + private Integer _pictureWidth; + + private Integer _pictureHeight; + + private Integer _startPosX; + + private Integer _startPosY; + + private final int _drawingTrainWidth = 150; + + private final int _drawingTrainHeight = 40; + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean rail, boolean secondCarriage){ + entityMonorail = new EntityMonorail(); + entityMonorail.Init(speed, weight, bodyColor, additionalColor, rail, secondCarriage); + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + drawingMonorailWheels = new DrawingMonorailWheels(); + Random rand = new Random(); + int wheelsCount = rand.nextInt(2, 5); + drawingMonorailWheels.setEnumNumber(wheelsCount); + } + + public boolean SetPictureSize(int width, int height){ + if (_drawingTrainWidth <= width && _drawingTrainHeight <= height){ + _pictureWidth = width; + _pictureHeight = height; + if (_startPosX != null && _startPosY != null){ + if (_startPosX + _drawingTrainWidth > _pictureWidth) { + _startPosX = _pictureWidth - _drawingTrainWidth; + } + if (_startPosY + _drawingTrainHeight > _pictureHeight) { + _startPosY = _pictureHeight - _drawingTrainHeight; + } + } + return true; + } + return false; + } + + public void SetPosition(int x, int y) + { + if (_pictureHeight == null || _pictureWidth == null) { + return; + } + if (x + _drawingTrainWidth > _pictureWidth || x < 0) + { + x = 0; + } + if (y + _drawingTrainHeight > _pictureHeight || y < 0) + { + y = 0; + } + _startPosX = x; + _startPosY = y; + } + + public boolean MoveTransport(DirectionType direction){ + if (entityMonorail == null || _startPosX == null || _startPosY == null){ + return false; + } + + switch (direction){ + case Up: + if (_startPosY - entityMonorail.Step() > 0){ + _startPosY -= (int) entityMonorail.Step(); + } + return true; + case Down: + if (_startPosY + entityMonorail.Step() + _drawingTrainHeight < _pictureHeight){ + _startPosY += (int) entityMonorail.Step(); + } + return true; + case Left: + if (_startPosX - entityMonorail.Step() > 0){ + _startPosX -= (int) entityMonorail.Step(); + } + return true; + case Right: + if (_startPosX + entityMonorail.Step() + _drawingTrainWidth < _pictureWidth){ + _startPosX += (int) entityMonorail.Step(); + } + return true; + default: return false; + } + } + + public void DrawMonorail(Graphics g) { + if (entityMonorail == null || _startPosX == null || _startPosY == null) { + return; + } + + drawingMonorailWheels.drawMonorailWheels(g, entityMonorail.getAdditionalColor(), _startPosX, _startPosY); + + Graphics2D g2d = (Graphics2D) g; + + Point[] monorailBorders = new Point[] { + new Point(_startPosX + 10, _startPosY), + new Point(_startPosX + 85, _startPosY), + new Point(_startPosX + 85, _startPosY + 30), + new Point(_startPosX + 5, _startPosY + 30), + new Point(_startPosX + 5, _startPosY + 15) + }; + + Polygon monorailPolygon = new Polygon(); + for (Point point : monorailBorders) + monorailPolygon.addPoint(point.x, point.y); + g2d.setColor(Color.BLACK); + g2d.draw(monorailPolygon); + g2d.setColor(entityMonorail.getBodyColor()); + g2d.fill(monorailPolygon); + + Point[][] carts = new Point[][] { + {new Point(_startPosX, _startPosY + 35), + new Point(_startPosX + 15, _startPosY + 35), + new Point(_startPosX + 15, _startPosY + 30), + new Point(_startPosX + 5, _startPosY + 30)}, + {new Point(_startPosX + 20, _startPosY + 30), + new Point(_startPosX + 25, _startPosY + 35), + new Point(_startPosX + 30, _startPosY + 35), + new Point(_startPosX + 35, _startPosY + 30)}, + {new Point(_startPosX + 55, _startPosY + 30), + new Point(_startPosX + 60, _startPosY + 35), + new Point(_startPosX + 65, _startPosY + 35), + new Point(_startPosX + 70, _startPosY + 30)}, + {new Point(_startPosX + 70, _startPosY + 30), + new Point(_startPosX + 75, _startPosY + 35), + new Point(_startPosX + 90, _startPosY + 35), + new Point(_startPosX + 85, _startPosY + 30)} + }; + + + g2d.setColor(Color.BLACK); + for (int i = 0; i < 4; i++){ + Polygon cartPolygon = new Polygon(); + for (Point point : carts[i]) + cartPolygon.addPoint(point.x, point.y); + g2d.fill(cartPolygon); + } + + g2d.drawRect(_startPosX + 17, _startPosY + 5, 17, 7); + g2d.drawRect(_startPosX + 55, _startPosY + 5, 27, 7); + g2d.drawRect(_startPosX + 40, _startPosY + 7, 10, 21); + + g2d.setColor(Color.CYAN); + g2d.fillRect(_startPosX + 18, _startPosY + 6, 16, 6); + g2d.fillRect(_startPosX + 56, _startPosY + 6, 26, 6); + g2d.setColor(Color.WHITE); + g2d.fillRect(_startPosX + 41, _startPosY + 8, 9, 20); + + + if (entityMonorail.getRail()) { + g2d.setColor(entityMonorail.getAdditionalColor()); + if (entityMonorail.getSecondCarriage()){ + g2d.fillRect(_startPosX, _startPosY + 40, 150, 3); + } + else{ + g2d.fillRect(_startPosX, _startPosY + 40, 90, 3); + } + } + if (entityMonorail.getSecondCarriage()) { + g2d.setColor(Color.BLACK); + g2d.drawRect(_startPosX + 95, _startPosY, 55, 30); + g2d.setColor(entityMonorail.getAdditionalColor()); + g2d.fillRect(_startPosX + 96, _startPosY + 1, 54, 29); + g2d.setColor(Color.BLACK); + g2d.fillRect(_startPosX + 85, _startPosY + 5, 10, 23); + g2d.drawRect(_startPosX + 105, _startPosY + 5, 35, 10); + g2d.setColor(Color.CYAN); + g2d.fillRect(_startPosX + 106, _startPosY + 6, 34, 9); + g2d.setColor(Color.BLACK); + g2d.drawRect(_startPosX + 105, _startPosY + 5, 10, 23); + g2d.setColor(Color.WHITE); + g2d.fillRect(_startPosX + 106, _startPosY + 16, 9, 12); + g2d.setColor(Color.BLACK); + g2d.drawOval(_startPosX + 100, _startPosY + 30, 10, 10); + g2d.drawOval(_startPosX + 110, _startPosY + 30, 10, 10); + g2d.drawOval(_startPosX + 130, _startPosY + 30, 10, 10); + g2d.drawOval(_startPosX + 140, _startPosY + 30, 10, 10); + } + } +} \ No newline at end of file diff --git a/ProjectMonorail/src/DrawingMonorailWheels.java b/ProjectMonorail/src/DrawingMonorailWheels.java new file mode 100644 index 0000000..ea594c2 --- /dev/null +++ b/ProjectMonorail/src/DrawingMonorailWheels.java @@ -0,0 +1,44 @@ +import java.awt.*; + +public class DrawingMonorailWheels { + private WheelsCount _wheelsCount; + + public void setEnumNumber(int wheelsCount){ + for (WheelsCount value : WheelsCount.values()){ + if (value.getEnumNumber() == wheelsCount){ + _wheelsCount = value; + return; + } + } + } + + public void drawWheels(Graphics2D g2d, Color color, int x, int y){ + g2d.setColor(Color.BLACK); + g2d.drawOval(x, y + 30, 10, 10); + g2d.setColor(color); + g2d.fillOval(x, y + 30, 10, 10); + } + + public void drawMonorailWheels(Graphics g, Color color, int startPosX, int startPosY){ + Graphics2D g2d = (Graphics2D) g; + switch (_wheelsCount){ + case Two: + drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY); + drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY); + break; + case Three: + drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY); + drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY); + drawWheels(g2d, color, startPosX + 30, startPosY); + break; + case Four: + drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY); + drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY); + drawWheels(g2d, color, startPosX + 30, startPosY); + drawWheels(g2d, color, startPosX + 50, startPosY); + break; + default: + break; + } + } +} diff --git a/ProjectMonorail/src/EntityMonorail.java b/ProjectMonorail/src/EntityMonorail.java new file mode 100644 index 0000000..ba3d500 --- /dev/null +++ b/ProjectMonorail/src/EntityMonorail.java @@ -0,0 +1,32 @@ +import java.awt.*; + +public class EntityMonorail { + 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 Rail; + public boolean getRail(){return Rail;} + + private boolean SecondCarriage; + public boolean getSecondCarriage(){return SecondCarriage;} + + public double Step(){return Speed*100/Weight;} + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean rail, boolean secondCarriage){ + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Rail = rail; + SecondCarriage = secondCarriage; + } +} diff --git a/ProjectMonorail/src/FormMonorail.form b/ProjectMonorail/src/FormMonorail.form new file mode 100644 index 0000000..cb39368 --- /dev/null +++ b/ProjectMonorail/src/FormMonorail.form @@ -0,0 +1,91 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectMonorail/src/FormMonorail.java b/ProjectMonorail/src/FormMonorail.java new file mode 100644 index 0000000..3133de1 --- /dev/null +++ b/ProjectMonorail/src/FormMonorail.java @@ -0,0 +1,114 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.LinkedList; +import java.util.Random; +import java.util.List; + +public class FormMonorail extends JFrame { + protected DrawingMonorail _drawingMonorail = new DrawingMonorail(); + JPanel PictureBox; + private JButton buttonCreate; + private JButton buttonRight; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonUp; + + private List controls; + + public FormMonorail() { + buttonUp.setName("buttonUp"); + buttonDown.setName("buttonDown"); + buttonLeft.setName("buttonLeft"); + buttonRight.setName("buttonRight"); + + InitializeControlsRepaintList(); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _drawingMonorail = new DrawingMonorail(); + Random random = new Random(); + + _drawingMonorail.Init(random.nextInt(30, 100), + random.nextInt(100, 500), + 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()); + _drawingMonorail.SetPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); + _drawingMonorail.SetPosition(random.nextInt(25, 100), + random.nextInt(25, 100)); + + Draw(); + + } + }); + ActionListener buttonMoveClickedListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String buttonName = ((JButton) e.getSource()).getName(); + boolean result = false; + + switch (buttonName) { + case "buttonUp": { + result = _drawingMonorail.MoveTransport(DirectionType.Up); + } + break; + case "buttonDown": { + result = _drawingMonorail.MoveTransport(DirectionType.Down); + } + break; + case "buttonLeft": { + result = _drawingMonorail.MoveTransport(DirectionType.Left); + } + break; + case "buttonRight": { + result = _drawingMonorail.MoveTransport(DirectionType.Right); + } + break; + + } + if (result) + Draw(); + + } + }; + buttonRight.addActionListener(buttonMoveClickedListener); + buttonDown.addActionListener(buttonMoveClickedListener); + buttonLeft.addActionListener(buttonMoveClickedListener); + buttonUp.addActionListener(buttonMoveClickedListener); + + } + + private void Draw() { + if (_drawingMonorail.getEntityMonorail() == null) + return; + if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) { + return; + } + Graphics g = PictureBox.getGraphics(); + g.setColor(PictureBox.getBackground()); + g.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); + _drawingMonorail.DrawMonorail(g); + + RepaintControls(); + + } + + private void RepaintControls() { + for (JComponent control : controls) { + control.repaint(); + } + } + + + private void InitializeControlsRepaintList() { + controls = new LinkedList<>(); + controls.add(buttonCreate); + controls.add(buttonUp); + controls.add(buttonDown); + controls.add(buttonLeft); + controls.add(buttonRight); + } +} diff --git a/ProjectMonorail/src/Main.java b/ProjectMonorail/src/Main.java index f5858db..62674ad 100644 --- a/ProjectMonorail/src/Main.java +++ b/ProjectMonorail/src/Main.java @@ -1,5 +1,15 @@ +import javax.swing.*; + public class Main { public static void main(String[] args) { + JFrame.setDefaultLookAndFeelDecorated(false); + JFrame frame = new JFrame("Монорельс"); + frame.setContentPane(new FormMonorail().PictureBox); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocation(500, 200); + frame.pack(); + frame.setSize(700, 500); + frame.setVisible(true); } } \ No newline at end of file diff --git a/ProjectMonorail/src/WheelsCount.java b/ProjectMonorail/src/WheelsCount.java new file mode 100644 index 0000000..98a4684 --- /dev/null +++ b/ProjectMonorail/src/WheelsCount.java @@ -0,0 +1,14 @@ +public enum WheelsCount { + Two(2), + Three(3), + Four(4); + + final private int EnumNumber; + WheelsCount(int enumNumber){ + EnumNumber = enumNumber; + } + + public int getEnumNumber(){ + return EnumNumber; + } +} -- 2.25.1