From bc9d72d0a21a84766bbad65cc32be1b5b3473a48 Mon Sep 17 00:00:00 2001 From: Esenia12 <148366616+Esenia12@users.noreply.github.com> Date: Sun, 14 Apr 2024 23:55:13 +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 --- src/src/CanvasDumpTrack.java | 16 +++++ src/src/DirectionType.java | 6 ++ src/src/DrawingDumpTrack.java | 110 ++++++++++++++++++++++++++++++ src/src/DrawingWheels.java | 54 +++++++++++++++ src/src/EntityDumpTrack.java | 25 +++++++ src/src/FormDumpTrack.java | 124 ++++++++++++++++++++++++++++++++++ src/src/WheelNumber.java | 7 ++ 7 files changed, 342 insertions(+) create mode 100644 src/src/CanvasDumpTrack.java create mode 100644 src/src/DirectionType.java create mode 100644 src/src/DrawingDumpTrack.java create mode 100644 src/src/DrawingWheels.java create mode 100644 src/src/EntityDumpTrack.java create mode 100644 src/src/FormDumpTrack.java create mode 100644 src/src/WheelNumber.java diff --git a/src/src/CanvasDumpTrack.java b/src/src/CanvasDumpTrack.java new file mode 100644 index 0000000..8ef9bc5 --- /dev/null +++ b/src/src/CanvasDumpTrack.java @@ -0,0 +1,16 @@ +import javax.swing.*; +import java.awt.*; + +public class CanvasDumpTrack extends JComponent { + public DrawingDumpTrack _drawingDumpTrack; + public CanvasDumpTrack(){} + public void paintComponent(Graphics g) { + if (_drawingDumpTrack == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawingDumpTrack.DrawTransport(g2d); + super.repaint(); + } +} \ No newline at end of file diff --git a/src/src/DirectionType.java b/src/src/DirectionType.java new file mode 100644 index 0000000..d3a2058 --- /dev/null +++ b/src/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} \ No newline at end of file diff --git a/src/src/DrawingDumpTrack.java b/src/src/DrawingDumpTrack.java new file mode 100644 index 0000000..f029fae --- /dev/null +++ b/src/src/DrawingDumpTrack.java @@ -0,0 +1,110 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawingDumpTrack extends JPanel { + public EntityDumpTrack EntityDumpTrack; + public DrawingWheels _wheels = null; + private Integer picture_width; + private Integer picture_height; + private Integer _StartPosX; + private Integer _StartPosY; + private int drawingDumpTrackWidth = 155; + private int drawingDumpTrackHeight = 85; + public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean _bodyWork, boolean _awning) { + EntityDumpTrack = new EntityDumpTrack(); + EntityDumpTrack.Init(speed, weight, bodycolor, additionalcolor, _bodyWork, _awning); + picture_width = null; + picture_height = null; + _StartPosX = null; + _StartPosY = null; + _wheels = new DrawingWheels(); + _wheels.setWheelNumber((int)(Math.random() * 8 + 0)); + } + public boolean SetPictureSize(int width, int height) { + if (width < drawingDumpTrackWidth || height < drawingDumpTrackHeight) return false; + picture_width = width; + picture_height = height; + if (_StartPosX != null || _StartPosY != null) { + if (_StartPosX + drawingDumpTrackWidth > picture_width) + { + _StartPosX = _StartPosX - (_StartPosX + drawingDumpTrackWidth - picture_width); + } + else if (_StartPosX < 0) _StartPosX = 0; + if (_StartPosY + drawingDumpTrackHeight > picture_height) + { + _StartPosY = _StartPosY - (_StartPosY + drawingDumpTrackHeight - picture_height); + } + else if (_StartPosY < 0) _StartPosY = 0; + } + return true; + } + public void SetPosition(int x, int y) + { + if (x < 0 || x + drawingDumpTrackWidth > picture_width) { x = 0; } + if (y < 0 || y + drawingDumpTrackHeight > picture_height) { y = 0; } + _StartPosX = x; + _StartPosY = y; + } + public boolean MoveTransport(DirectionType direction) { + if (EntityDumpTrack == null || _StartPosX == null || _StartPosY == null) return false; + switch (direction) { + case Left: + if (_StartPosX - EntityDumpTrack.Step > 0) { + _StartPosX -= (int)EntityDumpTrack.Step; + } + return true; + case Up: + if (_StartPosY - EntityDumpTrack.Step > 0) + { + _StartPosY -= (int)EntityDumpTrack.Step; + } + return true; + case Right: + if (_StartPosX + drawingDumpTrackWidth + (int)EntityDumpTrack.Step < picture_width - EntityDumpTrack.Step) + { + _StartPosX += (int)EntityDumpTrack.Step; + } + return true; + case Down: + if (_StartPosY + drawingDumpTrackHeight + (int)EntityDumpTrack.Step < picture_height - EntityDumpTrack.Step) + { + _StartPosY += (int)EntityDumpTrack.Step; + } + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics2D g2D) + { + if (EntityDumpTrack == null) { + return; + } + g2D.setStroke(new BasicStroke(3)); + g2D.setPaint(EntityDumpTrack.getBodyColor()); + g2D.fillRect(_StartPosX, _StartPosY + 40, 160, 10); + g2D.fillRect(_StartPosX + 120, _StartPosY, 40, 40); + _wheels.drawWheels(g2D, Color.BLACK, _StartPosX, _StartPosY); + if (EntityDumpTrack.getBodyWork()) + { + g2D.setPaint(EntityDumpTrack.getAdditionalColor()); + Polygon dumpBoxPolygon = new Polygon(); + dumpBoxPolygon.addPoint(_StartPosX + 20, _StartPosY); + dumpBoxPolygon.addPoint(_StartPosX + 120, _StartPosY); + dumpBoxPolygon.addPoint(_StartPosX + 100, _StartPosY + 39); + dumpBoxPolygon.addPoint(_StartPosX, _StartPosY + 39); + g2D.fillPolygon(dumpBoxPolygon); + } + if (EntityDumpTrack.getAwning()) + { + g2D.setPaint(EntityDumpTrack.getAdditionalColor()); + Polygon tentPolygon = new Polygon(); + tentPolygon.addPoint(_StartPosX + 15, _StartPosY); + tentPolygon.addPoint(_StartPosX + 120, _StartPosY); + tentPolygon.addPoint(_StartPosX + 115, _StartPosY + 10); + tentPolygon.addPoint(_StartPosX + 10, _StartPosY + 10); + g2D.fillPolygon(tentPolygon); + } + } +} diff --git a/src/src/DrawingWheels.java b/src/src/DrawingWheels.java new file mode 100644 index 0000000..78cf570 --- /dev/null +++ b/src/src/DrawingWheels.java @@ -0,0 +1,54 @@ +import java.awt.*; + +public class DrawingWheels { + + private WheelNumber wheelNumber; + + public void setWheelNumber(int number) { + switch (number) { + case 2: + wheelNumber = wheelNumber.Two; + break; + case 3: + wheelNumber = wheelNumber.Three; + break; + case 4: + wheelNumber = wheelNumber.Four; + break; + default: wheelNumber = wheelNumber.Two; + } + } + + public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + switch (wheelNumber) { + case Two: + drawTwoWheels(g2D, color, _startPosX, _startPosY); + break; + case Three: + drawThreeWheels(g2D, color, _startPosX, _startPosY); + break; + case Four: + drawFourWheels(g2D, color, _startPosX, _startPosY); + break; + } + } + + private void drawTwoWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + g2D.fillOval(_startPosX, _startPosY + 50, 40, 40); + g2D.fillOval(_startPosX + 120, _startPosY + 50, 40, 40); + } + + private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + drawTwoWheels(g2D, color, _startPosX, _startPosY); + g2D.fillOval(_startPosX + 40, _startPosY + 50, 40, 40); + } + + private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + drawThreeWheels(g2D, color, _startPosX, _startPosY); + g2D.fillOval(_startPosX + 80, _startPosY + 50, 40, 40); + } +} + diff --git a/src/src/EntityDumpTrack.java b/src/src/EntityDumpTrack.java new file mode 100644 index 0000000..102fcb2 --- /dev/null +++ b/src/src/EntityDumpTrack.java @@ -0,0 +1,25 @@ +import java.awt.*; +public class EntityDumpTrack { + private int Speed; + private double Weight; + private Color BodyColor; + public Color getBodyColor() {return BodyColor;} + private Color AdditionalColor; + public Color getAdditionalColor() {return AdditionalColor;} + private boolean BodyWork; + public boolean getBodyWork() {return BodyWork;} + private boolean Awning; + public boolean getAwning() {return Awning;} + public double Step; + public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean _bodyWork, boolean _awning) + { + Speed = speed; + Weight = weight; + BodyColor = bodycolor; + AdditionalColor = additionalcolor; + BodyWork = _bodyWork; + Awning = _awning; + Step = Speed * 100 / Weight; + } +} + diff --git a/src/src/FormDumpTrack.java b/src/src/FormDumpTrack.java new file mode 100644 index 0000000..32f2e71 --- /dev/null +++ b/src/src/FormDumpTrack.java @@ -0,0 +1,124 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; + +public class FormDumpTrack extends JFrame { + private String title; + private Dimension dimension; + private int Width, Height; + private CanvasDumpTrack canvasAirbus = new CanvasDumpTrack(); + private JButton CreateButton = new JButton("Create");; + private JButton UpButton = new JButton(); + private JButton DownButton = new JButton();; + private JButton LeftButton = new JButton();; + private JButton RightButton = new JButton(); + public FormDumpTrack(String title, Dimension dimension) { + this.title = title; + this.dimension = dimension; + } + public void Init() { + setTitle(title); + setMinimumSize(dimension); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + Width = getWidth() - 15; + Height = getHeight() - 35; + + CreateButton.setName("CREATE"); + Icon iconUp = new ImageIcon("src\\images\\up.jpg"); + UpButton.setIcon(iconUp); + UpButton.setName("UP"); + DownButton.setName("DOWN"); + Icon iconDown = new ImageIcon("src\\images\\down.jpg"); + DownButton.setIcon(iconDown); + LeftButton.setName("LEFT"); + Icon iconLeft = new ImageIcon("src\\images\\left.jpg"); + LeftButton.setIcon(iconLeft); + RightButton.setName("RIGHT"); + Icon iconRight = new ImageIcon("src\\images\\right.jpg"); + RightButton.setIcon(iconRight); + + CreateButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int StartPositionX = (int)(Math.random() * 90 + 10); + int StartPositionY = (int)(Math.random() * 90 + 10); + int speed = (int)(Math.random() * 300 + 100); + double weight = (double)(Math.random() * 3000 + 1000); + Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); + Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; + boolean sheepPipes = new Random().nextBoolean(); + boolean fuelTank = new Random().nextBoolean();; + canvasAirbus._drawingDumpTrack = new DrawingDumpTrack(); + canvasAirbus._drawingDumpTrack.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank); + canvasAirbus._drawingDumpTrack.SetPictureSize(Width, Height); + canvasAirbus._drawingDumpTrack.SetPosition(StartPositionX, StartPositionY); + canvasAirbus.repaint(); + } + }); + + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + if (canvasAirbus._drawingDumpTrack == null) return; + boolean result = false; + switch ((((JButton)(event.getSource())).getName())) { + case "UP": + result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Up); + break; + case "DOWN": + result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Down); + break; + case "LEFT": + result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Left); + break; + case "RIGHT": + result = canvasAirbus._drawingDumpTrack.MoveTransport(DirectionType.Right); + break; + } + if (result) { + canvasAirbus.repaint(); + } + } + }; + UpButton.addActionListener(actionListener); + DownButton.addActionListener(actionListener); + LeftButton.addActionListener(actionListener); + RightButton.addActionListener(actionListener); + + setSize(dimension.width,dimension.height); + setLayout(null); + canvasAirbus.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 100, 40); + UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); + DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); + RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); + LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); + add(CreateButton); + add(UpButton); + add(DownButton); + add(RightButton); + add(LeftButton); + add(canvasAirbus); + setVisible(true); + //обработка события изменения размеров окна + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + Width = getWidth() - 15; + Height = getHeight() - 35; + if (canvasAirbus._drawingDumpTrack != null) + canvasAirbus._drawingDumpTrack.SetPictureSize(Width, Height); + canvasAirbus.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 100, 40); + UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); + DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); + RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); + LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); + } + }); + } +} + diff --git a/src/src/WheelNumber.java b/src/src/WheelNumber.java new file mode 100644 index 0000000..1a692c7 --- /dev/null +++ b/src/src/WheelNumber.java @@ -0,0 +1,7 @@ +public enum WheelNumber { + Two, + + Three, + + Four +} -- 2.25.1