From ee561695c114e731c0220e98c1a22298d5ed33c7 Mon Sep 17 00:00:00 2001 From: insideq Date: Sat, 13 Apr 2024 17:55:17 +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 --- ProjectExcavator/res/down.png | Bin 0 -> 321 bytes ProjectExcavator/res/left.png | Bin 0 -> 270 bytes ProjectExcavator/res/right.png | Bin 0 -> 266 bytes ProjectExcavator/res/up.png | Bin 0 -> 304 bytes ProjectExcavator/src/CanvasExcavator.java | 16 ++ ProjectExcavator/src/DirectionType.java | 6 + ProjectExcavator/src/DrawingExcavator.java | 207 +++++++++++++++++++++ ProjectExcavator/src/DrawingRollers.java | 24 +++ ProjectExcavator/src/EntityExcavator.java | 33 ++++ ProjectExcavator/src/FormExcavator.java | 123 ++++++++++++ ProjectExcavator/src/Main.java | 5 - ProjectExcavator/src/Program.java | 8 + ProjectExcavator/src/RollersCount.java | 12 ++ 13 files changed, 429 insertions(+), 5 deletions(-) create mode 100644 ProjectExcavator/res/down.png create mode 100644 ProjectExcavator/res/left.png create mode 100644 ProjectExcavator/res/right.png create mode 100644 ProjectExcavator/res/up.png create mode 100644 ProjectExcavator/src/CanvasExcavator.java create mode 100644 ProjectExcavator/src/DirectionType.java create mode 100644 ProjectExcavator/src/DrawingExcavator.java create mode 100644 ProjectExcavator/src/DrawingRollers.java create mode 100644 ProjectExcavator/src/EntityExcavator.java create mode 100644 ProjectExcavator/src/FormExcavator.java delete mode 100644 ProjectExcavator/src/Main.java create mode 100644 ProjectExcavator/src/Program.java create mode 100644 ProjectExcavator/src/RollersCount.java diff --git a/ProjectExcavator/res/down.png b/ProjectExcavator/res/down.png new file mode 100644 index 0000000000000000000000000000000000000000..30e0015c4063766af2f6329d54e48713db85bba2 GIT binary patch literal 321 zcmeAS@N?(olHy`uVBq!ia0vp^${@_a1|;QfZjJ;}jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!E2r_jv*25Z>JgZF*-`Ls=F+{m~n)0OB45w zrc=5l=ML(Y^gZA>VyjZJ>Zz@rncrq|4@-?YBsY~1)H9ay0)Fug-0~0WIDgKud^UMy`&B14{sK1X0+!no zN*=JBdnh*VVfcfy?>K(eJ}HX-c`RIP-i$p0*RM?eW_YiRUCil^;<;GH>+wqAHFVdQ&MBb@0E2sh=l}o! literal 0 HcmV?d00001 diff --git a/ProjectExcavator/res/left.png b/ProjectExcavator/res/left.png new file mode 100644 index 0000000000000000000000000000000000000000..9f5b0d0af1df28a6f237891f975f04b0c5b25782 GIT binary patch literal 270 zcmeAS@N?(olHy`uVBq!ia0vp^${@_a1|;QfZjJ;}jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!Nr~~jv*25$r7xK6GVEN0u8t%BqSD^Xg0`q z$SV4@{dM>wcxZ{f08>(_0w4RM{|+^Br{o0$*pG-eN<0w|kTcjI*Wt)#!E@x8LSrM7 zcEb;50e|+SP=#kqNwXN7I3+}s798R7+G4(7MvwrV3o1EnQeH0uI>YUth6L+j5d#AT2Gf3poT@E*rU0GG;OXk; Jvd$@?2>=PePs9KK literal 0 HcmV?d00001 diff --git a/ProjectExcavator/res/right.png b/ProjectExcavator/res/right.png new file mode 100644 index 0000000000000000000000000000000000000000..e0568bb06e0be9acd0738e1be8dce566438941e3 GIT binary patch literal 266 zcmeAS@N?(olHy`uVBq!ia0vp^${@_a1|;QfZjJ;}jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!TFvpjv*25$r7xK6GVEN0u8t%BqSD^Y-O+) zuy&fk^Skkfa*L&bgKWon=8yFXeC?lF8!8+aPZ%oH2zESnU}EO&Y4~9v&@8i6PQ_qC zgK$S^V}+GKJ;NhWhaPbOuLD;;Ok@*j7BbcdWM?uAb2wqRKw+8Lfz0O%xFn;P^c2)I z&t4FcVE(w^gm3erISW^?$XzSFtO0e4B8P+o!zE{hoHzG$ZUddl;OXk;vd$@?2>|@M BQY8QY literal 0 HcmV?d00001 diff --git a/ProjectExcavator/res/up.png b/ProjectExcavator/res/up.png new file mode 100644 index 0000000000000000000000000000000000000000..5f5d5b656d9d04a0d1754a88d77d28e393f5edd4 GIT binary patch literal 304 zcmeAS@N?(olHy`uVBq!ia0vp^${@_a1|;QfZjJ;}jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!K0oojv*25Zzmn(WHJ@XMXdu}hheliXwYcgrWqYk`)hb}x|(dli@< zv~i-wSAX>_6RJ->Wv}F|4$JyBXY%TEUU5%6^-_PG5BNN{xF~ey>A6onZJl#+i{vZc z6(=XIKNeOb=yoYh$Y22WQ%mvv4FO#m{?ec=EA literal 0 HcmV?d00001 diff --git a/ProjectExcavator/src/CanvasExcavator.java b/ProjectExcavator/src/CanvasExcavator.java new file mode 100644 index 0000000..3ad543e --- /dev/null +++ b/ProjectExcavator/src/CanvasExcavator.java @@ -0,0 +1,16 @@ +import javax.swing.*; +import java.awt.*; + +public class CanvasExcavator extends JComponent{ + public DrawingExcavator _drawingExcavator; + public CanvasExcavator(){} + public void paintComponent(Graphics g){ + if(_drawingExcavator == null){ + return; + } + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + _drawingExcavator.DrawTransport(g2d); + super.repaint(); + } +} diff --git a/ProjectExcavator/src/DirectionType.java b/ProjectExcavator/src/DirectionType.java new file mode 100644 index 0000000..d3a2058 --- /dev/null +++ b/ProjectExcavator/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} \ No newline at end of file diff --git a/ProjectExcavator/src/DrawingExcavator.java b/ProjectExcavator/src/DrawingExcavator.java new file mode 100644 index 0000000..9278b07 --- /dev/null +++ b/ProjectExcavator/src/DrawingExcavator.java @@ -0,0 +1,207 @@ + +import java.awt.*; +import java.util.Random; + +public class DrawingExcavator { + private EntityExcavator EntityExcavator; // Класс-сущность + public DrawingRollers drawingRollers = null; + + private Integer _startPosX; // Левая координата отрисовки + private Integer _startPosY; // Верхняя координата отрисовки + private Integer _pictureWidth; // Ширина окна отрисовки + private Integer _pictureHeight; // Высота окна отрисовки + private final int _drawingExcWidth = 120; // Ширина отрисовки трактора + private final int _drawingExcHeight = 70; // Высота отрисовки трактора + + + + // Инициализация свойств + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){ + EntityExcavator = new EntityExcavator(); + EntityExcavator.Init(speed, weight, bodyColor, additionalColor, prop, ladle); + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + drawingRollers = new DrawingRollers(); + drawingRollers.setRollersCount((int)(Math.random() * 4)); + } + + // Смена границ формы отрисовки + public void SetPictureSize(int width, int height) + { + if (_drawingExcWidth > width || _drawingExcHeight > height) + { + return; + } + + if (_startPosX != null && _startPosY != null) { + if (_startPosX < 0) { _startPosX = 0; } + if (_startPosX + _drawingExcWidth > width) { _startPosX = width - _drawingExcWidth; } + if (_startPosY < 0) { _startPosY = 0; } + if (_startPosY + _drawingExcHeight > height) { _startPosY = height - _drawingExcHeight; } + } + _pictureWidth = width; + _pictureHeight = height; + } + + //Установка позиции Экскаватора + public void SetPosition(int x, int y){ + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + + if (x + _drawingExcWidth > _pictureWidth || x < 0) { + Random random = new Random(); + _startPosX = random.nextInt(_pictureWidth - _drawingExcWidth); + } + else { + _startPosX = x; + } + + if (y + _drawingExcHeight > _pictureHeight || y < 0) { + Random random = new Random(); + _startPosY = random.nextInt(_pictureHeight - _drawingExcHeight); + } + else { + _startPosY = y; + } + } + + // Изменение направления движения + public boolean MoveTransport(DirectionType direction){ + if (EntityExcavator == null || _startPosX == null || _startPosY == null){ + return false; + } + switch (direction) { + case Left: + if (_startPosX - EntityExcavator.Step > 0) { + _startPosX -= (int) EntityExcavator.Step; + } + return true; + case Up: + if (_startPosY - EntityExcavator.Step > 0) { + _startPosY -= (int) EntityExcavator.Step; + } + return true; + case Right: + if (_startPosX + EntityExcavator.Step < _pictureWidth - _drawingExcWidth) { + _startPosX += (int) EntityExcavator.Step; + } + return true; + case Down: + if (_startPosY + EntityExcavator.Step < _pictureHeight - _drawingExcHeight) { + _startPosY += (int) EntityExcavator.Step; + } + return true; + default: + return false; + } + } + + // Отрисовка Экскаватора + public void DrawTransport(Graphics2D g){ + if (EntityExcavator == null || _startPosX == null || _startPosY == null){ + return; + } + + // Границы экскаватора + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 20, _startPosY + 25, 60, 20); // главная нижняя + g.drawRect(_startPosX + 35, _startPosY + 10, 5, 15); + g.drawRect(_startPosX + 55, _startPosY + 3, 22, 22); // кабина + + g.setColor(EntityExcavator.getBodyColor()); + g.fillRect(_startPosX + 21, _startPosY + 26, 59, 19); + g.fillRect(_startPosX + 36, _startPosY + 11, 4, 14); + + g.setColor(Color.cyan); + g.fillRect(_startPosX + 56, _startPosY + 4, 21, 21); + + // Гусеница + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 24, _startPosY + 47, 48, 17); + + // Основные катки + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 15, _startPosY + 47, 17, 17); + g.drawOval(_startPosX + 63, _startPosY + 47, 17, 17); + + g.setColor(EntityExcavator.getBodyColor()); + g.fillOval(_startPosX + 15, _startPosY + 47, 17, 17); + g.fillOval(_startPosX + 63, _startPosY + 47, 17, 17); + + // Малые катки + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 40, _startPosY + 48, 5, 5); + g.drawOval(_startPosX + 50, _startPosY + 48, 5, 5); + + g.setColor(EntityExcavator.getBodyColor()); + g.fillOval(_startPosX + 40, _startPosY + 48, 5, 5); + g.fillOval(_startPosX + 50, _startPosY + 48, 5, 5); + + if (EntityExcavator.getProp()){ + g.setColor(Color.BLACK); + //Опоры + //справа + g.drawRect(_startPosX + 80, _startPosY + 40, 11, 3); + g.drawRect(_startPosX + 87, _startPosY + 43, 5, 25); + g.drawRect(_startPosX + 84, _startPosY + 68, 11, 3); + + //слева + g.drawRect(_startPosX + 6, _startPosY + 40, 13, 3); + g.drawRect(_startPosX + 4, _startPosY + 43, 5, 25); + g.drawRect(_startPosX + 1, _startPosY + 68, 11, 3); + + g.setColor(EntityExcavator.getAdditionalColor()); + //покраска справа + g.fillRect(_startPosX + 81, _startPosY + 41, 10, 2); + g.fillRect(_startPosX + 88, _startPosY + 44, 4, 24); + g.fillRect(_startPosX + 85, _startPosY + 69, 10, 2); + + //покраска слева + g.fillRect(_startPosX + 7, _startPosY + 41, 12, 2); + g.fillRect(_startPosX + 5, _startPosY + 44, 4, 24); + g.fillRect(_startPosX + 2, _startPosY + 69, 10, 2); + } + + if(EntityExcavator.getLadle()){ + g.setColor(Color.BLACK); + //Ковш + //ковш(стрела) + g.drawRect(_startPosX + 77, _startPosY + 17, 14, 8); + g.drawRect(_startPosX + 91, _startPosY + 9, 20, 7); + g.drawRect(_startPosX + 111, _startPosY + 17, 9, 20); + + int[] pointsLadleX = {_startPosX + 120, _startPosX + 104, _startPosX + 120}; + int[] pointsLadleY = {_startPosY + 37, _startPosY + 54, _startPosY + 54}; + + Polygon Ladle = new Polygon(pointsLadleX, pointsLadleY, 3); + g.drawPolygon(Ladle); + g.setColor(EntityExcavator.getAdditionalColor()); + g.fillPolygon(Ladle); + //покраска + g.fillRect(_startPosX + 78, _startPosY + 18, 13, 7); + g.fillRect(_startPosX + 92, _startPosY + 10, 19, 6); + g.fillRect(_startPosX + 112, _startPosY + 18, 8, 19); + } + int x = _startPosX; + if (drawingRollers.getRollersCount() != null){ + switch (drawingRollers.getRollersCount()){ + case OneRoller: + drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor()); + break; + case TwoRollers: + drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor()); + drawingRollers.DrawRollers(g,x + 44, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor()); + break; + case ThreeRollers: + drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor()); + drawingRollers.DrawRollers(g,x + 44, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor()); + drawingRollers.DrawRollers(g,x + 54, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor()); + break; + } + } + } +} diff --git a/ProjectExcavator/src/DrawingRollers.java b/ProjectExcavator/src/DrawingRollers.java new file mode 100644 index 0000000..e42b12f --- /dev/null +++ b/ProjectExcavator/src/DrawingRollers.java @@ -0,0 +1,24 @@ +import java.awt.*; + +public class DrawingRollers { + private RollersCount rollersCount; + public void setRollersCount(int numOfRoller){ + for (RollersCount numofenum : RollersCount.values()){ + if (numofenum.getNumOfRollers() == numOfRoller){ + rollersCount = numofenum; + return; + } + } + } + + public RollersCount getRollersCount(){ + return rollersCount; + } + public void DrawRollers(Graphics g, int x, int y, int width, int height, Color bodyColor){ + g.setColor(bodyColor); + g.fillOval(x, y, width, height); + g.setColor(Color.BLACK); + g.drawOval(x, y, width, height); + g.setColor(bodyColor); + } +} diff --git a/ProjectExcavator/src/EntityExcavator.java b/ProjectExcavator/src/EntityExcavator.java new file mode 100644 index 0000000..75b1aa1 --- /dev/null +++ b/ProjectExcavator/src/EntityExcavator.java @@ -0,0 +1,33 @@ +import java.awt.*; + +public class EntityExcavator { + private int Speed; + private double Weight; + private Color BodyColor; + + public Color getBodyColor() { + return BodyColor; + } + private Color AdditionalColor; + public Color getAdditionalColor(){ + return AdditionalColor; + } + private boolean Prop; + public boolean getProp(){ + return Prop; + } + private boolean Ladle; + public boolean getLadle(){ + return Ladle; + } + public double Step; + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){ + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Prop = prop; + Ladle = ladle; + Step = Speed * 100 / Weight; + } +} diff --git a/ProjectExcavator/src/FormExcavator.java b/ProjectExcavator/src/FormExcavator.java new file mode 100644 index 0000000..9834e27 --- /dev/null +++ b/ProjectExcavator/src/FormExcavator.java @@ -0,0 +1,123 @@ +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 FormExcavator extends JFrame { + private String title; + private Dimension dimension; + private int Width, Height; + private CanvasExcavator canvasExcavator = new CanvasExcavator(); + private JButton CreateButton = new JButton("Создать"); + private JButton UpButton = new JButton(); + private JButton DownButton = new JButton(); + private JButton LeftButton = new JButton(); + private JButton RightButton = new JButton(); + public FormExcavator(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("ProjectExcavator/res/up.png"); + UpButton.setIcon(iconUp); + UpButton.setName("UP"); + DownButton.setName("DOWN"); + Icon iconDown = new ImageIcon("ProjectExcavator/res/down.png"); + DownButton.setIcon(iconDown); + LeftButton.setName("LEFT"); + Icon iconLeft = new ImageIcon("ProjectExcavator/res/left.png"); + LeftButton.setIcon(iconLeft); + RightButton.setName("RIGHT"); + Icon iconRight = new ImageIcon("ProjectExcavator/res/right.png"); + 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 = (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(); + canvasExcavator._drawingExcavator = new DrawingExcavator(); + canvasExcavator._drawingExcavator.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank); + canvasExcavator._drawingExcavator.SetPictureSize(Width, Height); + canvasExcavator._drawingExcavator.SetPosition( StartPositionX, StartPositionY); + canvasExcavator.repaint(); + } + }); + + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + if (canvasExcavator._drawingExcavator == null) return; + boolean result = false; + switch ((((JButton)(event.getSource())).getName())) { + case "UP": + result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Up); + break; + case "DOWN": + result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Down); + break; + case "LEFT": + result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Left); + break; + case "RIGHT": + result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Right); + break; + } + if (result) { + canvasExcavator.repaint(); + } + } + }; + UpButton.addActionListener(actionListener); + DownButton.addActionListener(actionListener); + LeftButton.addActionListener(actionListener); + RightButton.addActionListener(actionListener); + + setSize(dimension.width,dimension.height); + setLayout(null); + canvasExcavator.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 100, 35); + UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35); + DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35); + RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35); + LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35); + add(CreateButton); + add(UpButton); + add(DownButton); + add(RightButton); + add(LeftButton); + add(canvasExcavator); + setVisible(true); + //обработка события изменения размеров окна + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + Width = getWidth() - 15; + Height = getHeight() - 35; + if (canvasExcavator._drawingExcavator != null) + canvasExcavator._drawingExcavator.SetPictureSize(Width, Height); + canvasExcavator.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 100, 35); + UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35); + DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35); + RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35); + LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35); + } + }); + } +} diff --git a/ProjectExcavator/src/Main.java b/ProjectExcavator/src/Main.java deleted file mode 100644 index 3e59c38..0000000 --- a/ProjectExcavator/src/Main.java +++ /dev/null @@ -1,5 +0,0 @@ -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file diff --git a/ProjectExcavator/src/Program.java b/ProjectExcavator/src/Program.java new file mode 100644 index 0000000..a6a309a --- /dev/null +++ b/ProjectExcavator/src/Program.java @@ -0,0 +1,8 @@ +import java.awt.*; + +public class Program { + public static void main(String[] args) { + FormExcavator form = new FormExcavator("Экскаватор", new Dimension(700, 500)); + form.Init(); + } +} \ No newline at end of file diff --git a/ProjectExcavator/src/RollersCount.java b/ProjectExcavator/src/RollersCount.java new file mode 100644 index 0000000..c8603d5 --- /dev/null +++ b/ProjectExcavator/src/RollersCount.java @@ -0,0 +1,12 @@ +public enum RollersCount { + OneRoller(1), + TwoRollers(2), + ThreeRollers(3); + private int numOfRollers; + RollersCount(int numOfRollers){ + this.numOfRollers = numOfRollers; + } + public int getNumOfRollers(){ + return numOfRollers; + } +}