From c6fc3c0b376bed90e6d3fa5b8acfffcefdc37636 Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Tue, 26 Sep 2023 19:51:49 +0400 Subject: [PATCH] Laba1 KozyrevSS GasolineTanker JAVA HARD --- Arrows/Down.png | Bin 0 -> 810 bytes Arrows/Left.png | Bin 0 -> 802 bytes Arrows/Right.png | Bin 0 -> 817 bytes Arrows/Up.png | Bin 0 -> 821 bytes BaseCar.java | 79 ++++++++++++++++++++++++++++ Direction.java | 16 ++++++ DrawFrame.java | 125 +++++++++++++++++++++++++++++++++++++++++++++ DrawingTanker.java | 89 ++++++++++++++++++++++++++++++++ Main.java | 6 +++ WheelCounter.java | 14 +++++ WheelsDrawing.java | 30 +++++++++++ 11 files changed, 359 insertions(+) create mode 100644 Arrows/Down.png create mode 100644 Arrows/Left.png create mode 100644 Arrows/Right.png create mode 100644 Arrows/Up.png create mode 100644 BaseCar.java create mode 100644 Direction.java create mode 100644 DrawFrame.java create mode 100644 DrawingTanker.java create mode 100644 Main.java create mode 100644 WheelCounter.java create mode 100644 WheelsDrawing.java diff --git a/Arrows/Down.png b/Arrows/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/Arrows/Left.png b/Arrows/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/Arrows/Up.png b/Arrows/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/BaseCar.java b/BaseCar.java new file mode 100644 index 0000000..3370cea --- /dev/null +++ b/BaseCar.java @@ -0,0 +1,79 @@ +import java.awt.*; + +public class BaseCar { + private int Speed; + public int getSpeed() { + return Speed; + } + private void setSpeed(int speed) + { + Speed = speed; + } + private double Weight; + public double getWeight() + { + return Weight; + } + private void setWeight(double weight) + { + Weight = weight; + } + private Color BodyColor; + public Color getBodyColor() + { + return BodyColor; + } + private void setBodyColor(Color bodyColor) + { + BodyColor = bodyColor; + } + private Color AdditionalColor; + public Color getAdditionalColor() + { + return AdditionalColor; + } + private void setAdditionalColor(Color additionalColor) + { + AdditionalColor = additionalColor; + } + private boolean BodyKit; + public boolean getBodyKit() + { + return BodyKit; + } + private void setBodyKit(boolean bodyKit) + { + BodyKit = bodyKit; + } + private boolean Wing; + public boolean getWing() + { + return Wing; + } + private void setWing(boolean wing) + { + Wing = wing; + } + private boolean SportLine; + public boolean getSportLine() + { + return SportLine; + } + private void setSportLine(boolean sportLine) + { + SportLine = sportLine; + } + public double Step; + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + BodyKit = bodyKit; + Wing = wing; + SportLine = sportLine; + Step = (double)Speed * 100 / Weight; + } +} diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..5e8001c --- /dev/null +++ b/Direction.java @@ -0,0 +1,16 @@ +public enum Direction { + Up ("U"), + Down ("D"), + Left ("L"), + Right ("R"); + private String direct; + Direction(String d) + { + direct = d; + } + public String getDirect() + { + return direct; + } + +} diff --git a/DrawFrame.java b/DrawFrame.java new file mode 100644 index 0000000..0c64d8f --- /dev/null +++ b/DrawFrame.java @@ -0,0 +1,125 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +class DrawGasoline extends JComponent { + private DrawingTanker _drawingTanker; + public void paintComponent(Graphics g) + { + super.paintComponent(g); + if (_drawingTanker == null) + return; + _drawingTanker.DrawTransport(g); + super.repaint(); + } + protected void CreateCarButton_Click() + { + Random rnd = new Random(); + _drawingTanker = new DrawingTanker(); + Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + _drawingTanker.Init(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), + bodyColor, addColor, IntToBool(rnd.nextInt(0, 2)), IntToBool(rnd.nextInt(0, 2)), + IntToBool(rnd.nextInt(0, 2)), Frame.Width, Frame.Height, rnd.nextInt(0, 30)); + _drawingTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + super.repaint(); + } + protected void ButtonMove_Click(String tag) + { + if (_drawingTanker == null) + return; + switch (tag) { + case "U" -> _drawingTanker.MoveTransport(Direction.Up); + case "D" -> _drawingTanker.MoveTransport(Direction.Down); + case "L" -> _drawingTanker.MoveTransport(Direction.Left); + case "R" -> _drawingTanker.MoveTransport(Direction.Right); + } + super.repaint(); + } + + private boolean IntToBool(int n) + { + return n > 0; + } +} + +class Frame extends JFrame { + public Frame() + { + initUI(); + } + + protected static final int Width = 1000; + protected static final int Height = 600; + DrawGasoline Gasoline; + private void initUI() + { + setSize(Width, Height); + setTitle("TankGasoline"); + setLocationRelativeTo(null); + setLayout(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setResizable(false); + Gasoline = new DrawGasoline(); + Gasoline.setBounds(0, 0, Width, Height); + add(Gasoline); + + MoveAL moving = new MoveAL(); + + JButton buttonUp = new JButton(); + buttonUp.setActionCommand("U"); + buttonUp.setBounds(Width - 90, Height - 120,30,30); + buttonUp.setLayout(null); + add(buttonUp); + buttonUp.addActionListener(moving); + + JButton buttonRight = new JButton(); + buttonRight.setActionCommand("R"); + buttonRight.setBounds(Width - 60, Height - 90,30,30); + buttonRight.setLayout(null); + add(buttonRight); + buttonRight.addActionListener(moving); + + JButton buttonLeft = new JButton(); + buttonLeft.setActionCommand("L"); + buttonLeft.setBounds(Width - 120, Height - 90,30,30); + buttonLeft.setLayout(null); + add(buttonLeft); + buttonLeft.addActionListener(moving); + + JButton buttonDown = new JButton(); + buttonDown.setActionCommand("D"); + buttonDown.setBounds(Width - 90, Height - 90,30,30); + buttonDown.setLayout(null); + add(buttonDown); + buttonDown.addActionListener(moving); + + + buttonUp.setIcon(new ImageIcon("Arrows/Up.png")); + buttonDown.setIcon(new ImageIcon("Arrows/Down.png")); + buttonLeft.setIcon(new ImageIcon("Arrows/Left.png")); + buttonRight.setIcon(new ImageIcon("Arrows/Right.png")); + + JButton buttonCreate = new JButton("Create"); + buttonCreate.setBounds(60, Height - 120, 120, 50); + buttonCreate.setLayout(null); + add(buttonCreate); + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Gasoline.CreateCarButton_Click(); + } + }); + + } + public class MoveAL implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + Gasoline.ButtonMove_Click(e.getActionCommand()); + } + } + +} diff --git a/DrawingTanker.java b/DrawingTanker.java new file mode 100644 index 0000000..61cd005 --- /dev/null +++ b/DrawingTanker.java @@ -0,0 +1,89 @@ +import java.awt.*; + +public class DrawingTanker { + private BaseCar GasolineTanker; + private WheelsDrawing wheelsDrawing; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private final int _carWidth = 120; + private final int _carHeight = 120; + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height, int wheelCount) + { + _pictureHeight = height; + _pictureWidth = width; + GasolineTanker = new BaseCar(); + GasolineTanker.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); + wheelsDrawing = new WheelsDrawing(); + wheelsDrawing.setWheelCount(wheelCount); + } + public void SetPosition(int x, int y) + { + _startPosX = x; + _startPosY = y; + } + public void MoveTransport(Direction direction) + { + if (GasolineTanker == null) + return; + switch (direction) + { + case Left -> { + if (_startPosX - GasolineTanker.Step > 0) + { + _startPosX -= (int)GasolineTanker.Step; + } + } + case Up -> { + if (_startPosY - GasolineTanker.Step > 0) + { + _startPosY -= (int)GasolineTanker.Step; + } + } + case Right -> { + if (_startPosX + _carWidth + GasolineTanker.Step < _pictureWidth) + { + _startPosX += (int)GasolineTanker.Step; + } + } + case Down -> { + if (_startPosY + GasolineTanker.Step + _carHeight < _pictureHeight) + { + _startPosY += (int)GasolineTanker.Step; + } + } + + } + } + public void DrawTransport(Graphics g) + { + if (GasolineTanker == null) + return; + var g2d = (Graphics2D) g; + if (GasolineTanker.getBodyKit()) + { + g2d.setColor(GasolineTanker.getAdditionalColor()); + g2d.fillOval(10 + _startPosX, 10 + _startPosY, 70, 30); + } + g2d.setColor(GasolineTanker.getBodyColor()); + // Отрисовка корпуса + g2d.fillRect(10 + _startPosX, 40 + _startPosY, 90, 20); + g2d.fillRect(80 + _startPosX, 10 + _startPosY, 20, 40); + // Отрисовка колесиков + wheelsDrawing.DrawWheels(_startPosX, _startPosY, GasolineTanker.getBodyColor(), g2d); + if (GasolineTanker.getSportLine()) + { + g2d.setColor(GasolineTanker.getAdditionalColor()); + g2d.fillRect( 15 + _startPosX, 45 + _startPosY, 20, 5); + g2d.fillRect(40 + _startPosX, 45 + _startPosY, 20, 5); + g2d.fillRect(65 + _startPosX, 45 + _startPosY, 20, 5); + } + + if (GasolineTanker.getWing()) + { + g2d.setColor(GasolineTanker.getAdditionalColor()); + g2d.fillRect(87 + _startPosX, 5 + _startPosY, 5, 5); + } + } +} 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..e01e72a --- /dev/null +++ b/WheelCounter.java @@ -0,0 +1,14 @@ +public enum WheelCounter { + TWO (2), + THREE (3), + FOUR (4); + private int count; + WheelCounter(int c) + { + count = c; + } + public int getCount() + { + return count; + } +} diff --git a/WheelsDrawing.java b/WheelsDrawing.java new file mode 100644 index 0000000..43ef1a4 --- /dev/null +++ b/WheelsDrawing.java @@ -0,0 +1,30 @@ +import java.awt.*; + +public class WheelsDrawing { + private WheelCounter wheelCounter; + public WheelCounter getWheelCounter() + { + return wheelCounter; + } + public void setWheelCount(int count) + { + if (count % 3 == 0) + wheelCounter = WheelCounter.THREE; + else if (count % 3 == 1) + wheelCounter = WheelCounter.FOUR; + else if (count % 3 == 2) + wheelCounter = WheelCounter.TWO; + } + + public void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d) + { + g2d.setColor(bodyColor); + g2d.fillOval(10 + _startPosX, 60 + _startPosY, 20, 20); + g2d.fillOval(80 + _startPosX, 60 + _startPosY, 20, 20); + switch (wheelCounter) + { + case THREE -> {g2d.fillOval(45 + _startPosX, 60 + _startPosY ,20, 20);} + case FOUR -> {g2d.fillOval(30 + _startPosX, 60 + _startPosY ,20, 20); g2d.fillOval(60 + _startPosX, 60 + _startPosY ,20, 20);} + } + } +}