From f65488323305308d28e58aef188835f2b4085216 Mon Sep 17 00:00:00 2001 From: GokaPek <109132407+GokaPek@users.noreply.github.com> Date: Wed, 25 Oct 2023 14:10:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawingRollers.java | 65 +++++++++++++++++++++++++++++++++++++++++++++ DrawingSPAU.java | 25 +++++------------ Form.java | 55 +++++++++++++++++++++++++------------- NumbeRollers.java | 28 +++++++++++++++++++ 4 files changed, 137 insertions(+), 36 deletions(-) create mode 100644 DrawingRollers.java create mode 100644 NumbeRollers.java diff --git a/DrawingRollers.java b/DrawingRollers.java new file mode 100644 index 0000000..3be2998 --- /dev/null +++ b/DrawingRollers.java @@ -0,0 +1,65 @@ +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.geom.Ellipse2D; + +public class DrawingRollers { + Graphics2D g2d; + int startPosX; + int startPosY; + Color color; + private NumbeRollers _numbeRollers; + + public void setNumRollers(int numRollers) { + if (numRollers < 4 || numRollers > 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 index e7095e4..ae82ea5 100644 --- a/DrawingSPAU.java +++ b/DrawingSPAU.java @@ -1,5 +1,4 @@ import java.awt.*; -import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Path2D; @@ -9,14 +8,16 @@ public class DrawingSPAU { 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 speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height) + 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) @@ -117,22 +118,10 @@ public class DrawingSPAU { Line2D line = new Line2D.Double(startPosX + 5, startPosY + 20, startPosX + 15, startPosY + 25); g2d.draw(line); } - - // Гусеницы - 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); - g2d.setPaint(Color.BLACK); - g2d.fill(ellipse1); - g2d.fill(ellipse2); - g2d.fill(ellipse3); - g2d.fill(ellipse4); - g2d.fill(ellipse5); - - Ellipse2D ellipse6 = new Ellipse2D.Double(startPosX + 10, startPosY + 55, 113, 20); - g2d.draw(ellipse6); + + //гусеницы + DrawingRollers Rollers = new DrawingRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers); + Rollers.Draw(); // пушка path.moveTo(startPosX + 35, startPosY + 40); diff --git a/Form.java b/Form.java index 62b5294..f2ab465 100644 --- a/Form.java +++ b/Form.java @@ -15,17 +15,18 @@ public class Form extends JFrame { 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); + BufferedImage bmp = new BufferedImage(pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D gr = bmp.createGraphics(); drawingSPAU.DrawTransport(gr); ImageIcon imageIcon = new ImageIcon(bmp); @@ -35,12 +36,15 @@ public class Form extends JFrame { 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); @@ -48,7 +52,13 @@ public class Form extends JFrame { pictureBoxSPAU.setBounds(0, 0, 882, 453); mainPanel.add(pictureBoxSPAU); - buttonCreate.setBounds(22, 410, 82, 29); + 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) { @@ -106,19 +116,28 @@ public class Form extends JFrame { } protected void buttonCreateActionPerformed(ActionEvent e) { - Random random = new Random(); - drawingSPAU = new DrawingSPAU(); - drawingSPAU.Init(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(); + 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) { 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