From 002d5890d9f6af74a5fdbd5ce68bdd2a476044cc Mon Sep 17 00:00:00 2001 From: GokaPek <109132407+GokaPek@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:52:02 +0400 Subject: [PATCH] Creating --- Arrows/down-arrow.png | Bin 0 -> 281 bytes Arrows/left-arrow.png | Bin 0 -> 265 bytes Arrows/right-arrow.png | Bin 0 -> 224 bytes Arrows/upper-arrow.png | Bin 0 -> 264 bytes DirectionType.java | 13 +++ DrawingRollers.java | 65 +++++++++++++ DrawingSPAU.java | 161 +++++++++++++++++++++++++++++++ EntitySPAU.java | 24 +++++ Form.java | 164 ++++++++++++++++++++++++++++++++ NumbeRollers.java | 28 ++++++ README.md | 2 +- SelfPropelledArtilleryUnit.java | 11 +++ 12 files changed, 467 insertions(+), 1 deletion(-) create mode 100644 Arrows/down-arrow.png create mode 100644 Arrows/left-arrow.png create mode 100644 Arrows/right-arrow.png create mode 100644 Arrows/upper-arrow.png create mode 100644 DirectionType.java create mode 100644 DrawingRollers.java create mode 100644 DrawingSPAU.java create mode 100644 EntitySPAU.java create mode 100644 Form.java create mode 100644 NumbeRollers.java create mode 100644 SelfPropelledArtilleryUnit.java diff --git a/Arrows/down-arrow.png b/Arrows/down-arrow.png new file mode 100644 index 0000000000000000000000000000000000000000..9d67143363585e3432a1c1d203ab1b9c01976c50 GIT binary patch literal 281 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz&H|6fVg?3oArNM~bhqvgQ1G;; zi(^Q|oVQcGxehr9xZG!PJ=iKPkv~* zc9Kx^`?K)}4&8QsA++)Z=NaeL@TnCH#^;@Tgv2kJgjw7>ZYuCH(qz)4*DH2Ru$Mb* cz?1ZWvE`jXwNtr}CD2<8p00i_>zopr0H3RI6#xJL literal 0 HcmV?d00001 diff --git a/Arrows/left-arrow.png b/Arrows/left-arrow.png new file mode 100644 index 0000000000000000000000000000000000000000..046470dd1579e2ce99e38e48677f93b995460f47 GIT binary patch literal 265 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz&H|6fVg?3oArNM~bhqvgP;j@W zi(^Q|oVU|#xtJVz+~TLPvK|zU=>9%IR50azvg(S67o|20$2W@PT-(3ZQ}D>v**$AM z9~3wt^4`$XaKfZXmnNm`XLx_4;d#{Y1DqBNvH2B@Ar5QPm(+f2is!A77T$Nb@L`vJ z!N<1uwhtDSGR)zvNq*OSE>?;q>{)3O*9}3RhUml3c6<6X?w%M??e!!o{lH-l9p`3y z&ohn7Cq`t%+o+y0?@ZL`y>m)cVfjVm7Hdm6Pg#bRdv?g zo_d7i&cBX%-Up_*C#c3a-842yRoo=xRhZ?*{Ndc)p1PG@KZIFQcRaRubCl_?gn{+W T1E1yqUB}?*>gTe~DWM4frWsJb literal 0 HcmV?d00001 diff --git a/Arrows/upper-arrow.png b/Arrows/upper-arrow.png new file mode 100644 index 0000000000000000000000000000000000000000..0bc323d4a84a7434e950a34a05d6b0cd4e0a1c26 GIT binary patch literal 264 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz&H|6fVg?3oArNM~bhqvgP;i&0 zi(^Q|oVQbbxegf!xW#)r9MrC8xaTY)c*L7CcFT3a^G|}xyX^1!D7K_1`USOA8kl%4 zuawtak|5Fc@p(wlfHmbNxVQmhc^IrFsVWmFgSL${tdEQr8i?PC!r5 z-mCYs_=f9zf$C58UcX%ZI{$%y&F{@MtaofZ#Vy%)J(#?rbq3?~4jUeUwjGSzQ!Gv{ z%T?&_jecpHJ#SL+0#A 6 ){ + + _numbeRollers = NumbeRollers.fromNumberToEnum(numRollers); + return; + } + _numbeRollers = NumbeRollers.fromNumberToEnum(numRollers); + } + + public DrawingRollers(Graphics2D g2d, int startPosX, int startPosY, Color color, int numRollers){ + this.color = color; + this.g2d = g2d; + this.startPosX = startPosX; + this.startPosY = startPosY; + setNumRollers(numRollers); + } + + public void Draw(){ + g2d.setPaint(Color.BLACK); + Ellipse2D ellipse7 = new Ellipse2D.Double(startPosX + 10, startPosY + 55, 120, 20); + + Ellipse2D ellipse1 = new Ellipse2D.Double(startPosX + 5, startPosY + 50, 20, 20); + Ellipse2D ellipse2 = new Ellipse2D.Double(startPosX + 30, startPosY + 50, 20, 20); + Ellipse2D ellipse3 = new Ellipse2D.Double(startPosX + 55, startPosY + 50, 20, 20); + Ellipse2D ellipse4 = new Ellipse2D.Double(startPosX + 80, startPosY + 50, 20, 20); + Ellipse2D ellipse5 = new Ellipse2D.Double(startPosX + 105, startPosY + 50, 20, 20); + Ellipse2D ellipse6 = new Ellipse2D.Double(startPosX + 125, startPosY + 60, 10, 10); + g2d.setPaint(color); + g2d.draw(ellipse7); + switch (_numbeRollers) { + case Min: + g2d.fill(ellipse1); + g2d.fill(ellipse2); + g2d.fill(ellipse3); + g2d.fill(ellipse4); + break; + case Mid: + g2d.fill(ellipse1); + g2d.fill(ellipse2); + g2d.fill(ellipse3); + g2d.fill(ellipse4); + g2d.fill(ellipse5); + break; + case Max: + g2d.fill(ellipse1); + g2d.fill(ellipse2); + g2d.fill(ellipse3); + g2d.fill(ellipse4); + g2d.fill(ellipse5); + g2d.fill(ellipse6); + break; + } + } +} diff --git a/DrawingSPAU.java b/DrawingSPAU.java new file mode 100644 index 0000000..ae82ea5 --- /dev/null +++ b/DrawingSPAU.java @@ -0,0 +1,161 @@ +import java.awt.*; +import java.awt.geom.Line2D; +import java.awt.geom.Path2D; + +public class DrawingSPAU { + private EntitySPAU EntitySPAU; + private int pictureWidth; + private int pictureHeight; + private int startPosX; + private int startPosY; + private int _numbeRollers; + private final int carWidth = 135; + private final int carHeight = 75; + + public void setEntitySPAU(EntitySPAU entitySPAU) { + this.EntitySPAU = entitySPAU; + } + public boolean Init(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height) + { + this._numbeRollers = _numbeRollers; + this.pictureWidth = width; + this.pictureHeight = height; + if (this.carHeight >= height) + { + return false; + } + if (this.carWidth >= width) + { + return false; + } + EntitySPAU = new EntitySPAU(); + EntitySPAU.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); + return true; + } + + public void SetPosition(int x, int y) + { + if (x < 0 || x > this.pictureWidth - this.carWidth) + { + x = 0; + } + if (y < 0 || y > this.pictureHeight - this.carHeight) + { + y = 0; + } + this.startPosX = x; + this.startPosY = y; + } + public void MoveTransport(DirectionType direction) + { + if (EntitySPAU == null) + { + return; + } + switch (direction) + { + //влево + case Left: + if (this.startPosX - EntitySPAU.Step > 0) + { + this.startPosX -= (int)EntitySPAU.Step; + } + break; + //вверх + case Up: + if (this.startPosY - EntitySPAU.Step > 0) + { + this.startPosY -= (int)EntitySPAU.Step; + } + break; + // вправо + case Right: + if (this.startPosX + EntitySPAU.Step < this.pictureWidth - this.carWidth) + { + this.startPosX += (int)EntitySPAU.Step; + } + break; + //вниз + case Down: + if (this.startPosY + EntitySPAU.Step < this.pictureHeight - this.carHeight) + { + this.startPosY += (int)EntitySPAU.Step; + } + break; + } + } + + public void DrawTransport(Graphics g) { + if (EntitySPAU == null) { + return; + } + + Graphics2D g2d = (Graphics2D) g; + + BasicStroke penBlack = new BasicStroke(1); + Color additionalColor = EntitySPAU.AdditionalColor; + Color bodyColor = EntitySPAU.BodyColor; + boolean hasBodyKit = EntitySPAU.BodyKit; + + Path2D.Double path = new Path2D.Double(); + + // Обвесы + if (hasBodyKit) { + path.moveTo(startPosX + 15, startPosY + 20); + path.lineTo(startPosX + 35, startPosY + 20); + path.lineTo(startPosX + 35, startPosY + 60); + path.lineTo(startPosX + 15, startPosY + 60); + path.closePath(); + + g2d.setPaint(additionalColor); + g2d.fill(path); + + g2d.setPaint(Color.BLACK); + g2d.setStroke(penBlack); + g2d.draw(path); + path.reset(); + + Line2D line = new Line2D.Double(startPosX + 5, startPosY + 20, startPosX + 15, startPosY + 25); + g2d.draw(line); + } + + //гусеницы + DrawingRollers Rollers = new DrawingRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers); + Rollers.Draw(); + + // пушка + path.moveTo(startPosX + 35, startPosY + 40); + path.lineTo(startPosX + 40, startPosY + 45); + path.lineTo(startPosX + 135, startPosY + 5); + path.lineTo(startPosX + 130, startPosY + 0); + path.closePath(); + + g2d.setPaint(bodyColor); + g2d.fill(path); + g2d.draw(path); + path.reset(); + + // корпус + path.moveTo(startPosX, startPosY + 60); + path.lineTo(startPosX + 10, startPosY + 30); + path.lineTo(startPosX + 130, startPosY + 30); + path.lineTo(startPosX + 135, startPosY + 60); + path.closePath(); + + g2d.setPaint(bodyColor); + g2d.fill(path); + g2d.draw(path); + path.reset(); + + // башня + path.moveTo(startPosX + 40, startPosY + 30); + path.lineTo(startPosX + 45, startPosY + 15); + path.lineTo(startPosX + 70, startPosY + 15); + path.lineTo(startPosX + 75, startPosY + 30); + path.closePath(); + + g2d.setPaint(bodyColor); + g2d.fill(path); + g2d.draw(path); + } +} diff --git a/EntitySPAU.java b/EntitySPAU.java new file mode 100644 index 0000000..9dc1ee5 --- /dev/null +++ b/EntitySPAU.java @@ -0,0 +1,24 @@ +import java.awt.Color; + +public class EntitySPAU { + + public int Speed; + public double Weight; + public Color BodyColor; + public Color AdditionalColor; + public boolean BodyKit; + public boolean Wing; + public boolean SportLine; + public double Step = (double)Speed * 100 / Weight; + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine) { + Speed = speed; + Weight = weight; + Step = (double)Speed * 100 / Weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + BodyKit = bodyKit; + Wing = wing; + SportLine = sportLine; + } +} \ No newline at end of file diff --git a/Form.java b/Form.java new file mode 100644 index 0000000..f2ab465 --- /dev/null +++ b/Form.java @@ -0,0 +1,164 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class Form extends JFrame { + + private JPanel mainPanel; + private JButton buttonCreate; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + private JLabel pictureBoxSPAU; + private DrawingSPAU drawingSPAU; + private JTextField wheelsTextField; + private JLabel wheelsLabel; + + public Form() { + initComponents(); + } + + private void Draw() { + if (drawingSPAU == null) { + return; + } + BufferedImage bmp = new BufferedImage(pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D gr = bmp.createGraphics(); + drawingSPAU.DrawTransport(gr); + ImageIcon imageIcon = new ImageIcon(bmp); + pictureBoxSPAU.setIcon(imageIcon); + } + + private void initComponents() { + mainPanel = new JPanel(); + buttonCreate = new JButton("Создать"); + + buttonUp = new JButton(new ImageIcon("resources/upper_arrow.png")); + buttonDown = new JButton(new ImageIcon("resources/down_arrow.png")); + buttonLeft = new JButton(new ImageIcon("resources/left_arrow.png")); + buttonRight = new JButton(new ImageIcon("resources/right_arrow.png")); + pictureBoxSPAU = new JLabel(); + wheelsLabel = new JLabel("Количество колёс:"); + wheelsTextField = new JTextField(); + + setLayout(new BorderLayout()); + add(mainPanel, BorderLayout.CENTER); + mainPanel.setLayout(null); + + pictureBoxSPAU.setBounds(0, 0, 882, 453); + mainPanel.add(pictureBoxSPAU); + + wheelsLabel.setBounds(22, 410, 150, 29); + mainPanel.add(wheelsLabel); + + wheelsTextField.setBounds(150, 410, 80, 29); + mainPanel.add(wheelsTextField); + + buttonCreate.setBounds(250, 410, 100, 29); + mainPanel.add(buttonCreate); + buttonCreate.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + buttonCreateActionPerformed(e); + } + }); + + buttonUp.setBounds(804, 336, 30, 30); + mainPanel.add(buttonUp); + ImageIcon iconUp = new ImageIcon("Arrows/upper-arrow.png"); + buttonUp.setIcon(iconUp); + buttonUp.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + buttonMoveActionPerformed(buttonUp, e); + } + }); + + buttonDown.setBounds(804, 409, 30, 30); + mainPanel.add(buttonDown); + ImageIcon iconDown = new ImageIcon("Arrows/down-arrow.png"); + buttonDown.setIcon(iconDown); + buttonDown.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + buttonMoveActionPerformed(buttonDown, e); + } + }); + + buttonLeft.setBounds(768, 372, 30, 30); + mainPanel.add(buttonLeft); + ImageIcon iconLeft = new ImageIcon("Arrows/left-arrow.png"); + buttonLeft.setIcon(iconLeft); + buttonLeft.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + buttonMoveActionPerformed(buttonLeft, e); + } + }); + + buttonRight.setBounds(840, 372, 30, 30); + mainPanel.add(buttonRight); + ImageIcon iconRight = new ImageIcon("Arrows/right-arrow.png"); + buttonRight.setIcon(iconRight); + buttonRight.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + buttonMoveActionPerformed(buttonRight, e); + } + }); + buttonUp.setName("buttonUp"); + buttonDown.setName("buttonDown"); + buttonLeft.setName("buttonLeft"); + buttonRight.setName("buttonRight"); + + setPreferredSize(new Dimension(882, 453)); + setDefaultCloseOperation(EXIT_ON_CLOSE); + pack(); + } + + protected void buttonCreateActionPerformed(ActionEvent e) { + int wheelCount; + try { + wheelCount = Integer.parseInt(wheelsTextField.getText()); + } catch (NumberFormatException ex) { + wheelCount = 0; + } + + if (wheelCount > 0) { + Random random = new Random(); + drawingSPAU = new DrawingSPAU(); + drawingSPAU.Init(wheelCount, random.nextInt(200) + 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)), + random.nextBoolean(), + random.nextBoolean(), + random.nextBoolean(), + pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight()); + drawingSPAU.SetPosition(random.nextInt(90) + 10, + random.nextInt(90) + 10); + Draw(); + } + } + + protected void buttonMoveActionPerformed(Object sender, ActionEvent e) { + if (drawingSPAU == null) { + return; + } + String name = (sender instanceof JButton) ? ((JButton) sender).getName() : ""; + switch (name) { + case "buttonUp": + drawingSPAU.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + drawingSPAU.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + drawingSPAU.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + drawingSPAU.MoveTransport(DirectionType.Right); + break; + } + Draw(); + } +} \ No newline at end of file diff --git a/NumbeRollers.java b/NumbeRollers.java new file mode 100644 index 0000000..a6bf697 --- /dev/null +++ b/NumbeRollers.java @@ -0,0 +1,28 @@ + +public enum NumbeRollers { + Min(4), + Mid(5), + Max(6); + + private final int NumbeRollersCode; + + private NumbeRollers(int NumbeRollersCode) { + this.NumbeRollersCode = NumbeRollersCode; + } + + public int getNumbeRollersCode() { + return this.NumbeRollersCode; + } + + public static NumbeRollers fromNumberToEnum(int number) { + try{ + for (NumbeRollers numbeRoller : NumbeRollers.values()) { + if (numbeRoller.getNumbeRollersCode() == number) { + return numbeRoller; + } + } + }catch(NumberFormatException e){ + } + return Min; + } +} \ No newline at end of file diff --git a/README.md b/README.md index 48792a5..2d643fa 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# PIbd-22_Petrushin_E.A._SelfPropelledArtilleryUnit_Hard_ +# PIbd-22_Petrushin_E.A._SelfPropelledArtilleryUnit_Hard diff --git a/SelfPropelledArtilleryUnit.java b/SelfPropelledArtilleryUnit.java new file mode 100644 index 0000000..059f215 --- /dev/null +++ b/SelfPropelledArtilleryUnit.java @@ -0,0 +1,11 @@ +import javax.swing.*; + +public class SelfPropelledArtilleryUnit { + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + Form form = new Form(); + form.setSize(900, 500); + form.setVisible(true); + }); + } +} \ No newline at end of file