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); }