diff --git a/.idea/PIbd-21_Anisin_R.S._DumpTruck._Hard.iml b/.idea/PIbd-21_Anisin_R.S._DumpTruck._Hard.iml index c90834f..d5b343a 100644 --- a/.idea/PIbd-21_Anisin_R.S._DumpTruck._Hard.iml +++ b/.idea/PIbd-21_Anisin_R.S._DumpTruck._Hard.iml @@ -3,6 +3,7 @@ + diff --git a/Resources/down.png b/Resources/down.png new file mode 100644 index 0000000..8efa2de Binary files /dev/null and b/Resources/down.png differ diff --git a/Resources/left.png b/Resources/left.png new file mode 100644 index 0000000..4aaee88 Binary files /dev/null and b/Resources/left.png differ diff --git a/Resources/right.png b/Resources/right.png new file mode 100644 index 0000000..20eccb6 Binary files /dev/null and b/Resources/right.png differ diff --git a/Resources/up.png b/Resources/up.png new file mode 100644 index 0000000..81a7903 Binary files /dev/null and b/Resources/up.png differ diff --git a/src/DumpTruck/DirectionType.java b/src/DumpTruck/DirectionType.java new file mode 100644 index 0000000..0d52a30 --- /dev/null +++ b/src/DumpTruck/DirectionType.java @@ -0,0 +1,11 @@ +package DumpTruck; + +public enum DirectionType { + Up, + + Down, + + Left, + + Right +} \ No newline at end of file diff --git a/src/DumpTruck/DrawingDumpTruck.java b/src/DumpTruck/DrawingDumpTruck.java new file mode 100644 index 0000000..ec14ba9 --- /dev/null +++ b/src/DumpTruck/DrawingDumpTruck.java @@ -0,0 +1,110 @@ +package DumpTruck; + +import java.awt.*; + +public class DrawingDumpTruck { + + private EntityDumpTruck entityDumpTruck; + + public EntityDumpTruck getEntityDumpTruck() { + return entityDumpTruck; + } + + private DrawingWheels drawingWheels; + + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private int _truckWidth = 160; + private int _truckHeight = 90; + + public boolean Init(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber) + { + if (height < _truckHeight || width < _truckWidth) { + return false; + } + _pictureWidth = width; + _pictureHeight = height; + entityDumpTruck = new EntityDumpTruck(); + entityDumpTruck.Init(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor); + drawingWheels = new DrawingWheels(); + drawingWheels.setWheelNumber(wheelNumber); + return true; + } + + public void SetPosition(int x, int y) + { + if (x < 0 || x + _truckWidth > _pictureWidth) { x = 0; } + if (y < 0 || y + _truckHeight > _pictureHeight) { y = 0; } + _startPosX = x; + _startPosY = y; + } + + public void MoveTransport(DirectionType direction) { + if (entityDumpTruck == null) return; + + switch (direction) { + //влево + case Left: + if (_startPosX - entityDumpTruck.getStep() > 0) + { + _startPosX -= (int)entityDumpTruck.getStep(); + } + break; + //вверх + case Up: + if (_startPosY - entityDumpTruck.getStep() > 0) + { + _startPosY -= (int)entityDumpTruck.getStep(); + } + break; + //вправо + case Right: + if (_startPosX + _truckWidth + entityDumpTruck.getStep() < _pictureWidth) + { + _startPosX += (int)entityDumpTruck.getStep(); + } + break; + //вниз + case Down: + if (_startPosY + _truckHeight + entityDumpTruck.getStep() < _pictureHeight) + { + _startPosY += (int)entityDumpTruck.getStep(); + } + break; + } + } + + public void DrawTransport(Graphics2D g2D) + { + if (entityDumpTruck == null) { + return; + } + g2D.setStroke(new BasicStroke(3)); + g2D.setPaint(entityDumpTruck.getBodyColor()); + g2D.fillRect(_startPosX, _startPosY + 40, 160, 10); + g2D.fillRect(_startPosX + 120, _startPosY, 40, 40); + drawingWheels.drawWheels(g2D, Color.BLACK, _startPosX, _startPosY); + if (entityDumpTruck.getDumpBox()) + { + g2D.setPaint(entityDumpTruck.getDumpBoxColor()); + 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 (entityDumpTruck.getDumpBox() && entityDumpTruck.getTent()) + { + g2D.setPaint(entityDumpTruck.getTentColor()); + 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/DumpTruck/DrawingWheels.java b/src/DumpTruck/DrawingWheels.java new file mode 100644 index 0000000..fe1d640 --- /dev/null +++ b/src/DumpTruck/DrawingWheels.java @@ -0,0 +1,55 @@ +package DumpTruck; + +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); + } +} \ No newline at end of file diff --git a/src/DumpTruck/EntityDumpTruck.java b/src/DumpTruck/EntityDumpTruck.java new file mode 100644 index 0000000..cefd08a --- /dev/null +++ b/src/DumpTruck/EntityDumpTruck.java @@ -0,0 +1,61 @@ +package DumpTruck; + +import java.awt.*; +public class EntityDumpTruck { + + 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 boolean Tent; + + public boolean getTent() { + return Tent; + } + + private boolean DumpBox; + + public boolean getDumpBox() { + return DumpBox; + } + + private Color DumpBoxColor; + + public Color getDumpBoxColor() { + return DumpBoxColor; + } + + private Color TentColor; + + public Color getTentColor() { + return TentColor; + } + + public double getStep() { + return (double)Speed * 100 / Weight; + } + + public void Init(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Tent = tent; + DumpBox = dumpBox; + DumpBoxColor = dumpBoxColor; + TentColor = tentColor; + } +} diff --git a/src/DumpTruck/FrameDumpTruck.java b/src/DumpTruck/FrameDumpTruck.java new file mode 100644 index 0000000..e7f69d6 --- /dev/null +++ b/src/DumpTruck/FrameDumpTruck.java @@ -0,0 +1,17 @@ +package DumpTruck; + +import javax.swing.*; + +public class FrameDumpTruck extends JFrame { + + private PictureBoxDumpTruck pictureBoxDumpTruck; + + public FrameDumpTruck() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + pictureBoxDumpTruck = new PictureBoxDumpTruck(); + add(pictureBoxDumpTruck); + pack(); + setLocationRelativeTo(null); + setVisible(true); + } +} \ No newline at end of file diff --git a/src/Main.java b/src/DumpTruck/Main.java similarity index 59% rename from src/Main.java rename to src/DumpTruck/Main.java index 6054a64..be46a71 100644 --- a/src/Main.java +++ b/src/DumpTruck/Main.java @@ -1,5 +1,7 @@ +package DumpTruck; + public class Main { public static void main(String[] args) { - + new FrameDumpTruck(); } -} +} \ No newline at end of file diff --git a/src/DumpTruck/PictureBoxDumpTruck.java b/src/DumpTruck/PictureBoxDumpTruck.java new file mode 100644 index 0000000..d0d100b --- /dev/null +++ b/src/DumpTruck/PictureBoxDumpTruck.java @@ -0,0 +1,122 @@ +package DumpTruck; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionListener; +import java.util.Random; + +public class PictureBoxDumpTruck extends JPanel { + private DrawingDumpTruck drawingDumpTruck; + + private JButton buttonLeft; + + private JButton buttonUp; + + private JButton buttonRight; + + private JButton buttonDown; + + private JButton buttonCreateDumpTruck; + + public PictureBoxDumpTruck() { + setLayout(null); + setBounds(0, 0, 800, 450); + buttonCreateDumpTruck = new JButton("Создать"); + buttonCreateDumpTruck.setFocusable(false); + buttonCreateDumpTruck.setBounds(12, 415, 100, 30); + add(buttonCreateDumpTruck); + + buttonCreateDumpTruck.addActionListener(e -> { + Random random = new Random(); + drawingDumpTruck = new DrawingDumpTruck(); + drawingDumpTruck.Init(random.nextInt(200, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), + random.nextBoolean(), random.nextBoolean(), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), + this.getWidth(), this.getHeight(), random.nextInt(2, 5)); + drawingDumpTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + repaint(); + }); + + ActionListener buttonMoveListener = e -> { + if (drawingDumpTruck == null) + { + return; + } + + String buttonName = ((JButton) e.getSource()).getName(); + + switch (buttonName) { + case ("buttonUp"): + drawingDumpTruck.MoveTransport(DirectionType.Up); + break; + case ("buttonDown"): + drawingDumpTruck.MoveTransport(DirectionType.Down); + break; + case ("buttonLeft"): + drawingDumpTruck.MoveTransport(DirectionType.Left); + break; + case ("buttonRight"): + drawingDumpTruck.MoveTransport(DirectionType.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 (drawingDumpTruck == null) { + return; + } + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + drawingDumpTruck.DrawTransport(g2d); + } +} diff --git a/src/DumpTruck/WheelNumber.java b/src/DumpTruck/WheelNumber.java new file mode 100644 index 0000000..c3363a1 --- /dev/null +++ b/src/DumpTruck/WheelNumber.java @@ -0,0 +1,9 @@ +package DumpTruck; + +public enum WheelNumber { + Two, + + Three, + + Four +} \ No newline at end of file