diff --git a/res/down.png b/res/down.png new file mode 100644 index 0000000..30e0015 Binary files /dev/null and b/res/down.png differ diff --git a/res/left.png b/res/left.png new file mode 100644 index 0000000..9f5b0d0 Binary files /dev/null and b/res/left.png differ diff --git a/res/right.png b/res/right.png new file mode 100644 index 0000000..e0568bb Binary files /dev/null and b/res/right.png differ diff --git a/res/up.png b/res/up.png new file mode 100644 index 0000000..5f5d5b6 Binary files /dev/null and b/res/up.png differ diff --git a/src/CanvasExcavator.java b/src/CanvasExcavator.java new file mode 100644 index 0000000..3ad543e --- /dev/null +++ b/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/src/DirectionType.java b/src/DirectionType.java new file mode 100644 index 0000000..d3a2058 --- /dev/null +++ b/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} \ No newline at end of file diff --git a/src/DrawingExcavator.java b/src/DrawingExcavator.java new file mode 100644 index 0000000..9278b07 --- /dev/null +++ b/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/src/DrawingRollers.java b/src/DrawingRollers.java new file mode 100644 index 0000000..e42b12f --- /dev/null +++ b/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/src/EntityExcavator.java b/src/EntityExcavator.java new file mode 100644 index 0000000..7bca279 --- /dev/null +++ b/src/EntityExcavator.java @@ -0,0 +1,32 @@ +import java.awt.*; + +public class EntityExcavator { + private Integer 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/src/FormExcavator.java b/src/FormExcavator.java new file mode 100644 index 0000000..2605fe7 --- /dev/null +++ b/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("res/up.png"); + UpButton.setIcon(iconUp); + UpButton.setName("Up"); + DownButton.setName("Down"); + Icon iconDown = new ImageIcon("res/down.png"); + DownButton.setIcon(iconDown); + LeftButton.setName("Left"); + Icon iconLeft = new ImageIcon("res/left.png"); + LeftButton.setIcon(iconLeft); + RightButton.setName("Right"); + Icon iconRight = new ImageIcon("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/src/Main.java b/src/Main.java deleted file mode 100644 index 3e59c38..0000000 --- a/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/src/Program.java b/src/Program.java new file mode 100644 index 0000000..57e76c4 --- /dev/null +++ b/src/Program.java @@ -0,0 +1,9 @@ +import java.awt.*; + +public class Program { + public static void main(String[] args) { + FormExcavator form = new FormExcavator("Экскаватор", new Dimension(700, 500)); + form.Init(); + form.setLocationRelativeTo(null); + } +} \ No newline at end of file diff --git a/src/RollersCount.java b/src/RollersCount.java new file mode 100644 index 0000000..c8603d5 --- /dev/null +++ b/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; + } +}