From c8a89e6c8652fbaf613fbb8773291a202b317995 Mon Sep 17 00:00:00 2001 From: sardq Date: Mon, 4 Dec 2023 20:41:05 +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 --- src/Crane/Down.png | Bin 0 -> 810 bytes src/Crane/Left.png | Bin 0 -> 802 bytes src/Crane/Right.png | Bin 0 -> 817 bytes src/Crane/Up.png | Bin 0 -> 821 bytes src/DirectionType.java | 16 +++++ src/DrawingCrane.java | 115 +++++++++++++++++++++++++++++++++ src/DrawingWheel.java | 27 ++++++++ src/EntityCrane.java | 44 +++++++++++++ src/Frame.java | 142 +++++++++++++++++++++++++++++++++++++++++ src/Main.java | 6 ++ src/WheelCounter.java | 14 ++++ 11 files changed, 364 insertions(+) create mode 100644 src/Crane/Down.png create mode 100644 src/Crane/Left.png create mode 100644 src/Crane/Right.png create mode 100644 src/Crane/Up.png create mode 100644 src/DirectionType.java create mode 100644 src/DrawingCrane.java create mode 100644 src/DrawingWheel.java create mode 100644 src/EntityCrane.java create mode 100644 src/Frame.java create mode 100644 src/Main.java create mode 100644 src/WheelCounter.java diff --git a/src/Crane/Down.png b/src/Crane/Down.png new file mode 100644 index 0000000000000000000000000000000000000000..19764482ecbb0f931603c9ab76a22cb82b77b7a4 GIT binary patch literal 810 zcmeAS@N?(olHy`uVBq!ia0vp^;y^6N!2%?6b$lNKDTQQ@AYTTCDm4a%h86~fUqGRT z7Yq!g1`G_Z5*Qe)W&rgR%-I!a1C(G&@^*J&_}|`tWO>_%)r2R7=#&*=dVZs z3bL1Y`ns||=j0Oo5<($lBa+Mk$II@wfv z;)DfF{2>Ad-8!10I+lfqut+&RSN zv71M4&c1u{-}0T$=k*`1f5+^k6{uy!?Ipkx_^9U8w9SbO)^pYB^Sz-(IsJr0*Lb2rh$dvaR6#qT(|K3sO> z#r~T<)1#iP;OZAlkow`h`F0N%M?woa_);g+e_1Jd~lYO1%J zH~u~rRUG##oa;7ge}${A^4tykr@LR`IC}9&>hqmnZ_iaO571Zi`m(9ozOHqzguQ$2b#)Hc5gc5_q+LF)kcGv3ddG; z#xpC_9{MF&^jEu7s;*$+b$`nvB2D$qrnlEI|G$=OdhlUgd^Y>xp5N~d^WLcq@t-EC z>||-glUp38cPn(Dj&G8JIyVv1^M<1?pk&boFyt=akR{0M%SS$p8QV literal 0 HcmV?d00001 diff --git a/src/Crane/Left.png b/src/Crane/Left.png new file mode 100644 index 0000000000000000000000000000000000000000..ec4438956b678e9be84edc637e49186c94ca1721 GIT binary patch literal 802 zcmeAS@N?(olHy`uVBq!ia0vp^azHH3!2%?=nVCNXQVPi)LB0$ORcZ_j4J`}|zkosw zFBlj~4Hy_+B``2p%>b$|n6oR;1}MRl)^mS!_&dDbz$kxkpdlv%(<3~>y#}JFtbEnv51P6*V?5}=*+WUU>J{8|f5eCG-`M|DRQn<#eP~dIQNB$PREnE%@nd7<}6i?KyjN43Qh z<8!fhU6S;>?@e>O$fT;U`Fho0!GQ}VClY6OyybuGcJ#+*$CaBkPcf|*a$P4-zozNyl{E^E z=l)kMEnPeL&*_(oR%NQrU9ec?O<8=%<~4bR^8J>k##>94$3|^DdQa{AF)7Ba2Nndr zu#4Hi%+U3QX;bx^<*TMoS*NZ(>50J&o`;V%F7&U?E^N`S=vg|qEALy`@5TVRHHFVN zoGiTbBYjP4fxu_w$CAs~4!?f*^W>Y&@fwaH$9Icl7O?L)C-5_;!gJpH0`9;MpB<)o zKAyjA)vxqLZ``=fS$`5~JoE2|_WP!|$ItVp-u=t>z)98{m>yJ1Tq8XSySBJgpdJQKS3j3^P6b$|n6oR;1}MRl)^mS!_&dDbz$kxkpdlyg>gQtsQh{fr-Q+zXo0|k!P=f`iFo#J~bWpaS(+O?Wo zN?r0HJ`3gcXt)aSFS5O+FCw_->Sp%t1sq+^zdZ|FqVOot=EYL>kBhVRda$lu>vK~Ro__q@-0AW$F0S#FVot(*3SQEuKDf$kl>V~* z$h!O;mcgr=grdrxMwWBm<%)7=?zhXfUNNg*=H8i;Tf^%nS#H>7z57~3b-S8S<-177 zde)^|+!VR*x;bRNwXRFvzW=D3RbKt9&jGD}^xr;w_%LbZ9x)r6nwXeZ4U=aA9T2tf zGk5im-xU+*$EY59{!}nE3I=o>zjCBnxbq!5I49%?!&8$pKwG9lc3=H~Mb{s^}kei>9nO2Eg v!~aNzTA&6ExD6$lxv9k^iMa*ndd#g1%pjK7wZ)|Z^)Pt4`njxgN@xNAQKd(l literal 0 HcmV?d00001 diff --git a/src/Crane/Up.png b/src/Crane/Up.png new file mode 100644 index 0000000000000000000000000000000000000000..b93baf828410ea86d898489dc3c6e5159b0e3a77 GIT binary patch literal 821 zcmeAS@N?(olHy`uVBq!ia0vp^;y^6N!2%?6b$lNKDTQQ@AYTTCDm4a%h86~fUqGRT z7Yq!g1`G_Z5*Qe)W&rgR%-I!a1C(G&@^*J&_}|`tWO>_%)r2R7=#&*=dVZs z3bL1Y`ns||=j0OF$bD8#KMhzbVWRzGz3Q zbJwFKTmOhFcYNKr!@5=cX{wvX^SSDKB^>V@7v3o})xgf9*ZA1(rAy|o_WSfKH8y=$ zw|;@B$NcNkCl?)FK#IZ0z{ptFz*5)HB*f6%%FxWp)KuHRz{mdKI;Vst01+-n A+5i9m literal 0 HcmV?d00001 diff --git a/src/DirectionType.java b/src/DirectionType.java new file mode 100644 index 0000000..9a183ad --- /dev/null +++ b/src/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/src/DrawingCrane.java b/src/DrawingCrane.java new file mode 100644 index 0000000..48df5c8 --- /dev/null +++ b/src/DrawingCrane.java @@ -0,0 +1,115 @@ +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); + } + } +} \ No newline at end of file diff --git a/src/DrawingWheel.java b/src/DrawingWheel.java new file mode 100644 index 0000000..adae0c1 --- /dev/null +++ b/src/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/src/EntityCrane.java b/src/EntityCrane.java new file mode 100644 index 0000000..c47e360 --- /dev/null +++ b/src/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/src/Frame.java b/src/Frame.java new file mode 100644 index 0000000..b76a4ff --- /dev/null +++ b/src/Frame.java @@ -0,0 +1,142 @@ +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/src/Main.java b/src/Main.java new file mode 100644 index 0000000..d155d60 --- /dev/null +++ b/src/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/src/WheelCounter.java b/src/WheelCounter.java new file mode 100644 index 0000000..093fc04 --- /dev/null +++ b/src/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