From e67810013bccf0870e6d6585e586a89041dc2947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Tue, 5 Dec 2023 14:28:37 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=203?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20(=D0=A3=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawingObjects/DrawingLocomotive.java | 17 ++ .../DrawingObjects/DrawingMonorail.java | 10 +- src/projectMonorail/Extras/RandomGeneric.java | 58 ++++++ src/projectMonorail/FrameMonorail.java | 12 +- .../FrameMonorailCollection.java | 179 ++++++++++++++++++ .../FrameRandomGeneration.java | 20 ++ .../LocomotivesGenericCollection.java | 87 +++++++++ src/projectMonorail/Generics/SetGeneric.java | 53 ++++++ src/projectMonorail/Main.java | 2 +- src/projectMonorail/PictureBox.java | 60 +++++- src/projectMonorail/PictureBoxCollection.java | 25 +++ .../PictureBoxRandomGeneration.java | 63 ++++++ 12 files changed, 570 insertions(+), 16 deletions(-) create mode 100644 src/projectMonorail/Extras/RandomGeneric.java create mode 100644 src/projectMonorail/FrameMonorailCollection.java create mode 100644 src/projectMonorail/FrameRandomGeneration.java create mode 100644 src/projectMonorail/Generics/LocomotivesGenericCollection.java create mode 100644 src/projectMonorail/Generics/SetGeneric.java create mode 100644 src/projectMonorail/PictureBoxCollection.java create mode 100644 src/projectMonorail/PictureBoxRandomGeneration.java diff --git a/src/projectMonorail/DrawingObjects/DrawingLocomotive.java b/src/projectMonorail/DrawingObjects/DrawingLocomotive.java index 9e696d4..e9c243f 100644 --- a/src/projectMonorail/DrawingObjects/DrawingLocomotive.java +++ b/src/projectMonorail/DrawingObjects/DrawingLocomotive.java @@ -3,6 +3,7 @@ package projectMonorail.DrawingObjects; import projectMonorail.DirectionType; import projectMonorail.Entities.*; import projectMonorail.Extras.*; +import projectMonorail.MovementStrategy.*; import java.awt.*; import java.util.Random; @@ -49,6 +50,10 @@ public class DrawingLocomotive { return locomotiveHeight; } + public IMoveableObject getMoveableObject() { + return new DrawingObjectLocomotive(this); + } + public DrawingLocomotive(int speed, double weight, Color mainColor, int width, int height, int wheelNumber) { if (width < locomotiveWidth || height < locomotiveHeight) { return; @@ -86,6 +91,18 @@ public class DrawingLocomotive { drawingWheels.setWheelNumber(wheelNumber); } + public DrawingLocomotive(EntityLocomotive entityLocomotive, int width, int height, int wheelNumber, + IDrawingWheels drawingWheels) { + if (width < locomotiveWidth || height < locomotiveHeight) { + return; + } + pictureWidth = width; + pictureHeight = height; + this.entityLocomotive = entityLocomotive; + this.drawingWheels = drawingWheels; + drawingWheels.setWheelNumber(wheelNumber); + } + public void setPosition(int x, int y) { if (x < 0 || x + locomotiveWidth > pictureWidth) { x = 0; diff --git a/src/projectMonorail/DrawingObjects/DrawingMonorail.java b/src/projectMonorail/DrawingObjects/DrawingMonorail.java index 2a9b3ab..02d41e6 100644 --- a/src/projectMonorail/DrawingObjects/DrawingMonorail.java +++ b/src/projectMonorail/DrawingObjects/DrawingMonorail.java @@ -1,6 +1,7 @@ package projectMonorail.DrawingObjects; import projectMonorail.Entities.*; +import projectMonorail.Extras.*; import java.awt.*; @@ -25,6 +26,11 @@ public class DrawingMonorail extends DrawingLocomotive { } } + public DrawingMonorail(EntityMonorail entityMonorail, int width, int height, int wheelNumber, + IDrawingWheels drawingWheels) { + super(entityMonorail, width, height, wheelNumber, drawingWheels); + } + @Override public void drawTransport(Graphics2D g2d) { if (!(getEntityLocomotive() instanceof EntityMonorail monorail)) { @@ -54,7 +60,7 @@ public class DrawingMonorail extends DrawingLocomotive { //дополнительная кабина if (monorail.getExtraCabin()) { //корпус дополнительной кабины - g2d.setColor(mainColor); + g2d.setColor(additionalColor); g2d.fillRect(startPosX + 118, startPosY + 15, 65, 31); g2d.setColor(Color.BLACK); g2d.drawRect(startPosX + 118, startPosY + 15, 65, 31); @@ -64,7 +70,7 @@ public class DrawingMonorail extends DrawingLocomotive { g2d.drawLine(startPosX + 118, startPosY + 31, startPosX + 183, startPosY + 31); //дверь дополнительной кабины - g2d.setColor(additionalColor); + g2d.setColor(Color.GRAY); g2d.fillRect(startPosX + 146, startPosY + 21, 7, 20); g2d.setStroke(largerWidth); diff --git a/src/projectMonorail/Extras/RandomGeneric.java b/src/projectMonorail/Extras/RandomGeneric.java new file mode 100644 index 0000000..5e1decd --- /dev/null +++ b/src/projectMonorail/Extras/RandomGeneric.java @@ -0,0 +1,58 @@ +package projectMonorail.Extras; + +import projectMonorail.DrawingObjects.DrawingLocomotive; +import projectMonorail.DrawingObjects.DrawingMonorail; +import projectMonorail.Entities.*; + +import java.util.concurrent.ThreadLocalRandom; + +public class RandomGeneric { + private final E[] locomotives; + + private int locomotivesCurrNum = 0; + + private final D[] wheels; + + private int wheelsCurrNum = 0; + + private int pictureWidth; + + private int pictureHeight; + + public RandomGeneric(int locomotivesCount, int drawingsCount, int pictureWidth, int pictureHeight) { + locomotives = (E[])new EntityLocomotive[locomotivesCount]; + wheels = (D[])new IDrawingWheels[drawingsCount]; + this.pictureWidth = pictureWidth; + this.pictureHeight = pictureHeight; + } + + public void addItem(E locomotive) { + if (locomotive == null || locomotivesCurrNum >= locomotives.length) + return; + + locomotives[locomotivesCurrNum++] = locomotive; + } + + public void addItem(D drawingWheels) { + if (drawingWheels == null || wheelsCurrNum >= wheels.length) + return; + + wheels[wheelsCurrNum++] = drawingWheels; + } + + public DrawingLocomotive getDrawingLocomotive() { + if (locomotivesCurrNum == 0 || wheelsCurrNum == 0) + return null; + + EntityLocomotive locomotive = locomotives[ThreadLocalRandom.current().nextInt(0, locomotivesCurrNum)]; + IDrawingWheels drawingWheels = wheels[ThreadLocalRandom.current().nextInt(0, wheelsCurrNum)]; + + if (locomotive instanceof EntityMonorail) { + return new DrawingMonorail((EntityMonorail) locomotive, pictureWidth, pictureHeight, ThreadLocalRandom.current().nextInt(2, 5), + drawingWheels); + } + + return new DrawingLocomotive(locomotive, pictureWidth, pictureHeight, ThreadLocalRandom.current().nextInt(2, 5), + drawingWheels); + } +} diff --git a/src/projectMonorail/FrameMonorail.java b/src/projectMonorail/FrameMonorail.java index 287cd83..b16a957 100644 --- a/src/projectMonorail/FrameMonorail.java +++ b/src/projectMonorail/FrameMonorail.java @@ -2,13 +2,17 @@ package projectMonorail; import javax.swing.*; -public class FrameMonorail extends JFrame { +public class FrameMonorail extends JDialog { private PictureBox pictureBox; - public FrameMonorail() { - super("Monorail"); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + public PictureBox getPictureBox() { + return pictureBox; + } + + public FrameMonorail(JFrame parent) { + super(parent ,"Monorail", true); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); pictureBox = new PictureBox(); add(pictureBox); diff --git a/src/projectMonorail/FrameMonorailCollection.java b/src/projectMonorail/FrameMonorailCollection.java new file mode 100644 index 0000000..82ab4d5 --- /dev/null +++ b/src/projectMonorail/FrameMonorailCollection.java @@ -0,0 +1,179 @@ +package projectMonorail; + +import projectMonorail.DrawingObjects.*; +import projectMonorail.Generics.*; +import projectMonorail.MovementStrategy.*; + +import javax.swing.*; +import javax.swing.text.NumberFormatter; +import java.awt.*; +import java.text.NumberFormat; + +public class FrameMonorailCollection extends JFrame { + + private final LocomotivesGenericCollection locomotives; + + private JPanel paddingTopPanel; + + private JPanel mainPanel; + + private PictureBoxCollection pictureBoxCollection; + + private int pictureBoxCollectionWidth = 900; + + private int pictureBoxCollectionHeight = 430; + + private JPanel toolsPanel; + + private JLabel nameLabel; + + private JButton buttonAddMonorail; + + private JButton buttonRemoveMonorail; + + private JButton buttonRefreshCollection; + + private JButton buttonRandomGeneration; + + private JFormattedTextField textFieldNumber; + + public FrameMonorailCollection() { + super("Monorail collection"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + locomotives = new LocomotivesGenericCollection<>(pictureBoxCollectionWidth, pictureBoxCollectionHeight); + pictureBoxCollection = new PictureBoxCollection(locomotives, pictureBoxCollectionWidth, + pictureBoxCollectionHeight); + + nameLabel = new JLabel(" Tools"); + nameLabel.setFont(new Font("Segoe UI", Font.BOLD, 16)); + nameLabel.setHorizontalTextPosition(JLabel.LEFT); + nameLabel.setMaximumSize(new Dimension(190, 30)); + nameLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + + buttonAddMonorail = new JButton("Add monorail"); + buttonAddMonorail.setHorizontalTextPosition(JButton.CENTER); + buttonAddMonorail.setVerticalTextPosition(JButton.CENTER); + buttonAddMonorail.setFocusable(false); + buttonAddMonorail.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + buttonAddMonorail.setBackground(Color.WHITE); + buttonAddMonorail.setMaximumSize(new Dimension(155, 36)); + buttonAddMonorail.setBorder(BorderFactory.createLineBorder(Color.black, 2)); + buttonAddMonorail.setAlignmentX(Component.CENTER_ALIGNMENT); + + buttonAddMonorail.addActionListener(e -> { + FrameMonorail frame = new FrameMonorail(this); + if (frame.getPictureBox().getDialogResult()) { + if (locomotives.addition(frame.getPictureBox().getSelectedLocomotive()) != -1) { + JOptionPane.showMessageDialog(null, "Object added", "Message", + JOptionPane.INFORMATION_MESSAGE); + pictureBoxCollection.repaint(); + } else { + JOptionPane.showMessageDialog(null, "Failed to add the object", "Message", + JOptionPane.INFORMATION_MESSAGE); + } + } + }); + + NumberFormat format = NumberFormat.getInstance(); + NumberFormatter formatter = new NumberFormatter(format); + formatter.setValueClass(Integer.class); + formatter.setMinimum(0); + formatter.setMaximum(Integer.MAX_VALUE); + formatter.setAllowsInvalid(false); + formatter.setCommitsOnValidEdit(true); + + textFieldNumber = new JFormattedTextField(formatter); + textFieldNumber.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + textFieldNumber.setMaximumSize(new Dimension(155, 25)); + textFieldNumber.setAlignmentX(Component.CENTER_ALIGNMENT); + + buttonRemoveMonorail = new JButton("Remove monorail"); + buttonRemoveMonorail.setHorizontalTextPosition(JButton.CENTER); + buttonRemoveMonorail.setVerticalTextPosition(JButton.CENTER); + buttonRemoveMonorail.setFocusable(false); + buttonRemoveMonorail.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + buttonRemoveMonorail.setBackground(Color.WHITE); + buttonRemoveMonorail.setMaximumSize(new Dimension(155, 36)); + buttonRemoveMonorail.setBorder(BorderFactory.createLineBorder(Color.black, 2)); + buttonRemoveMonorail.setAlignmentX(Component.CENTER_ALIGNMENT); + + buttonRemoveMonorail.addActionListener(e -> { + if (JOptionPane.showConfirmDialog(null, "Delete the object?", "Deletion", + JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { + return; + } + if (textFieldNumber.getText().isEmpty()) { + return; + } + int pos = Integer.parseInt(textFieldNumber.getText()); + if (locomotives.subtraction(pos)) { + JOptionPane.showMessageDialog(null, "Object deleted", "Message", + JOptionPane.INFORMATION_MESSAGE); + pictureBoxCollection.repaint(); + } else { + JOptionPane.showMessageDialog(null, "Failed to delete the object", "Message", + JOptionPane.INFORMATION_MESSAGE); + } + }); + + buttonRefreshCollection = new JButton("Refresh collection"); + buttonRefreshCollection.setHorizontalTextPosition(JButton.CENTER); + buttonRefreshCollection.setVerticalTextPosition(JButton.CENTER); + buttonRefreshCollection.setFocusable(false); + buttonRefreshCollection.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + buttonRefreshCollection.setBackground(Color.WHITE); + buttonRefreshCollection.setMaximumSize(new Dimension(155, 36)); + buttonRefreshCollection.setBorder(BorderFactory.createLineBorder(Color.black, 2)); + buttonRefreshCollection.setAlignmentX(Component.CENTER_ALIGNMENT); + + buttonRefreshCollection.addActionListener(e -> pictureBoxCollection.repaint()); + + buttonRandomGeneration = new JButton("Show additional frame"); + buttonRandomGeneration.setHorizontalTextPosition(JButton.CENTER); + buttonRandomGeneration.setVerticalTextPosition(JButton.CENTER); + buttonRandomGeneration.setFocusable(false); + buttonRandomGeneration.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + buttonRandomGeneration.setBackground(Color.WHITE); + buttonRandomGeneration.setMaximumSize(new Dimension(155, 36)); + buttonRandomGeneration.setBorder(BorderFactory.createLineBorder(Color.black, 2)); + buttonRandomGeneration.setAlignmentX(Component.CENTER_ALIGNMENT); + + buttonRandomGeneration.addActionListener(e -> new FrameRandomGeneration(this)); + + toolsPanel = new JPanel(); + toolsPanel.setMaximumSize(new Dimension(190, 430)); + toolsPanel.setLayout(new BoxLayout(toolsPanel, BoxLayout.Y_AXIS)); + toolsPanel.setBorder(BorderFactory.createLineBorder(Color.black)); + toolsPanel.add(nameLabel); + toolsPanel.add(Box.createVerticalStrut(14)); + toolsPanel.add(buttonAddMonorail); + toolsPanel.add(Box.createVerticalStrut(77)); + toolsPanel.add(textFieldNumber); + toolsPanel.add(Box.createVerticalStrut(6)); + toolsPanel.add(buttonRemoveMonorail); + toolsPanel.add(Box.createVerticalStrut(74)); + toolsPanel.add(buttonRefreshCollection); + toolsPanel.add(Box.createVerticalStrut(40)); + toolsPanel.add(buttonRandomGeneration); + + mainPanel = new JPanel(); + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS)); + mainPanel.add(Box.createHorizontalStrut(10)); + mainPanel.add(pictureBoxCollection); + mainPanel.add(Box.createHorizontalStrut(15)); + mainPanel.add(toolsPanel); + + paddingTopPanel = new JPanel(); + paddingTopPanel.setLayout(new BoxLayout(paddingTopPanel, BoxLayout.Y_AXIS)); + paddingTopPanel.add(Box.createVerticalStrut(15)); + paddingTopPanel.add(mainPanel); + + add(paddingTopPanel); + + setPreferredSize(new Dimension(990, 478)); + pack(); + setLocationRelativeTo(null); + setVisible(true); + } +} diff --git a/src/projectMonorail/FrameRandomGeneration.java b/src/projectMonorail/FrameRandomGeneration.java new file mode 100644 index 0000000..f61c284 --- /dev/null +++ b/src/projectMonorail/FrameRandomGeneration.java @@ -0,0 +1,20 @@ +package projectMonorail; + +import javax.swing.*; + +public class FrameRandomGeneration extends JDialog { + + private PictureBoxRandomGeneration pictureBox; + + public FrameRandomGeneration(JFrame parent) { + super(parent ,"Random generation", true); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + + pictureBox = new PictureBoxRandomGeneration(400, 400); + add(pictureBox); + + pack(); + setLocationRelativeTo(null); + setVisible(true); + } +} diff --git a/src/projectMonorail/Generics/LocomotivesGenericCollection.java b/src/projectMonorail/Generics/LocomotivesGenericCollection.java new file mode 100644 index 0000000..4bbca05 --- /dev/null +++ b/src/projectMonorail/Generics/LocomotivesGenericCollection.java @@ -0,0 +1,87 @@ +package projectMonorail.Generics; +import projectMonorail.DrawingObjects.*; +import projectMonorail.MovementStrategy.*; + +import java.awt.*; + +public class LocomotivesGenericCollection { + private final int pictureWidth; + + private final int pictureHeight; + + private final int placeSizeWidth = 193; + + private final int placeSizeHeight = 102; + + private final SetGeneric collection; + + public LocomotivesGenericCollection(int picWidth, int picHeight) { + int width = picWidth / placeSizeWidth; + int height = picHeight / placeSizeHeight; + pictureWidth = picWidth; + pictureHeight = picHeight; + collection = new SetGeneric(width * height); + } + + public int addition(T obj) { + if (obj == null) + { + return -1; + } + return collection.insert(obj); + } + + public boolean subtraction(int pos) { + T obj = collection.get(pos); + if (obj != null) + { + return collection.remove(pos); + } + return false; + } + + public U getU(int pos) { + if (collection.get(pos) != null) + return (U)collection.get(pos).getMoveableObject(); + return null; + } + + public void showLocomotives(Graphics2D g2d) { + DrawBackground(g2d); + DrawObjects(g2d); + } + + private void DrawBackground(Graphics2D g2d) { + g2d.setColor(Color.BLACK); + g2d.setStroke(new BasicStroke(3)); + for (int i = 0; i < pictureWidth / placeSizeWidth; i++) { + for (int j = 0; j < pictureHeight / placeSizeHeight + 1; ++j) { + g2d.drawLine(i * placeSizeWidth, j * placeSizeHeight, i * placeSizeWidth + + placeSizeWidth / 2, j * placeSizeHeight); + } + g2d.drawLine( i * placeSizeWidth, 0, i * placeSizeWidth, + pictureHeight / placeSizeHeight * placeSizeHeight); + } + } + + private void DrawObjects(Graphics2D g2d) { + T obj; + int width = pictureWidth / placeSizeWidth; + int height = pictureHeight / placeSizeHeight; + int diff = 1, currWidth = 0; + for (int i = 0; i < collection.count(); i++) { + currWidth++; + if (currWidth > width) { + diff++; + currWidth = 1; + } + obj = collection.get(i); + if (obj != null) { + obj.setPosition(i % width * placeSizeWidth + placeSizeWidth / 40, + (height - diff) * placeSizeHeight + placeSizeHeight / 15); + obj.drawTransport(g2d); + } + } + } + +} diff --git a/src/projectMonorail/Generics/SetGeneric.java b/src/projectMonorail/Generics/SetGeneric.java new file mode 100644 index 0000000..022997e --- /dev/null +++ b/src/projectMonorail/Generics/SetGeneric.java @@ -0,0 +1,53 @@ +package projectMonorail.Generics; + +public class SetGeneric { + private final T[] places; + + public int count() { + return places.length; + } + + public SetGeneric(int count) { + places = (T[])new Object[count]; + } + + public int insert(T locomotive) { + return insert(locomotive, 0); + } + + public int insert(T locomotive, int position) { + int nullIndex = -1, i; + + if (position < 0 || position >= count()) + return -1; + + for (i = position; i < count(); i++) { + if (places[i] == null) { + nullIndex = i; + break; + } + } + if (nullIndex < 0) + return -1; + + for (i = nullIndex; i > position; i--) { + places[i] = places[i - 1]; + } + + places[position] = locomotive; + return position; + } + + public boolean remove(int position) { + if (position < 0 || position >= count()) + return false; + places[position] = null; + return true; + } + + public T get(int position) { + if (position < 0 || position >= count()) + return null; + return places[position]; + } +} diff --git a/src/projectMonorail/Main.java b/src/projectMonorail/Main.java index f23be88..e2633a1 100644 --- a/src/projectMonorail/Main.java +++ b/src/projectMonorail/Main.java @@ -2,7 +2,7 @@ package projectMonorail; public class Main { public static void main(String[] args) { - FrameMonorail frameMonorail = new FrameMonorail(); + FrameMonorailCollection frameMonorailCollection = new FrameMonorailCollection(); } } diff --git a/src/projectMonorail/PictureBox.java b/src/projectMonorail/PictureBox.java index 03473ae..fcbf352 100644 --- a/src/projectMonorail/PictureBox.java +++ b/src/projectMonorail/PictureBox.java @@ -9,11 +9,22 @@ import java.awt.event.ActionListener; import java.util.Random; public class PictureBox extends JPanel { + private boolean dialogResult = false; + + public boolean getDialogResult() { + return dialogResult; + } private DrawingLocomotive drawingLocomotive; private AbstractStrategy abstractStrategy; + private DrawingLocomotive selectedLocomotive; + + public DrawingLocomotive getSelectedLocomotive() { + return selectedLocomotive; + } + private JButton buttonLeft; private JButton buttonUp; @@ -26,6 +37,8 @@ public class PictureBox extends JPanel { private JButton buttonCreateMonorail; + private JButton buttonSelectLocomotive; + private JButton buttonStep; private JComboBox comboBoxStrategy; @@ -41,6 +54,9 @@ public class PictureBox extends JPanel { private JPanel strategyPanel; public PictureBox() { + abstractStrategy = null; + selectedLocomotive = null; + setLayout(new BorderLayout()); buttonsPanel = new JPanel(); @@ -55,11 +71,14 @@ public class PictureBox extends JPanel { buttonCreateLocomotive.addActionListener(e -> { Random random = new Random(); + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color chosenColor = JColorChooser.showDialog(this, "Main color", Color.WHITE); + if (chosenColor != null) { + color = chosenColor; + } drawingLocomotive = new DrawingLocomotive(random.nextInt(200, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), - random.nextInt(0, 256)), - this.getWidth(), this.getHeight(), random.nextInt(2, 5)); + color, this.getWidth(), this.getHeight(), random.nextInt(2, 5)); drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100)); repaint(); }); @@ -72,17 +91,38 @@ public class PictureBox extends JPanel { buttonCreateMonorail.addActionListener(e -> { Random random = new Random(); + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color chosenColor = JColorChooser.showDialog(this, "Main color", Color.WHITE); + if (chosenColor != null) { + color = chosenColor; + } + Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color chosenAddColor = JColorChooser.showDialog(this, "Additional Color", Color.WHITE); + if (chosenAddColor != null) { + additionalColor = chosenAddColor; + } drawingLocomotive = new DrawingMonorail(random.nextInt(200, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), - random.nextInt(0, 256)), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), - random.nextInt(0, 256)), - random.nextBoolean(), random.nextBoolean(), this.getWidth(), this.getHeight(), random.nextInt(2, 5)); + color, additionalColor, random.nextBoolean(), random.nextBoolean(), + this.getWidth(), this.getHeight(), random.nextInt(2, 5)); drawingLocomotive.setPosition(random.nextInt(10, 100), random.nextInt(10, 100)); repaint(); }); + buttonSelectLocomotive = new JButton("Select"); + buttonSelectLocomotive.setFocusable(false); + buttonSelectLocomotive.setFont(new Font("Segoe UI", Font.PLAIN, 12)); + buttonSelectLocomotive.setBackground(Color.LIGHT_GRAY); + buttonSelectLocomotive.setMaximumSize(new Dimension(140, 42)); + + buttonSelectLocomotive.addActionListener(e -> { + selectedLocomotive = drawingLocomotive; + dialogResult = true; + JComponent comp = (JComponent)e.getSource(); + Window win = SwingUtilities.getWindowAncestor(comp); + win.dispose(); + }); + ActionListener buttonMoveListener = e -> { if (drawingLocomotive == null) { return; @@ -182,7 +222,9 @@ public class PictureBox extends JPanel { buttonsPanel.add(buttonCreateMonorail); buttonsPanel.add(Box.createHorizontalStrut(16)); buttonsPanel.add(buttonCreateLocomotive); - buttonsPanel.add(Box.createHorizontalStrut(460)); + buttonsPanel.add(Box.createHorizontalStrut(16)); + buttonsPanel.add(buttonSelectLocomotive); + buttonsPanel.add(Box.createHorizontalStrut(400)); buttonsPanel.add(movePaddingPanel); add(buttonsPanel, BorderLayout.SOUTH); diff --git a/src/projectMonorail/PictureBoxCollection.java b/src/projectMonorail/PictureBoxCollection.java new file mode 100644 index 0000000..9731107 --- /dev/null +++ b/src/projectMonorail/PictureBoxCollection.java @@ -0,0 +1,25 @@ +package projectMonorail; + +import projectMonorail.DrawingObjects.*; +import projectMonorail.Generics.*; +import projectMonorail.MovementStrategy.*; + +import javax.swing.*; +import java.awt.*; + +public class PictureBoxCollection extends JPanel { + private final LocomotivesGenericCollection locomotives; + + public PictureBoxCollection(LocomotivesGenericCollection locomotives, + int width, int height) { + this.locomotives = locomotives; + setMaximumSize(new Dimension(width, height)); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + locomotives.showLocomotives(g2d); + } +} diff --git a/src/projectMonorail/PictureBoxRandomGeneration.java b/src/projectMonorail/PictureBoxRandomGeneration.java new file mode 100644 index 0000000..5ad3605 --- /dev/null +++ b/src/projectMonorail/PictureBoxRandomGeneration.java @@ -0,0 +1,63 @@ +package projectMonorail; + +import projectMonorail.DrawingObjects.*; +import projectMonorail.Entities.*; +import projectMonorail.Extras.*; + +import javax.swing.*; +import java.awt.*; + +public class PictureBoxRandomGeneration extends JPanel { + private RandomGeneric randomGeneric; + + private DrawingLocomotive drawingLocomotive; + + private JButton buttonRandomCreation; + + public PictureBoxRandomGeneration(int width, int height) { + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + + buttonRandomCreation = new JButton("Create monorail"); + buttonRandomCreation.setFocusable(false); + buttonRandomCreation.setMaximumSize(new Dimension(155, 36)); + buttonRandomCreation.setBorder(BorderFactory.createLineBorder(Color.black, 2)); + buttonRandomCreation.setBackground(Color.WHITE); + buttonRandomCreation.setHorizontalTextPosition(JButton.CENTER); + buttonRandomCreation.setVerticalTextPosition(JButton.CENTER); + buttonRandomCreation.setAlignmentX(Component.CENTER_ALIGNMENT); + + + buttonRandomCreation.addActionListener(e -> { + drawingLocomotive = randomGeneric.getDrawingLocomotive(); + repaint(); + }); + + add(Box.createVerticalStrut(height - 80)); + add(buttonRandomCreation); + setPreferredSize(new Dimension(width, height)); + + randomGeneric = new RandomGeneric<>(7, 3, width, height); + randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.RED)); + randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.GREEN)); + randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.BLUE)); + randomGeneric.addItem(new EntityLocomotive(200, 2000, Color.PINK)); + randomGeneric.addItem(new EntityMonorail(200, 2000, Color.WHITE, Color.BLUE,true, false)); + randomGeneric.addItem(new EntityMonorail(200, 2000, Color.ORANGE, Color.LIGHT_GRAY,false, true)); + randomGeneric.addItem(new EntityMonorail(200, 2000, Color.CYAN, Color.MAGENTA,true, true)); + randomGeneric.addItem(new DrawingNormalWheels()); + randomGeneric.addItem(new DrawingVintageWheels()); + randomGeneric.addItem(new DrawingSquarePatternWheels()); + } + + @Override + protected void paintComponent(Graphics g) { + if (drawingLocomotive == null) { + return; + } + + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + drawingLocomotive.setPosition(getWidth() * 27 / 100, getHeight() * 3 / 10); + drawingLocomotive.drawTransport(g2d); + } +}