From 6e69f8b10087d7b089c9bf1932a4efb81c51a4f7 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Tue, 15 Nov 2022 17:10:04 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=A1=D0=B2=D0=B5=D1=80=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=20=D1=84=D0=BE=D1=80=D0=BC=D1=83=20=D1=81=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D0=B0=D0=BC=D0=B8,=20=D1=83?= =?UTF-8?q?=D0=BA=D1=80=D0=B0=D1=88=D0=B0=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormLocomotive.java | 2 +- FormLocomotiveConfig.form | 337 ++++++++++++++++++++++++++++++++++++++ FormLocomotiveConfig.java | 61 +++++++ 3 files changed, 399 insertions(+), 1 deletion(-) create mode 100644 FormLocomotiveConfig.form create mode 100644 FormLocomotiveConfig.java diff --git a/FormLocomotive.java b/FormLocomotive.java index 53a92cc..65ce9c5 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -107,6 +107,6 @@ public class FormLocomotive extends JComponent{ super.repaint(); } public static void main(String[] args) { - new FormMapWithSetLocomotives(); + new FormLocomotiveConfig(); } } diff --git a/FormLocomotiveConfig.form b/FormLocomotiveConfig.form new file mode 100644 index 0000000..5a7c514 --- /dev/null +++ b/FormLocomotiveConfig.form @@ -0,0 +1,337 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormLocomotiveConfig.java b/FormLocomotiveConfig.java new file mode 100644 index 0000000..d7b351d --- /dev/null +++ b/FormLocomotiveConfig.java @@ -0,0 +1,61 @@ +import javax.swing.*; +import java.awt.*; + +public class FormLocomotiveConfig extends JFrame{ + private JPanel FormPanel; + private JPanel ParametersPanel; + private JPanel CreatePanel; + private JPanel ColorPanel; + private JPanel StatsPanel; + private JLabel SpeedLabel; + private JSpinner SpeedSpinner; + private JLabel WeightLabel; + private JSpinner WeightSpinner; + private JCheckBox PipeCheckBox; + private JCheckBox FuelCheckBox; + private JLabel SimpleObjectLabel; + private JLabel AdvancedObjectLabel; + private JLabel SimpleWheelsLabel; + private JLabel StarWheelsLabel; + private JLabel RoundWheelsLabel; + private JSpinner WheelsCountSpinner; + private JLabel WheelsCountLabel; + private JPanel RedColorPanel; + private JPanel GreenColorPanel; + private JPanel BlueColorPanel; + private JPanel YellowColorPanel; + private JPanel WhiteColorPanel; + private JPanel BlackColorPanel; + private JPanel AquaColorPanel; + private JPanel PurpleColorPanel; + private JLabel MainColorLabel; + private JLabel ExtraColorLabel; + private JPanel ObjectViewPanel; + private JButton AddButton; + private JButton CancelButton; + + public FormLocomotiveConfig() { + + // Добавили цветные границы + SimpleObjectLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + AdvancedObjectLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + SimpleWheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + RoundWheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + StarWheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + MainColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + ExtraColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + + + + + + + setContentPane(FormPanel); + setSize(800, 500); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setVisible(true); + } + + + +} -- 2.25.1 From 7e8bac26f389b27d674b11fff50b1cd9c38e28d9 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Tue, 15 Nov 2022 20:22:49 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A5=D0=B0=D1=80=D0=B4=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B1=205=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0,=20=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=BF=D0=BE=D1=87?= =?UTF-8?q?=D0=B8=D1=81=D1=82=D0=B8=D1=82=D1=8C=20=D0=BA=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=B8=D1=82=D1=8C?= =?UTF-8?q?=20=D0=B1=D0=B0=D0=B3=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 15 ++++-- DrawningWarmlyLocomotive.java | 11 +++- EntityLocomotive.java | 3 ++ EntityWarmlyLocomotive.java | 6 ++- EventListener.java | 15 ++++++ ExtraRoundWheelDraw.java | 7 ++- ExtraStarWheelDraw.java | 6 +++ ExtraWheelsDraw.java | 15 +++--- FormLocomotive.java | 6 ++- FormLocomotiveConfig.java | 97 ++++++++++++++++++++++++++++++++++ FormMap.java | 3 +- FormMapWithSetLocomotives.java | 23 +++++++- IDrawningExtra.java | 1 + 13 files changed, 188 insertions(+), 20 deletions(-) create mode 100644 EventListener.java diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 3293091..e0271cc 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -18,9 +18,9 @@ public class DrawningLocomotive { private int _locomotiveHeight = 50; /// Инициализация свойств private final Random random = new Random(); - public DrawningLocomotive(int speed, float weight, Color bodyColor) + public DrawningLocomotive(int speed, float weight, Color bodyColor, int wheelsCount) { - int randExtra = random.nextInt(2); + /*int randExtra = random.nextInt(2); switch (random.nextInt(3)){ case 0: drawningExtra = new ExtraStarWheelDraw(randExtra, bodyColor); @@ -31,7 +31,8 @@ public class DrawningLocomotive { case 2: drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor); break; - } + }*/ + drawningExtra = new ExtraWheelsDraw(wheelsCount, bodyColor); Locomotive = new EntityLocomotive(speed, weight, bodyColor); } @@ -41,13 +42,17 @@ public class DrawningLocomotive { } // Новый конструктор - protected DrawningLocomotive (int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) + protected DrawningLocomotive (int speed, float weight, Color bodyColor, int wheelsCount, int locomotiveWidth, int locomotiveHeight) { - this(speed, weight, bodyColor); + this(speed, weight, bodyColor, wheelsCount); _locomotiveWidth = locomotiveWidth; _locomotiveHeight = locomotiveHeight; } + public void SetColor(Color color) { + Locomotive.SetColor(color); + } + /// Установка позиции локомотива public void SetPosition(int x, int y, int width, int height) { diff --git a/DrawningWarmlyLocomotive.java b/DrawningWarmlyLocomotive.java index 4019911..ca7fcf4 100644 --- a/DrawningWarmlyLocomotive.java +++ b/DrawningWarmlyLocomotive.java @@ -1,8 +1,8 @@ import java.awt.*; public class DrawningWarmlyLocomotive extends DrawningLocomotive{ - public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean storage) + public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, int wheelsCount, Color extraColor, boolean pipe, boolean storage) { - super(speed, weight, bodyColor, 140, 70); + super(speed, weight, bodyColor,wheelsCount, 140, 70); Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); } @@ -10,6 +10,13 @@ public class DrawningWarmlyLocomotive extends DrawningLocomotive{ super(locomotive, extra); Locomotive = locomotive; } + @Override + public void SetColor(Color color) { + ((EntityWarmlyLocomotive) Locomotive).SetColor(color); + } + public void SetExtraColor(Color color) { + ((EntityWarmlyLocomotive) Locomotive).SetExtraColor(color); + } @Override public void DrawTransport(Graphics2D g) diff --git a/EntityLocomotive.java b/EntityLocomotive.java index a4d15d7..2dd722c 100644 --- a/EntityLocomotive.java +++ b/EntityLocomotive.java @@ -14,6 +14,9 @@ public class EntityLocomotive { public Color getBodyColor() { return BodyColor; } + public void SetColor(Color color) { + BodyColor = color; + } public float Step () { return Speed * 100 / Weight; diff --git a/EntityWarmlyLocomotive.java b/EntityWarmlyLocomotive.java index 2e1a502..5e93729 100644 --- a/EntityWarmlyLocomotive.java +++ b/EntityWarmlyLocomotive.java @@ -1,7 +1,7 @@ import java.awt.*; public class EntityWarmlyLocomotive extends EntityLocomotive{ - public final Color ExtraColor; + public Color ExtraColor; public final boolean Pipe; public final boolean FuelStorage; public EntityWarmlyLocomotive (int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean fuelStorage) @@ -12,4 +12,8 @@ public class EntityWarmlyLocomotive extends EntityLocomotive{ FuelStorage = fuelStorage; } + public void SetExtraColor(Color color) { + ExtraColor = color; + } + } diff --git a/EventListener.java b/EventListener.java new file mode 100644 index 0000000..f9cb7bc --- /dev/null +++ b/EventListener.java @@ -0,0 +1,15 @@ +import java.util.ArrayList; +import java.util.function.Consumer; + +public class EventListener { + private ArrayList> listeners = new ArrayList<>(); + public void Add(Consumer listener) { + listeners.add(listener); + } + + public void Emit(T artillery) { + for (var listener : listeners) { + listener.accept(artillery); + } + } +} diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java index e14642b..669ef3c 100644 --- a/ExtraRoundWheelDraw.java +++ b/ExtraRoundWheelDraw.java @@ -3,6 +3,7 @@ import java.awt.*; public class ExtraRoundWheelDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; private ExtraWheelsDraw extraWheelsDraw; + private Color color; public void setExtraNum(int num) { switch (num) { case 0: { @@ -23,9 +24,13 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{ extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); } + public void SetColor(Color color) { + this.color = color; + } + public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { extraWheelsDraw.DrawExtra(startPosX, startPosY, g); - g.setColor(Color.BLACK); + g.setColor(color); g.fillOval(startPosX + 5, startPosY + 35, 10, 10); g.fillOval(startPosX + 95, startPosY + 35, 10, 10); switch (wheelsCount) { diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java index 3b415d6..17a8b33 100644 --- a/ExtraStarWheelDraw.java +++ b/ExtraStarWheelDraw.java @@ -3,6 +3,7 @@ import java.awt.*; public class ExtraStarWheelDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; private ExtraWheelsDraw extraWheelsDraw; + private Color color; public void setExtraNum(int num) { switch (num) { case 0: { @@ -23,6 +24,10 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); } + public void SetColor(Color color) { + this.color = color; + } + public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { extraWheelsDraw.DrawExtra(startPosX, startPosY, g); DrawStarOnWheel(startPosX, startPosY + 30, g); @@ -41,6 +46,7 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ } private void DrawStarOnWheel(int startPosX, int startPosY, Graphics2D g) { + g.setColor(color); g.drawLine(startPosX + 10, startPosY, startPosX + 15, startPosY + 17); g.drawLine(startPosX + 10, startPosY, startPosX + 5, startPosY + 17); g.drawLine(startPosX + 15, startPosY + 17, startPosX + 2, startPosY + 8); diff --git a/ExtraWheelsDraw.java b/ExtraWheelsDraw.java index a515643..eb37d42 100644 --- a/ExtraWheelsDraw.java +++ b/ExtraWheelsDraw.java @@ -4,11 +4,11 @@ public class ExtraWheelsDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; public void setExtraNum(int num) { switch (num) { - case 0: { + case 3: { wheelsCount = WheelsCount.Three; break; } - case 1: { + case 4: { wheelsCount = WheelsCount.Four; break; } @@ -17,6 +17,9 @@ public class ExtraWheelsDraw implements IDrawningExtra{ } } private Color color; + public void SetColor(Color color) { + this.color = color; + } public ExtraWheelsDraw(int num, Color color) { setExtraNum(num); @@ -27,19 +30,19 @@ public class ExtraWheelsDraw implements IDrawningExtra{ g.setColor(Color.BLACK); g.drawOval(startPosX, startPosY + 30, 20, 20); g.drawOval(startPosX + 90, startPosY + 30, 20, 20); - g.setColor(color); + g.setColor(Color.BLACK); g.fillOval(startPosX, startPosY + 30, 20, 20); g.fillOval(startPosX + 90, startPosY + 30, 20, 20); switch (wheelsCount) { case Four: { - g.setColor(Color.BLACK); - g.drawOval(startPosX + 70, startPosY + 30, 20, 20); g.setColor(color); + g.drawOval(startPosX + 70, startPosY + 30, 20, 20); + g.setColor(Color.BLACK); g.fillOval(startPosX + 70, startPosY + 30, 20, 20); } case Three: { g.fillOval(startPosX + 20, startPosY + 30, 20, 20); - g.setColor(Color.BLACK); + g.setColor(color); g.drawOval(startPosX + 20, startPosY + 30, 20, 20); break; } diff --git a/FormLocomotive.java b/FormLocomotive.java index 65ce9c5..57fd25a 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -30,7 +30,7 @@ public class FormLocomotive extends JComponent{ Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); - _locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst); + _locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst, rnd.nextInt(2) + 2); _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-75); speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); @@ -47,6 +47,7 @@ public class FormLocomotive extends JComponent{ _locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst, + rnd.nextInt(2) + 2, colorSecond, rnd.nextBoolean(), rnd.nextBoolean()); @@ -107,6 +108,7 @@ public class FormLocomotive extends JComponent{ super.repaint(); } public static void main(String[] args) { - new FormLocomotiveConfig(); + + new FormMapWithSetLocomotives(); } } diff --git a/FormLocomotiveConfig.java b/FormLocomotiveConfig.java index d7b351d..c2cdead 100644 --- a/FormLocomotiveConfig.java +++ b/FormLocomotiveConfig.java @@ -1,7 +1,15 @@ import javax.swing.*; import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.function.Consumer; public class FormLocomotiveConfig extends JFrame{ + // Рабочие поля + private DrawningLocomotive locomotive; + private EventListener eventListener = new EventListener<>(); + + //Элементы формы private JPanel FormPanel; private JPanel ParametersPanel; private JPanel CreatePanel; @@ -45,17 +53,106 @@ public class FormLocomotiveConfig extends JFrame{ MainColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); ExtraColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + // Модели для намериков + SpeedSpinner.setModel(new SpinnerNumberModel(100, 50, 1000, 10)); + WeightSpinner.setModel(new SpinnerNumberModel(1000, 1000, 5000, 10)); + WheelsCountSpinner.setModel(new SpinnerNumberModel(2, 2, 4, 1)); + //Обработчик d&d + var DragDropAdapter = new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + super.mouseReleased(e); + } + @Override + public void mouseReleased(MouseEvent e) { + super.mouseReleased(e); + Drop((JComponent) e.getSource()); + } + }; + SimpleObjectLabel.addMouseListener(DragDropAdapter); + AdvancedObjectLabel.addMouseListener(DragDropAdapter); + StarWheelsLabel.addMouseListener(DragDropAdapter); + RoundWheelsLabel.addMouseListener(DragDropAdapter); + StarWheelsLabel.addMouseListener(DragDropAdapter); + RedColorPanel.addMouseListener(DragDropAdapter); + GreenColorPanel.addMouseListener(DragDropAdapter); + BlueColorPanel.addMouseListener(DragDropAdapter); + YellowColorPanel.addMouseListener(DragDropAdapter); + WhiteColorPanel.addMouseListener(DragDropAdapter); + BlackColorPanel.addMouseListener(DragDropAdapter); + AquaColorPanel.addMouseListener(DragDropAdapter); + PurpleColorPanel.addMouseListener(DragDropAdapter); + AddButton.addActionListener(e -> { + eventListener.Emit(locomotive); + dispose(); + }); + CancelButton.addActionListener(e -> dispose()); + // Параметры фрейма setContentPane(FormPanel); setSize(800, 500); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } + // Drop обработка + public void Drop (JComponent component) { + if (component == null) { + return; + } + if (component instanceof JPanel panel) { + if (MainColorLabel.getMousePosition() != null) { + locomotive.SetColor(panel.getBackground()); + locomotive.drawningExtra.SetColor(panel.getBackground()); + } + if (ExtraColorLabel.getMousePosition() != null && locomotive instanceof DrawningWarmlyLocomotive warmlyLocomotive) { + warmlyLocomotive.SetExtraColor(panel.getBackground()); + } + } + if (component instanceof JLabel label && ObjectViewPanel.getMousePosition() != null) { + int speed = (Integer) SpeedSpinner.getValue(); + int weight = (Integer) WeightSpinner.getValue(); + int wheelsCount = (Integer) WheelsCountSpinner.getValue(); + boolean pipe = PipeCheckBox.isSelected(); + boolean fuel = FuelCheckBox.isSelected(); + + if (label == SimpleObjectLabel) { + locomotive = new DrawningLocomotive(speed, weight, Color.WHITE, wheelsCount); + } else if (label == AdvancedObjectLabel) { + locomotive = new DrawningWarmlyLocomotive(speed, weight, Color.WHITE, wheelsCount, Color.WHITE, pipe, fuel); + } else if (locomotive != null && label == SimpleWheelsLabel) { + locomotive.drawningExtra = new ExtraWheelsDraw(wheelsCount, locomotive.Locomotive.getBodyColor()); + locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor()); + } else if (locomotive != null && label == StarWheelsLabel) { + locomotive.drawningExtra = new ExtraStarWheelDraw(wheelsCount, locomotive.Locomotive.getBodyColor()); + locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor()); + } else if (locomotive != null && label == RoundWheelsLabel) { + locomotive.drawningExtra = new ExtraRoundWheelDraw(wheelsCount, locomotive.Locomotive.getBodyColor()); + locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor()); + } + } + + repaint(); + } + + public void AddListener(Consumer listener) { + eventListener.Add(listener); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + if (locomotive != null) { + g = ObjectViewPanel.getGraphics(); + locomotive.SetPosition(20, 20, ObjectViewPanel.getWidth(), ObjectViewPanel.getHeight()); + locomotive.DrawTransport((Graphics2D) g); + } + } + } diff --git a/FormMap.java b/FormMap.java index 4b0064d..3087aa3 100644 --- a/FormMap.java +++ b/FormMap.java @@ -56,7 +56,7 @@ public class FormMap extends JComponent { JButton createButton = new JButton("Create"); createButton.addActionListener(e -> { Random rnd = new Random(); - var locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + var locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextInt(2) + 2); speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue()); @@ -69,6 +69,7 @@ public class FormMap extends JComponent { Random rnd = new Random(); var locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextInt(2) + 2, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextBoolean(), rnd.nextBoolean()); diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index f091664..fed8467 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -113,7 +113,7 @@ public class FormMapWithSetLocomotives extends JComponent { // Кнопка добавления локомотива JButton addLocomotiveButton = new JButton("Add Locomotive"); addLocomotiveButton.addActionListener(e -> { - // логика добавления + /*// логика добавления if (listBoxMaps.getSelectedIndex() == -1) { return; @@ -132,7 +132,26 @@ public class FormMapWithSetLocomotives extends JComponent { } else { JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION); - } + }*/ + + FormLocomotiveConfig formLocomotiveConfig = new FormLocomotiveConfig(); + formLocomotiveConfig.setVisible(true); + formLocomotiveConfig.AddListener(locomotive -> { + if (listBoxMaps.getSelectedIndex() == -1) { + return; + } + if (locomotive!=null) { + DrawningObjectLocomotive objectLocomotive = new DrawningObjectLocomotive(locomotive); + if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){ + JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION); + bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet(); + repaint(); + } + else { + JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION); + } + } + }); }); statusPanel.add(addLocomotiveButton); diff --git a/IDrawningExtra.java b/IDrawningExtra.java index 725e87a..fc9cf41 100644 --- a/IDrawningExtra.java +++ b/IDrawningExtra.java @@ -3,4 +3,5 @@ import java.awt.*; public interface IDrawningExtra { void setExtraNum(int num); void DrawExtra(int startPosX, int startPosY, Graphics2D g); + void SetColor(Color color); } -- 2.25.1 From 64a31c1a76f082779e71996928541df3a9d6c138 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 18 Nov 2022 21:18:41 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=A5=D0=B0=D1=80=D0=B4=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B1=205=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0,=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=87=D0=B8=D1=89=D0=B5=D0=BD=20=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 12 --- FormLocomotiveConfig.java | 2 +- FormMap.java | 130 --------------------------------- FormMapWithSetLocomotives.java | 21 ------ 4 files changed, 1 insertion(+), 164 deletions(-) delete mode 100644 FormMap.java diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index e0271cc..015f2ef 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -20,18 +20,6 @@ public class DrawningLocomotive { private final Random random = new Random(); public DrawningLocomotive(int speed, float weight, Color bodyColor, int wheelsCount) { - /*int randExtra = random.nextInt(2); - switch (random.nextInt(3)){ - case 0: - drawningExtra = new ExtraStarWheelDraw(randExtra, bodyColor); - break; - case 1: - drawningExtra = new ExtraRoundWheelDraw(randExtra, bodyColor); - break; - case 2: - drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor); - break; - }*/ drawningExtra = new ExtraWheelsDraw(wheelsCount, bodyColor); Locomotive = new EntityLocomotive(speed, weight, bodyColor); } diff --git a/FormLocomotiveConfig.java b/FormLocomotiveConfig.java index c2cdead..de89c28 100644 --- a/FormLocomotiveConfig.java +++ b/FormLocomotiveConfig.java @@ -7,7 +7,7 @@ import java.util.function.Consumer; public class FormLocomotiveConfig extends JFrame{ // Рабочие поля private DrawningLocomotive locomotive; - private EventListener eventListener = new EventListener<>(); + private final EventListener eventListener = new EventListener<>(); //Элементы формы private JPanel FormPanel; diff --git a/FormMap.java b/FormMap.java deleted file mode 100644 index 3087aa3..0000000 --- a/FormMap.java +++ /dev/null @@ -1,130 +0,0 @@ -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 FormMap extends JComponent { - - private AbstractMap _abstractMap; - private BufferedImage bufferImg = null; - - public FormMap() { - JFrame formFrame = new JFrame("Form Map"); - formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - formFrame.setSize(1000, 500); - formFrame.setVisible(true); - formFrame.setLocationRelativeTo(null); - - Panel statusPanel = new Panel(); - statusPanel.setBackground(Color.WHITE); - statusPanel.setLayout(new FlowLayout()); - setLayout(new BorderLayout()); - add(statusPanel, BorderLayout.SOUTH); - - Label speedLabel = new Label("Speed: "); - Label weightLabel = new Label("Weight: "); - Label colorLabel = new Label("Color: "); - - String[] maps = { - "Simple Map", - "Spike Map", - "Rail Map" - }; - JComboBox mapSelectComboBox = new JComboBox(maps); - mapSelectComboBox.setEditable(true); - mapSelectComboBox.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String item = (String)mapSelectComboBox.getSelectedItem(); - if (item == null) return; - switch (item) { - case ("Simple Map"): - _abstractMap = new SimpleMap(); - break; - case ("Spike Map"): - _abstractMap = new SpikeMap(); - break; - case ("Rail Map"): - _abstractMap = new RailMap(); - break; - } - } - }); - - JButton createButton = new JButton("Create"); - createButton.addActionListener(e -> { - Random rnd = new Random(); - var locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextInt(2) + 2); - speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); - weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); - colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue()); - if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawningObjectLocomotive(locomotive)); - repaint(); - }); - - JButton modifiedButton = new JButton("Modified"); - modifiedButton.addActionListener(e -> { - Random rnd = new Random(); - var locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - rnd.nextInt(2) + 2, - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - rnd.nextBoolean(), - rnd.nextBoolean()); - speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); - weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); - colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue() ); - if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawningObjectLocomotive(locomotive)); - repaint(); - }); - statusPanel.add(mapSelectComboBox); - statusPanel.add(createButton); - statusPanel.add(modifiedButton); - statusPanel.add(speedLabel); - statusPanel.add(weightLabel); - statusPanel.add(colorLabel); - - JButton moveDownButton = new JButton("Down"); - moveDownButton.addActionListener(e -> { - if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Down); - repaint(); - }); - - JButton moveUpButton = new JButton("Up"); - moveUpButton.addActionListener(e -> { - if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Up); - repaint(); - }); - - JButton moveLeftButton = new JButton("Left"); - moveLeftButton.addActionListener(e -> { - if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Left); - repaint(); - }); - - JButton moveRightButton = new JButton("Right"); - moveRightButton.addActionListener(e -> { - if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Right); - repaint(); - }); - - statusPanel.add(moveUpButton); - statusPanel.add(moveDownButton); - statusPanel.add(moveLeftButton); - statusPanel.add(moveRightButton); - - formFrame.getContentPane().add(this); - super.repaint(); - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - Graphics2D g2 = (Graphics2D)g; - if (bufferImg != null) g2.drawImage(bufferImg, 0,0,1000,490,null); - super.repaint(); - } - -} diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index fed8467..ce8dfab 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -113,27 +113,6 @@ public class FormMapWithSetLocomotives extends JComponent { // Кнопка добавления локомотива JButton addLocomotiveButton = new JButton("Add Locomotive"); addLocomotiveButton.addActionListener(e -> { - /*// логика добавления - if (listBoxMaps.getSelectedIndex() == -1) - { - return; - } - JDialog dialog = new JDialog(formFrame, "Dialog", true); - FormLocomotive formLocomotive = new FormLocomotive(dialog); - dialog.setSize(800, 500); - dialog.setContentPane(formLocomotive); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - dialog.setVisible(true); - DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive()); - if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(locomotive)!= -1) { - JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION); - bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet(); - repaint(); - } - else { - JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION); - }*/ - FormLocomotiveConfig formLocomotiveConfig = new FormLocomotiveConfig(); formLocomotiveConfig.setVisible(true); formLocomotiveConfig.AddListener(locomotive -> { -- 2.25.1 From aa76d75acfd3a807f2948b498f59fa7b8ab3fde0 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 19:47:16 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=B1=D0=B0=D0=B3=20=D1=84=D0=B8=D0=BA?= =?UTF-8?q?=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ExtraRoundWheelDraw.java | 4 ++-- ExtraStarWheelDraw.java | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java index 669ef3c..0f7e917 100644 --- a/ExtraRoundWheelDraw.java +++ b/ExtraRoundWheelDraw.java @@ -6,11 +6,11 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{ private Color color; public void setExtraNum(int num) { switch (num) { - case 0: { + case 3: { wheelsCount = WheelsCount.Three; break; } - case 1: { + case 4: { wheelsCount = WheelsCount.Four; break; } diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java index 17a8b33..a89b286 100644 --- a/ExtraStarWheelDraw.java +++ b/ExtraStarWheelDraw.java @@ -6,11 +6,11 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ private Color color; public void setExtraNum(int num) { switch (num) { - case 0: { + case 3: { wheelsCount = WheelsCount.Three; break; } - case 1: { + case 4: { wheelsCount = WheelsCount.Four; break; } @@ -29,6 +29,7 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ } public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + g.setColor(color); extraWheelsDraw.DrawExtra(startPosX, startPosY, g); DrawStarOnWheel(startPosX, startPosY + 30, g); DrawStarOnWheel(startPosX + 90, startPosY + 30, g); -- 2.25.1