From ce4309fe95f6dfaf25eb59e19d62eab7ba904047 Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Tue, 19 Dec 2023 19:39:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B4=D0=BE=D0=BB=D0=B6?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningObjects/DrawningMonorail.java | 7 +- .../DrawningSecondMonorail.java | 6 +- Monorail/MonoFrame.java | 120 +++++++++++++++--- 3 files changed, 111 insertions(+), 22 deletions(-) diff --git a/Monorail/DrawningObjects/DrawningMonorail.java b/Monorail/DrawningObjects/DrawningMonorail.java index 707fd23..b5198a2 100644 --- a/Monorail/DrawningObjects/DrawningMonorail.java +++ b/Monorail/DrawningObjects/DrawningMonorail.java @@ -13,15 +13,17 @@ public class DrawningMonorail extends JPanel { private int pictureHeight; private int _startPosX; private int _startPos ; + private int rollVar; public int relWidth = 150; private int relHeight = 46; Random rd = new Random(); public DrawingWheels _drawingWheels; - public DrawningMonorail(int wheelCount ,int speed, double weight, Color bodyColor, int width, int height){ + public DrawningMonorail(int wheelCount ,int rollVar,int speed, double weight, Color bodyColor, int width, int height){ if (relWidth >= width || relHeight >= height) { return; } + this.rollVar = rollVar; pictureWidth = width; pictureHeight = height; entity = new EntityMonorail(wheelCount, speed, weight, bodyColor); @@ -35,12 +37,13 @@ public class DrawningMonorail extends JPanel { this.entity = entity; } - protected DrawningMonorail(int wheelCount, int speed, double weight, Color bodyColor, int width, + protected DrawningMonorail(int wheelCount, int rollVar, int speed, double weight, Color bodyColor, int width, int height, int carWidth, int carHeight){ if (relWidth >= width || relHeight >= height) { return; } + this.rollVar = rollVar; pictureWidth = width; pictureHeight = height; relWidth = carWidth; diff --git a/Monorail/DrawningObjects/DrawningSecondMonorail.java b/Monorail/DrawningObjects/DrawningSecondMonorail.java index 1997f22..f98fa11 100644 --- a/Monorail/DrawningObjects/DrawningSecondMonorail.java +++ b/Monorail/DrawningObjects/DrawningSecondMonorail.java @@ -2,12 +2,10 @@ package DrawningObjects; import java.awt.*; import Entities.*; -import Entities.EntitySecondMonorail; - public class DrawningSecondMonorail extends DrawningMonorail{ - public DrawningSecondMonorail(int wheelCount, int speed, double weight, Color bodyColor, Color + public DrawningSecondMonorail(int wheelCount, int rollVar, int speed, double weight, Color bodyColor, Color additionalColor, boolean monorails, boolean secondCabin, int width, int height){ - super(wheelCount, speed, weight, bodyColor, width, height, 110, 60); + super(wheelCount, rollVar, speed, weight, bodyColor, width, height, 110, 60); if(secondCabin){ relWidth = 290; } diff --git a/Monorail/MonoFrame.java b/Monorail/MonoFrame.java index 3c17947..37318c8 100644 --- a/Monorail/MonoFrame.java +++ b/Monorail/MonoFrame.java @@ -6,22 +6,29 @@ import javax.swing.*; import Directions.Direction; import DrawningObjects.DrawningMonorail; +import DrawningObjects.DrawningSecondMonorail; +import MovementStrategy.*; import java.util.Random; class Form extends JFrame{ + private DrawningMonorail _drawningMonorail; + private AbstractStrategy abstractStrategy; private JLabel pictureBoxMonorail; + private JLabel wheelsLabel; private JPanel mainPanel; private JButton btUp; private JButton btDown; private JButton btRight; private JButton btLeft; - private JButton btCreate; - private JLabel wheelsLabel; + private JButton btCreateMonorail; + private JButton btCreateSecondMonorail; + private JButton btStep; + private JComboBox comboBoxWheel; + private JComboBox comboBoxStrategy; private JTextField wheelsTextField; Random rd = new Random(); - private DrawningMonorail _drawningMonorail; public Form(){ initComponents(); @@ -33,13 +40,14 @@ class Form extends JFrame{ BufferedImage bmp = new BufferedImage(pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight(),BufferedImage.TYPE_INT_ARGB); Graphics2D gr = bmp.createGraphics(); - _drawningMonorail.drawMonorail(gr); + _drawningMonorail.DrawTransport(gr); ImageIcon imageIcon = new ImageIcon(bmp); pictureBoxMonorail.setIcon(imageIcon); } private void initComponents(){ mainPanel = new JPanel(); - btCreate = new JButton("Создать"); + btCreateMonorail = new JButton("Создать Монорельс"); + btCreateSecondMonorail = new JButton("Создать два монорельса"); btUp = new JButton(new ImageIcon("resources/upper-arrow.png")); btDown = new JButton(new ImageIcon("resources/down-arrow.png")); @@ -63,14 +71,41 @@ class Form extends JFrame{ wheelsTextField.setBounds(150, 410, 80, 29); mainPanel.add(wheelsTextField); - btCreate.setBounds(250, 410, 100, 29); - mainPanel.add(btCreate); - btCreate.addActionListener(new ActionListener() { + btCreateMonorail.setBounds(250, 410, 100, 29); + mainPanel.add(btCreateMonorail); + btCreateMonorail.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { buttonCreateActionPerformed(e); } }); + btCreateSecondMonorail.setBounds(500, 409, 150, 30); + mainPanel.add(btCreateSecondMonorail); + btCreateSecondMonorail.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + btCreateSecondMonorail_Click(e); + } + }); + + btStep = new JButton("Шаг"); + mainPanel.add(btStep); + btStep.setBounds(794, 55, 76, 30); + btStep.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + btStep_Click(e); + } + }); + + comboBoxWheel = new JComboBox<>(); + mainPanel.add(comboBoxWheel); + comboBoxWheel.setModel(new DefaultComboBoxModel<>(new String[] { "1", "2", "3" })); + comboBoxWheel.setBounds(767, 232, 103, 28); + + comboBoxStrategy = new JComboBox<>(); + mainPanel.add(comboBoxStrategy); + comboBoxStrategy.setModel(new DefaultComboBoxModel<>(new String[] { "0", "1" })); + comboBoxStrategy.setBounds(767, 12, 103, 28); + btUp.setBounds(790, 355, 40, 40); mainPanel.add(btUp); ImageIcon iconUp = new ImageIcon("resources/upper-arrow.png"); @@ -131,18 +166,71 @@ class Form extends JFrame{ if (wheelCount > 0) { Random random = new Random(); - _drawningMonorail = new DrawningMonorail(); - _drawningMonorail.init(wheelCount, random.nextInt(500,800), - random.nextInt(1000,3000), + _drawningMonorail = new DrawningMonorail(wheelCount, Integer.parseInt((String) comboBoxWheel.getSelectedItem()), + random.nextInt(200) + 100, (double)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(), pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight()); - _drawningMonorail.setPosition(random.nextInt(90) + 10, - random.nextInt(90) + 10); Draw(); + _drawningMonorail.setPosition(random.nextInt(90) + 10, + random.nextInt(90) + 10); + Draw(); } } + protected void btCreateSecondMonorail_Click(ActionEvent e) { + int wheelCount; + try { + wheelCount = Integer.parseInt(wheelsTextField.getText()); + } catch (NumberFormatException ex) { + wheelCount = 0; + } + + if (wheelCount > 0) { + Random random = new Random(); + _drawningMonorail = new DrawningSecondMonorail(wheelCount, Integer.parseInt((String) comboBoxWheel.getSelectedItem()), + random.nextInt(200) + 100, + (double) 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)), + true, true, + pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight()); + _drawningMonorail.setPosition(random.nextInt(90) + 20, random.nextInt(90) + 20); + Draw(); + } + } + + protected void btStep_Click (ActionEvent e) { + if (_drawningMonorail == null) { + return; + } + if (comboBoxStrategy.isEnabled()) { + int selectedIndex = comboBoxStrategy.getSelectedIndex(); + switch (selectedIndex) { + case 0: + abstractStrategy = new MoveToCenter(); + break; + case 1: + abstractStrategy = new MoveToBorder(); + break; + default: + abstractStrategy = null; + break; + } + if (abstractStrategy == null) { + return; + } + abstractStrategy.setData(new DrawingObjectMonorail(_drawningMonorail), pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight()); + comboBoxStrategy.setEnabled(false); + } + if (abstractStrategy == null) { + return; + } + abstractStrategy.makeStep(); + Draw(); + if (abstractStrategy.getStatus() == Status.Finish) { + comboBoxStrategy.setEnabled(true); + abstractStrategy = null; + } + } + protected void buttonMoveActionPerformed(Object sender, ActionEvent e) { if (_drawningMonorail == null) { return;