From e7f49166914b28f2d537b2e962e32328d8847b3e Mon Sep 17 00:00:00 2001 From: sardq Date: Mon, 4 Dec 2023 20:27:51 +0400 Subject: [PATCH] =?UTF-8?q?1=20=D0=BB=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DirectionType.java | 16 +++++ DrawingCrane.java | 117 +++++++++++++++++++++++++++++++++++ DrawingWheel.java | 27 ++++++++ EntityCrane.java | 44 +++++++++++++ Frame.java | 150 +++++++++++++++++++++++++++++++++++++++++++++ Main.java | 6 ++ WheelCounter.java | 14 +++++ 7 files changed, 374 insertions(+) create mode 100644 DirectionType.java create mode 100644 DrawingCrane.java create mode 100644 DrawingWheel.java create mode 100644 EntityCrane.java create mode 100644 Frame.java create mode 100644 Main.java create mode 100644 WheelCounter.java diff --git a/DirectionType.java b/DirectionType.java new file mode 100644 index 0000000..9a183ad --- /dev/null +++ b/DirectionType.java @@ -0,0 +1,16 @@ +public enum DirectionType +{ + Up("U"), + Down("D"), + Left("L"), + Right("R"); + private String direct; + DirectionType(String d) + { + direct = d; + } + public String getDirect() + { + return direct; + } +} diff --git a/DrawingCrane.java b/DrawingCrane.java new file mode 100644 index 0000000..98dee47 --- /dev/null +++ b/DrawingCrane.java @@ -0,0 +1,117 @@ +import java.awt.*; + +public class DrawingCrane +{ + private EntityCrane entityCrane; + private DrawingWheel drawingWheel; + private int _pictureWidth; + + private int _pictureHeight; + + private int _startPositionX; + private int _startPositionY; + + + private final int _craneWidth = 200; + + private final int _craneHeight = 150; + public void Init(int speed, double weight, boolean counterWeight, boolean crane, Color bodyColor, Color additionalColor, int width, int height,int wheelCount) + { + if ((_craneWidth > width) || (_craneHeight > height)) return; + _pictureHeight = height; + _pictureWidth = width; + entityCrane = new EntityCrane(); + entityCrane.Init(speed, weight, counterWeight,crane, bodyColor, additionalColor); + drawingWheel = new DrawingWheel(); + drawingWheel.SetWheelCounter(wheelCount); + } + public void SetPosition(int x, int y) + { + + if(x+_craneWidth>_pictureWidth) + { + while(x + _craneWidth > _pictureWidth) + { + x -= _pictureWidth; + } + } + _startPositionX = x; + + if(y+_craneHeight>_pictureHeight) + { + while(y+_craneHeight>_pictureHeight) + { + y -= _pictureHeight; + } + } + _startPositionY = y; + } + public void MoveCrane(DirectionType direction) + { + if (entityCrane == null) return; + switch (direction) { + case Left -> { + + if (_startPositionX - entityCrane.Step > 0) { + _startPositionX -= (int) entityCrane.Step; + } + } + case Right->{ + + if (_startPositionX + entityCrane.Step + _craneWidth < _pictureWidth) { + _startPositionX += (int) entityCrane.Step; + } + } + case Up-> { + + if (_startPositionY - entityCrane.Step > 0) { + _startPositionY -= (int) entityCrane.Step; + } + } + + case Down ->{ + if (_startPositionY + entityCrane.Step + _craneHeight < _pictureHeight) { + + _startPositionY += (int) entityCrane.Step; + } + } + + } + + } + public void DrawTransport(Graphics g) { + var g2d = (Graphics2D)g; + if (entityCrane == null) return; + // Гусеницы + + g2d.drawLine( _startPositionX + 5, _startPositionY + 110, _startPositionX + 195, _startPositionY + 110); + g2d.drawLine( _startPositionX, _startPositionY + 115, _startPositionX, _startPositionY + 145); + g2d.drawLine( _startPositionX + 5, _startPositionY + 150, _startPositionX + 195, _startPositionY + 150); + g2d.drawLine( _startPositionX + 200, _startPositionY + 115, _startPositionX + 200, _startPositionY + 145); + g2d.drawArc( _startPositionX, _startPositionY + 110, _craneWidth / 20, _craneHeight / 15, 45, 135); + g2d.drawArc( _startPositionX, _startPositionY + 140, _craneWidth / 20, _craneHeight / 15, 135, 180); + g2d.drawArc( _startPositionX + 190, _startPositionY + 110, _craneWidth / 20, _craneHeight / 20, 305, 170); + g2d.drawArc( _startPositionX + 190, _startPositionY + 143, _craneWidth / 20, _craneHeight / 20, 230, 180); + // основное тело + g2d.setColor(entityCrane.getBodyColor()); + g2d.drawRect( _startPositionX + 10, _startPositionY + 65, 180, 40); + g2d.fillRect( _startPositionX + 10, _startPositionY + 65, 180, 40); + g2d.setColor(Color.BLACK); + + drawingWheel.DrawWheels(_startPositionX, _startPositionY, g2d); + //кабинка и выхлоп + g2d.drawRect( _startPositionX + 60, _startPositionY + 10, 20, 55); + g2d.drawRect( _startPositionX + 110, _startPositionY, 75, 65); + if (entityCrane.getCounterWeight()) { + g2d.setColor(entityCrane.getAdditionalColor()); + g2d.drawRect( _startPositionX + 185, _startPositionY + 20, 15, 45); + + } + if (entityCrane.getCrane()) { + g2d.drawRect( _startPositionX + 20, _startPositionY, 30, 65); + g2d.drawRect( _startPositionX, _startPositionY, 20, 30); + g2d.fillRect( _startPositionX + 20, _startPositionY, 30, 65); + g2d.fillRect( _startPositionX, _startPositionY, 20, 30); + } + } + } diff --git a/DrawingWheel.java b/DrawingWheel.java new file mode 100644 index 0000000..adae0c1 --- /dev/null +++ b/DrawingWheel.java @@ -0,0 +1,27 @@ +import java.awt.*; + +public class DrawingWheel { + private WheelCounter wheelCounter; + public void SetWheelCounter(int count) + { + if (count % 3 == 0) + wheelCounter = WheelCounter.SIX; + else if (count % 3 == 1) + wheelCounter = WheelCounter.FOUR; + else if (count % 3 == 2) + wheelCounter = WheelCounter.FIVE; + } + + public void DrawWheels(int _startPositionX, int _startPositionY, Graphics2D g2d) + { + g2d.drawOval( _startPositionX + 2, _startPositionY + 112, 36, 36); + g2d.drawOval( _startPositionX + 160, _startPositionY + 112, 36, 36); + g2d.drawOval( _startPositionX + 74, _startPositionY + 112, 10, 10); + g2d.drawOval( _startPositionX + 111, _startPositionY + 112, 10, 10); + switch (wheelCounter) + { + case FIVE -> {g2d.drawOval( _startPositionX + 133, _startPositionY + 128, 20, 20);} + case SIX -> {g2d.drawOval( _startPositionX + 45, _startPositionY + 128, 20, 20); g2d.drawOval( _startPositionX + 133, _startPositionY + 128, 20, 20);} + } + } +} diff --git a/EntityCrane.java b/EntityCrane.java new file mode 100644 index 0000000..c47e360 --- /dev/null +++ b/EntityCrane.java @@ -0,0 +1,44 @@ +import java.awt.*; + +public class EntityCrane +{ + //скорость + private int Speed ; + public int getSpeed() {return Speed;} + private void setSpeed(int value) {Speed = value;} + + + //вес + private double Weight ; + public double getWeight() {return Weight;} + private void setWeight(double value) {Weight = value;} + //основной цвет + private Color BodyColor ; + public Color getBodyColor() {return BodyColor;} + private void setBodyColor(Color value) {BodyColor = value;} + //дополнительный + private Color AdditionalColor ; + public Color getAdditionalColor() {return AdditionalColor;} + private void setAdditionalColor(Color value) {AdditionalColor = value;} + + private boolean CounterWeight ; + public boolean getCounterWeight() {return CounterWeight;} + private void setCounterWeight(boolean value) {CounterWeight = value;} + + private boolean Crane; + public boolean getCrane() {return Crane;} + private void setCrane(boolean value) {Crane = value;} + //шаг перемещения + public double Step; + + public void Init(int speed, double weight, boolean counterWeight,boolean crane, Color bodyColor, Color additionalColor) + { + Speed = speed; + Weight = weight; + CounterWeight = counterWeight; + Crane = crane; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Step = (double)Speed * 100 / Weight; + } +} diff --git a/Frame.java b/Frame.java new file mode 100644 index 0000000..77ab4ad --- /dev/null +++ b/Frame.java @@ -0,0 +1,150 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.*; +import java.io.IOException; +import java.util.Random; + +class DrawCrane extends JComponent { + private DrawingCrane _drawingCrane; + public void paintComponent(Graphics g) + { + super.paintComponent(g); + if (_drawingCrane == null) + return; + _drawingCrane.DrawTransport(g); + super.repaint(); + } + protected void CreateCraneButton_Click() + { + Random rnd = new Random(); + _drawingCrane = new DrawingCrane(); + Color addColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt( 256)); + Color bodyColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt(256)); + _drawingCrane.Init(rnd.nextInt(100)+100, rnd.nextInt(2000)+1000, + ConvertToBoolean(rnd.nextInt( 2)), ConvertToBoolean(rnd.nextInt( 2)), bodyColor, addColor, + Frame.Width-10, Frame.Height-38, rnd.nextInt( 3)+4); + _drawingCrane.SetPosition(rnd.nextInt( 100), rnd.nextInt(90)+10); + super.repaint(); + } + protected void ButtonMove_Click(String tag) + { + if (_drawingCrane == null) + return; + switch (tag) { + case "U" -> _drawingCrane.MoveCrane(DirectionType.Up); + case "D" -> _drawingCrane.MoveCrane(DirectionType.Down); + case "L" -> _drawingCrane.MoveCrane(DirectionType.Left); + case "R" -> _drawingCrane.MoveCrane(DirectionType.Right); + } + super.repaint(); + } + + static boolean ConvertToBoolean(int value) + { + if(value ==0) + return false; + return true; + } +} + + +public class Frame extends JFrame { + public Frame() + { + InitUI(); + } + protected static final int Width = 900; + protected static final int Height = 500; + + DrawCrane Crane; + private void InitUI() { + setSize(Width, Height); + setTitle("HoistingCrane"); + setLocationRelativeTo(null); + setLayout(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setResizable(false); + Crane = new DrawCrane(); + Crane.setBounds(0, 0, Width, Height); + add(Crane); + + MoveAL moving = new MoveAL(); + + JButton left = new JButton(); + left.setActionCommand("L"); + left.addActionListener(moving); + left.setLayout(null); + + JButton right = new JButton(); + right.setActionCommand("R"); + right.setLayout(null); + right.addActionListener(moving); + + JButton up = new JButton(); + up.setActionCommand("U"); + up.addActionListener(moving); + up.setLayout(null); + + JButton down = new JButton(); + down.setActionCommand("D"); + down.addActionListener(moving); + down.setLayout(null); + + JButton CreateButton = new JButton("Создать"); + right.setIcon(new ImageIcon("src/crane/right.png")); + left.setIcon(new ImageIcon("src/crane/left.png")); + down.setIcon(new ImageIcon("src/crane/down.png")); + up.setIcon(new ImageIcon("src/crane/up.png")); + JPanel panelMoveButtons = new JPanel(); + + panelMoveButtons.setLayout(new GridBagLayout()); + panelMoveButtons.setBounds(774, 395, 98, 66); + + GridBagConstraints constraints = new GridBagConstraints(); + constraints.fill = GridBagConstraints.BOTH; + constraints.gridx = 1; + constraints.gridy = 0; + constraints.weightx = 4; + constraints.weighty = 6; + panelMoveButtons.add(up, constraints); + constraints.gridy = 1; + constraints.gridx = 0; + panelMoveButtons.add(left, constraints); + constraints.gridx = 1; + panelMoveButtons.add(down, constraints); + constraints.gridx = 2; + panelMoveButtons.add(right, constraints); + + + JPanel panelCreateButton = new JPanel(); + panelCreateButton.setLayout(new BorderLayout()); + panelCreateButton.setBounds(0, 420, 112, 34); + panelCreateButton.add(CreateButton); + + add(panelMoveButtons); + add(panelCreateButton); + + + setVisible(true); + CreateButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Crane.CreateCraneButton_Click(); + } + }); + } + + public class MoveAL implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + Crane.ButtonMove_Click(e.getActionCommand()); + } + } +} + + + + diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..d155d60 --- /dev/null +++ b/Main.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args) { + Frame a = new Frame(); + a.setVisible(true); + } +} \ No newline at end of file diff --git a/WheelCounter.java b/WheelCounter.java new file mode 100644 index 0000000..093fc04 --- /dev/null +++ b/WheelCounter.java @@ -0,0 +1,14 @@ +public enum WheelCounter { + FOUR (4), + FIVE (5), + SIX (6); + private int count; + WheelCounter(int c) + { + count = c; + } + public int getCount() + { + return count; + } +} -- 2.25.1