diff --git a/ProjectBulldozer/DirectionBulldozer.java b/ProjectBulldozer/DirectionBulldozer.java new file mode 100644 index 0000000..7798f66 --- /dev/null +++ b/ProjectBulldozer/DirectionBulldozer.java @@ -0,0 +1,7 @@ +public enum DirectionBulldozer { + Up, + Down, + Left, + Right + +} diff --git a/ProjectBulldozer/DrawningBulldozer.java b/ProjectBulldozer/DrawningBulldozer.java new file mode 100644 index 0000000..f297a26 --- /dev/null +++ b/ProjectBulldozer/DrawningBulldozer.java @@ -0,0 +1,110 @@ +import java.awt.*; +public class DrawningBulldozer { + private EntityBulldozer entityBulldozer; + public EntityBulldozer getEntityBulldozer() { + return entityBulldozer; + } + private DrawningWheels drawningWheels; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private int _bulldozerWidth = 160; + private int _bulldozerHeight = 80; + public boolean Init(int speed, double weight, Color bulldozerColor, Color cabinColor, Color covshColor, boolean hasMoldboardfront, boolean hasRipper, int width, int height, int wheelNumber) + { + if (height < _bulldozerHeight || width < _bulldozerWidth) { + return false; + } + _pictureWidth = width; + _pictureHeight = height; + entityBulldozer = new EntityBulldozer(); + entityBulldozer.Init(speed, weight, bulldozerColor, cabinColor, covshColor, hasMoldboardfront, hasRipper); + drawningWheels = new DrawningWheels(); + drawningWheels.setWheelNumber(wheelNumber); + return true; + } + public void SetPosition(int x, int y) + { + if (x < 0 || x + _bulldozerWidth > _pictureWidth) { x = 0; } + if (y < 0 || y + _bulldozerHeight > _pictureHeight) { y = 0; } + _startPosX = x; + _startPosY = y; + } + public void MoveTransport(DirectionBulldozer direction) { + if (entityBulldozer == null) return; + switch (direction) { + //влево + case Left: + if (_startPosX - entityBulldozer.getStep() > 0) + { + _startPosX -= (int)entityBulldozer.getStep(); + } + break; + //вверх + case Up: + if (_startPosY - entityBulldozer.getStep() > 0) + { + _startPosY -= (int)entityBulldozer.getStep(); + } + break; + //вправо + case Right: + if (_startPosX + _bulldozerWidth + entityBulldozer.getStep() < _pictureWidth) + { + _startPosX += (int)entityBulldozer.getStep(); + } + break; + //вниз + case Down: + if (_startPosY + _bulldozerHeight + entityBulldozer.getStep() < _pictureHeight) + { + _startPosY += (int)entityBulldozer.getStep(); + } + break; + } + } + public void DrawTransport(Graphics2D g2D) + { + if (entityBulldozer == null) { + return; + } + g2D.setStroke(new BasicStroke(3)); + g2D.setPaint(entityBulldozer.getBulldozerColor()); + g2D.fillRect(_startPosX + 25, _startPosY + 20, 110, 30); + g2D.fillRect(_startPosX+60, _startPosY, 10, 30); + int x = _startPosX + 30; // начальная позиция X + int y = _startPosY; // начальная позиция Y + int width = 110; // ширина прямоугольника + int height = 30; // высота прямоугольника + int radius = 20; // радиус закругления углов + // Рисуем закругленный прямоугольник + g2D.setColor(Color.BLACK); + g2D.drawArc(x - 5, y + 50, radius * 2, radius * 2, 180, 90); // верхний левый угол + g2D.drawLine(x + radius - 5, y + 50, x + width - radius - 5, y + 50); // верхняя горизонталь + g2D.drawArc(x + width - radius * 2 - 5, y + 50, radius * 2, radius * 2, 270, 90); // верхний правый угол + g2D.drawArc(x + width - radius * 2 - 5, y + height - radius * 2 + 60, radius * 2, radius * 2, 0, 90); // нижний правый угол + g2D.drawLine(x + width - radius - 5, y + height + 60, x + radius - 5, y + height + 60); // нижняя горизонталь + g2D.drawArc(x - 5, y + height - radius * 2 + 60, radius * 2, radius * 2, 90, 90); // нижний левый угол + // Кабина + g2D.setPaint(entityBulldozer.getCabinColor()); + g2D.fillRect(_startPosX + 105, _startPosY, 30, 20); + //Колёса + drawningWheels.drawWheels(g2D, Color.BLACK, _startPosX, _startPosY); + if (entityBulldozer.getHasMoldboardfront()) + { + g2D.setPaint(entityBulldozer.getCabinColor()); + int[] xPoints = {_startPosX + 25, _startPosX + 25, _startPosX}; + int[] yPoints = {_startPosY + 30, _startPosY + 80, _startPosY + 80}; + Polygon triangle = new Polygon(xPoints, yPoints, 3); + g2D.drawPolygon(triangle); + } + if (entityBulldozer.getHasRipper()) + { + int[] xPoints2 = {_startPosX + 130, _startPosX + 160, _startPosX + 160}; + int[] yPoints2 = {_startPosY + 50, _startPosY + 50, _startPosY + 80}; + Polygon triangle2 = new Polygon(xPoints2, yPoints2, 3); + g2D.drawPolygon(triangle2); + } + } +} diff --git a/ProjectBulldozer/DrawningWheels.java b/ProjectBulldozer/DrawningWheels.java new file mode 100644 index 0000000..e8fc5b9 --- /dev/null +++ b/ProjectBulldozer/DrawningWheels.java @@ -0,0 +1,73 @@ +import java.awt.*; +public class DrawningWheels { + private WheelNumber wheelNumber; + public void setWheelNumber(int number) { + switch (number) { + case 1: + wheelNumber = WheelNumber.Four; + break; + case 2: + wheelNumber = WheelNumber.Five; + break; + case 3: + wheelNumber = WheelNumber.Six; + break; + default: wheelNumber = WheelNumber.Four; + } + } + public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + switch (wheelNumber) { + case Four: + drawFourWheels(g2D, color, _startPosX, _startPosY); + break; + case Five: + drawFiveWheels(g2D, color, _startPosX, _startPosY); + break; + case Six: + drawSixWheels(g2D, color, _startPosX, _startPosY); + break; + } + } + private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + int wheelRadius = 10; + g2D.drawOval(_startPosX + 30, _startPosY + 60, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 30, _startPosY + 60, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 56, _startPosY + 60, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 56, _startPosY + 60, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 84, _startPosY + 60, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 84, _startPosY + 60, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 110, _startPosY + 60, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 110, _startPosY + 60, wheelRadius * 2, wheelRadius * 2); + } + private void drawFiveWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + int wheelRadius = 10; + g2D.drawOval(_startPosX + 34, _startPosY + 55, wheelRadius, wheelRadius); + g2D.fillOval(_startPosX + 34, _startPosY + 55, wheelRadius, wheelRadius); + g2D.drawOval(_startPosX + 45, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 45, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 69, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 69, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 94, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 94, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 115, _startPosY + 55, wheelRadius, wheelRadius); + g2D.fillOval(_startPosX + 115, _startPosY + 55, wheelRadius, wheelRadius); + } + private void drawSixWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + int wheelRadius = 10; + g2D.drawOval(_startPosX + 34, _startPosY + 54, wheelRadius, wheelRadius); + g2D.fillOval(_startPosX + 34, _startPosY + 54, wheelRadius, wheelRadius); + g2D.drawOval(_startPosX + 35, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 35, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 58, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 58, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 82, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 82, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 105, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.fillOval(_startPosX + 105, _startPosY + 65, wheelRadius * 2, wheelRadius * 2); + g2D.drawOval(_startPosX + 115, _startPosY + 54, wheelRadius, wheelRadius); + g2D.fillOval(_startPosX + 115, _startPosY + 54, wheelRadius, wheelRadius); + } +} \ No newline at end of file diff --git a/ProjectBulldozer/EntityBulldozer.java b/ProjectBulldozer/EntityBulldozer.java new file mode 100644 index 0000000..9ad18d3 --- /dev/null +++ b/ProjectBulldozer/EntityBulldozer.java @@ -0,0 +1,41 @@ +import java.awt.*; +public class EntityBulldozer { + private int Speed; + public int getSpeed() { + return Speed; + } + private double Weight; + public double getWeight() { + return Weight; + } + private Color BulldozerColor; + public Color getBulldozerColor() { + return BulldozerColor; + } + private Color CabinColor; + public Color getCabinColor() { + return CabinColor; + } + private Color CovshColor; + public Color getCovshColor() {return CovshColor;} + private boolean HasMoldboardfront; + public boolean getHasMoldboardfront() { + return HasMoldboardfront; + } + private boolean HasRipper; + public boolean getHasRipper() { + return HasRipper; + } + public double getStep() { + return (double)Speed * 800 / Weight; + } + public void Init(int speed, double weight, Color bulldozerColor, Color cabinColor, Color covshColor, boolean hasMoldboardfront, boolean hasRipper) { + Speed = speed; + Weight = weight; + BulldozerColor = bulldozerColor; + CabinColor = cabinColor; + CovshColor = covshColor; + HasMoldboardfront = hasMoldboardfront; + HasRipper = hasRipper; + } +} diff --git a/ProjectBulldozer/FrameBulldozer.java b/ProjectBulldozer/FrameBulldozer.java new file mode 100644 index 0000000..60b8d70 --- /dev/null +++ b/ProjectBulldozer/FrameBulldozer.java @@ -0,0 +1,12 @@ +import javax.swing.*; +public class FrameBulldozer extends JFrame { + private PictureBoxBulldozer pictureBoxBulldozer; + public FrameBulldozer() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + pictureBoxBulldozer = new PictureBoxBulldozer(); + add(pictureBoxBulldozer); + pack(); + setLocationRelativeTo(null); + setVisible(true); + } +} diff --git a/ProjectBulldozer/Main.java b/ProjectBulldozer/Main.java index 6054a64..3a86f38 100644 --- a/ProjectBulldozer/Main.java +++ b/ProjectBulldozer/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - + FrameBulldozer frameBulldozer = new FrameBulldozer(); } -} +} \ No newline at end of file diff --git a/ProjectBulldozer/PictureBoxBulldozer.java b/ProjectBulldozer/PictureBoxBulldozer.java new file mode 100644 index 0000000..15b932e --- /dev/null +++ b/ProjectBulldozer/PictureBoxBulldozer.java @@ -0,0 +1,98 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionListener; +import java.util.Random; +public class PictureBoxBulldozer extends JPanel { + private DrawningBulldozer drawningBulldozer; + private JButton buttonLeft; + private JButton buttonUp; + private JButton buttonRight; + private JButton buttonDown; + private JButton buttonCreateBulldozer; + public PictureBoxBulldozer() { + setLayout(null); + setBounds(0, 0, 800, 450); + buttonCreateBulldozer = new JButton("Создать"); + buttonCreateBulldozer.setFocusable(false); + buttonCreateBulldozer.setBounds(12, 415, 100, 30); + add(buttonCreateBulldozer); + buttonCreateBulldozer.addActionListener(e -> { + Random random = new Random(); + drawningBulldozer = new DrawningBulldozer(); + drawningBulldozer.Init(random.nextInt(100)+100, + random.nextInt(2000)+1000, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + 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(), + this.getWidth(), this.getHeight(), random.nextInt(3) + 1); + drawningBulldozer.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + repaint(); + }); + ActionListener buttonMoveListener = e -> { + if (drawningBulldozer == null) + { + return; + } + String buttonName = ((JButton) e.getSource()).getName(); + + switch (buttonName) { + case ("buttonUp"): + drawningBulldozer.MoveTransport(DirectionBulldozer.Up); + break; + case ("buttonDown"): + drawningBulldozer.MoveTransport(DirectionBulldozer.Down); + break; + case ("buttonLeft"): + drawningBulldozer.MoveTransport(DirectionBulldozer.Left); + break; + case ("buttonRight"): + drawningBulldozer.MoveTransport(DirectionBulldozer.Right); + break; + } + repaint(); + }; + buttonLeft = new JButton(); + buttonLeft.setName("buttonLeft"); + buttonLeft.setFocusable(false); + buttonLeft.setPreferredSize(new Dimension(30, 30)); + buttonLeft.setIcon(new ImageIcon("Resources/left.png")); + buttonLeft.addActionListener(buttonMoveListener); + buttonLeft.setBounds(686, 408, 30, 30); + add(buttonLeft); + buttonRight = new JButton(); + buttonRight.setName("buttonRight"); + buttonRight.setFocusable(false); + buttonRight.setPreferredSize(new Dimension(30, 30)); + buttonRight.setIcon(new ImageIcon("Resources/right.png")); + buttonRight.addActionListener(buttonMoveListener); + buttonRight.setBounds(758, 408, 30, 30); + add(buttonRight); + buttonDown = new JButton(); + buttonDown.setName("buttonDown"); + buttonDown.setFocusable(false); + buttonDown.setPreferredSize(new Dimension(30, 30)); + buttonDown.setIcon(new ImageIcon("Resources/down.png")); + buttonDown.addActionListener(buttonMoveListener); + buttonDown.setBounds(722, 408, 30, 30); + add(buttonDown); + buttonUp = new JButton(); + buttonUp.setName("buttonUp"); + buttonUp.setFocusable(false); + buttonUp.setPreferredSize(new Dimension(30, 30)); + buttonUp.setIcon(new ImageIcon("Resources/up.png")); + buttonUp.addActionListener(buttonMoveListener); + buttonUp.setBounds(722, 372, 30, 30); + add(buttonUp); + setPreferredSize(new Dimension(800, 450)); + } + @Override + protected void paintComponent(Graphics g) { + if (drawningBulldozer == null) { + return; + } + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + drawningBulldozer.DrawTransport(g2d); + } +} \ No newline at end of file diff --git a/ProjectBulldozer/ProjectBulldozer.iml b/ProjectBulldozer/ProjectBulldozer.iml index 9886d61..b107a2d 100644 --- a/ProjectBulldozer/ProjectBulldozer.iml +++ b/ProjectBulldozer/ProjectBulldozer.iml @@ -1,5 +1,5 @@ - + diff --git a/ProjectBulldozer/Resources/down.png b/ProjectBulldozer/Resources/down.png new file mode 100644 index 0000000..8efa2de Binary files /dev/null and b/ProjectBulldozer/Resources/down.png differ diff --git a/ProjectBulldozer/Resources/left.png b/ProjectBulldozer/Resources/left.png new file mode 100644 index 0000000..4aaee88 Binary files /dev/null and b/ProjectBulldozer/Resources/left.png differ diff --git a/ProjectBulldozer/Resources/right.png b/ProjectBulldozer/Resources/right.png new file mode 100644 index 0000000..20eccb6 Binary files /dev/null and b/ProjectBulldozer/Resources/right.png differ diff --git a/ProjectBulldozer/Resources/up.png b/ProjectBulldozer/Resources/up.png new file mode 100644 index 0000000..81a7903 Binary files /dev/null and b/ProjectBulldozer/Resources/up.png differ diff --git a/ProjectBulldozer/WheelNumber.java b/ProjectBulldozer/WheelNumber.java new file mode 100644 index 0000000..aec5819 --- /dev/null +++ b/ProjectBulldozer/WheelNumber.java @@ -0,0 +1,5 @@ +public enum WheelNumber { + Four, + Five, + Six +} \ No newline at end of file