diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 3293091..015f2ef 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -18,20 +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); - 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); } @@ -41,13 +30,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..0f7e917 100644 --- a/ExtraRoundWheelDraw.java +++ b/ExtraRoundWheelDraw.java @@ -3,13 +3,14 @@ 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: { + case 3: { wheelsCount = WheelsCount.Three; break; } - case 1: { + case 4: { wheelsCount = WheelsCount.Four; break; } @@ -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..a89b286 100644 --- a/ExtraStarWheelDraw.java +++ b/ExtraStarWheelDraw.java @@ -3,13 +3,14 @@ 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: { + case 3: { wheelsCount = WheelsCount.Three; break; } - case 1: { + case 4: { wheelsCount = WheelsCount.Four; break; } @@ -23,7 +24,12 @@ 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) { + g.setColor(color); extraWheelsDraw.DrawExtra(startPosX, startPosY, g); DrawStarOnWheel(startPosX, startPosY + 30, g); DrawStarOnWheel(startPosX + 90, startPosY + 30, g); @@ -41,6 +47,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 53a92cc..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 FormMapWithSetLocomotives(); } } 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..de89c28 --- /dev/null +++ b/FormLocomotiveConfig.java @@ -0,0 +1,158 @@ +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 final EventListener eventListener = new EventListener<>(); + + //Элементы формы + 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)); + + // Модели для намериков + 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 deleted file mode 100644 index 4b0064d..0000000 --- a/FormMap.java +++ /dev/null @@ -1,129 +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))); - 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)), - 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 f091664..ce8dfab 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -113,26 +113,24 @@ 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 -> { + 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); }