diff --git a/src/Action.java b/src/Action.java new file mode 100644 index 0000000..bc795ea --- /dev/null +++ b/src/Action.java @@ -0,0 +1,16 @@ +import java.util.ArrayList; +import java.util.function.Consumer; + +public class Action { + private final ArrayList> listeners = new ArrayList<>(); + + public void addListener(Consumer listener) { + listeners.add(listener); + } + + public void emit(T gasolineTanker) { + for (var listener : listeners) { + listener.accept(gasolineTanker); + } + } +} diff --git a/src/DrawingGasolineTanker.java b/src/DrawingGasolineTanker.java index 939c0da..de22b7c 100644 --- a/src/DrawingGasolineTanker.java +++ b/src/DrawingGasolineTanker.java @@ -25,11 +25,24 @@ public class DrawingGasolineTanker { GasolineTanker = new EntityGasolineTanker(speed, weight, bodyColor); Wheels= wheelsForm; } + public DrawingGasolineTanker(int speed, float weight,int countwheel, Color bodyColor) + { + GasolineTanker = new EntityGasolineTanker(speed, weight, bodyColor); + Wheels= GetFormOfWheels((int)(2 + Math.random() + Math.random()*2)); + Wheels.SetCountWheels(countwheel); + } public DrawingGasolineTanker(EntityGasolineTanker gasolineTanker,IDrawningObjectWheels wheels) { GasolineTanker = gasolineTanker; Wheels = wheels; } + public void SetWheels(IDrawningObjectWheels wheels){ + Wheels = wheels; + } + + public void SetBodyColor(Color color){ + GasolineTanker= new EntityGasolineTanker(GasolineTanker.getSpeed(), GasolineTanker.getWeight(), color); + } public IDrawningObjectWheels GetFormOfWheels(int FormOfWheel){ OrnamentForm temp = null; diff --git a/src/DrawingImprovedGasolineTanker.java b/src/DrawingImprovedGasolineTanker.java index daf5c28..8a31588 100644 --- a/src/DrawingImprovedGasolineTanker.java +++ b/src/DrawingImprovedGasolineTanker.java @@ -8,6 +8,11 @@ public class DrawingImprovedGasolineTanker extends DrawingGasolineTanker { Wheels= GetFormOfWheels(wheelForm); Wheels.SetCountWheels((int)(2 + Math.random() + Math.random()*2)); } + public DrawingImprovedGasolineTanker(int speed, float weight,int countwheels, Color bodyColor, Color dopColor, boolean bodyKit,boolean antena) { + super(speed, weight, bodyColor, 120, 50); + GasolineTanker=new EntityImprovedGasolineTanker(speed,weight,bodyColor,dopColor,bodyKit,antena); + Wheels.SetCountWheels(countwheels); + } public DrawingImprovedGasolineTanker(int speed, float weight, Color bodyColor, Color dopColor, boolean bodyKit, boolean antena,IDrawningObjectWheels wheelForm) { super(speed, weight, bodyColor, 160, 70); GasolineTanker=new EntityImprovedGasolineTanker(speed,weight,bodyColor,dopColor,bodyKit,antena); @@ -17,8 +22,15 @@ public class DrawingImprovedGasolineTanker extends DrawingGasolineTanker { super(gasolineTanker,wheels); GasolineTanker=gasolineTanker; } - - + @Override + public void SetBodyColor(Color color){ + var gasolineTanker=(EntityImprovedGasolineTanker) GasolineTanker; + GasolineTanker=new EntityImprovedGasolineTanker(gasolineTanker.getSpeed(),gasolineTanker.getWeight(),color,gasolineTanker.GetDopColor(), gasolineTanker.GetBodyKit(), gasolineTanker.GetAntena()); + } + public void SetDopColor(Color color){ + var gasolineTanker=(EntityImprovedGasolineTanker) GasolineTanker; + GasolineTanker=new EntityImprovedGasolineTanker(gasolineTanker.getSpeed(),gasolineTanker.getWeight(),gasolineTanker.getBodyColor(),color, gasolineTanker.GetBodyKit(), gasolineTanker.GetAntena()); + } @Override public void DrawTransport(Graphics g) { diff --git a/src/DrawingOrnamentWheelsFirst.java b/src/DrawingOrnamentWheelsFirst.java index e818a5e..cec084f 100644 --- a/src/DrawingOrnamentWheelsFirst.java +++ b/src/DrawingOrnamentWheelsFirst.java @@ -6,6 +6,9 @@ public class DrawingOrnamentWheelsFirst implements IDrawningObjectWheels{ public DrawingOrnamentWheelsFirst(CountWheels wheels){ _wheels=wheels; } + public DrawingOrnamentWheelsFirst(int count) { + SetCountWheels(count); + } @Override public void SetCountWheels(int count) { for (CountWheels temp: CountWheels.values()) diff --git a/src/DrawingOrnamentWheelsSecond.java b/src/DrawingOrnamentWheelsSecond.java index cf2ff8d..e38d3de 100644 --- a/src/DrawingOrnamentWheelsSecond.java +++ b/src/DrawingOrnamentWheelsSecond.java @@ -6,6 +6,9 @@ public class DrawingOrnamentWheelsSecond implements IDrawningObjectWheels{ public DrawingOrnamentWheelsSecond(CountWheels wheels){ _wheels=wheels; } + public DrawingOrnamentWheelsSecond(int count) { + SetCountWheels(count); + } @Override public void SetCountWheels(int count) { for (CountWheels temp: CountWheels.values()) diff --git a/src/DrawingWheels.java b/src/DrawingWheels.java index e50d1ca..e9f272c 100644 --- a/src/DrawingWheels.java +++ b/src/DrawingWheels.java @@ -7,6 +7,9 @@ public class DrawingWheels implements IDrawningObjectWheels{ public DrawingWheels(CountWheels wheels) { _wheels=wheels; } + public DrawingWheels(int count) { + SetCountWheels(count); + } public void SetCountWheels(int Count){ for (CountWheels temp: CountWheels.values()) diff --git a/src/FormGasolineTanker.java b/src/FormGasolineTanker.java index aae2092..eaed5d9 100644 --- a/src/FormGasolineTanker.java +++ b/src/FormGasolineTanker.java @@ -42,9 +42,9 @@ public class FormGasolineTanker extends JFrame{ private void SetData() { Random rand=new Random(); _gasolineTanker.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); - SpeedLabel.setText("Speed: "+_gasolineTanker.getGasolineTanker().getSpeed()); - WeightLabel.setText("Weight: "+_gasolineTanker.getGasolineTanker().getWeight()); - BodyColorLabel.setText("Color: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2)); + SpeedLabel.setText("Скорость: "+_gasolineTanker.getGasolineTanker().getSpeed()); + WeightLabel.setText("Вес: "+_gasolineTanker.getGasolineTanker().getWeight()); + BodyColorLabel.setText("Цвет: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2)); } @Override diff --git a/src/FormGasolineTankerConfig.form b/src/FormGasolineTankerConfig.form new file mode 100644 index 0000000..31ecf96 --- /dev/null +++ b/src/FormGasolineTankerConfig.form @@ -0,0 +1,296 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormGasolineTankerConfig.java b/src/FormGasolineTankerConfig.java new file mode 100644 index 0000000..6cbd550 --- /dev/null +++ b/src/FormGasolineTankerConfig.java @@ -0,0 +1,153 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.FocusEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowEvent; +import java.util.function.Consumer; + +public class FormGasolineTankerConfig extends JFrame{ + private final Action event = new Action<>(); + private DrawingGasolineTanker gasolineTanker; + CountWheels wheels; + private IDrawningObjectWheels _wheel=null; + private JPanel MainPanel; + private JPanel PreviewPanel; + private JButton ButtonAdd; + private JButton ButtonCancel; + private JLabel BodyColorLabel; + private JLabel DopColorLabel; + private JPanel PictureBox; + private JPanel GroupBoxConfig; + private JSpinner SpinnerWheels; + private JLabel CountWheelsLabel; + private JPanel TypeWheels; + private JLabel WheelsLabel; + private JLabel WheelsFirstLabel; + private JLabel WheelsSecondLabel; + private JPanel ColorPanel; + private JSpinner SpinnerSpeed; + private JCheckBox CheckBoxAntenna; + private JCheckBox checkBoxBodyKit; + private JSpinner SpinnerWeight; + private JPanel RedPanel; + private JPanel OrangePanel; + private JPanel YellowPanel; + private JPanel CianPanel; + private JPanel PinkPanel; + private JPanel GreenPanel; + private JPanel BluePanel; + private JPanel GrayPanel; + private JLabel BaseGasolineTankerLabel; + private JLabel ImprovedGasolineTankerLabel; + private JLabel SpeedLabel; + private JLabel WeightLabel; + + public FormGasolineTankerConfig(){ + this.setTitle("Create object"); + this.setSize(900,500); + this.setContentPane(MainPanel); + this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); + + WheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + WheelsFirstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + WheelsFirstLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + BodyColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + DopColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + BaseGasolineTankerLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + ImprovedGasolineTankerLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + + SpinnerWheels.setModel(new SpinnerNumberModel(2, 2, 4, 1)); + SpinnerWeight.setModel(new SpinnerNumberModel(100, 100, 1000, 10)); + SpinnerSpeed.setModel(new SpinnerNumberModel(10000, 10000, 20000, 100)); + + var dragAdapter = new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + super.mouseReleased(e); + setCursor(new Cursor(Cursor.HAND_CURSOR)); + } + + @Override + public void mouseReleased(MouseEvent e) { + super.mouseReleased(e); + dispatchDrop((JComponent) e.getSource()); + } + }; + + RedPanel.addMouseListener(dragAdapter); + GreenPanel.addMouseListener(dragAdapter); + BluePanel.addMouseListener(dragAdapter); + YellowPanel.addMouseListener(dragAdapter); + OrangePanel.addMouseListener(dragAdapter); + GrayPanel.addMouseListener(dragAdapter); + CianPanel.addMouseListener(dragAdapter); + PinkPanel.addMouseListener(dragAdapter); + + BaseGasolineTankerLabel.addMouseListener(dragAdapter); + ImprovedGasolineTankerLabel.addMouseListener(dragAdapter); + WheelsLabel.addMouseListener(dragAdapter); + WheelsFirstLabel.addMouseListener(dragAdapter); + WheelsSecondLabel.addMouseListener(dragAdapter); + + ButtonAdd.addActionListener(e -> { + event.emit(gasolineTanker); + dispose(); + }); + ButtonCancel.addActionListener(e -> dispose()); + } + + public void addListener(Consumer listener) { + event.addListener(listener); + } + + public void dispatchDrop(JComponent droppedComponent) { + setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + if (droppedComponent == null) { + return; + } + + if (droppedComponent instanceof JPanel panel) { + if (BodyColorLabel.getMousePosition() != null) { + gasolineTanker.SetBodyColor(panel.getBackground()); + } + if (DopColorLabel.getMousePosition() != null && gasolineTanker instanceof DrawingImprovedGasolineTanker advanced) { + advanced.SetDopColor(panel.getBackground()); + } + } + if (droppedComponent instanceof JLabel label && PictureBox.getMousePosition() != null) { + int speed = (Integer) SpinnerSpeed.getValue(); + int weight = (Integer) SpinnerWeight.getValue(); + int countWheels = (Integer) SpinnerWheels.getValue(); + boolean antenna = CheckBoxAntenna.isSelected(); + boolean bodyKit = checkBoxBodyKit.isSelected(); + + + if (label == BaseGasolineTankerLabel) { + gasolineTanker = new DrawingGasolineTanker(speed, weight, countWheels,Color.WHITE); + } else if (label == ImprovedGasolineTankerLabel) { + gasolineTanker = new DrawingImprovedGasolineTanker(speed, weight,countWheels, Color.WHITE, Color.WHITE, bodyKit, antenna); + } else if (gasolineTanker != null && label == WheelsLabel) { + gasolineTanker.SetWheels(new DrawingWheels(countWheels)); + } else if (gasolineTanker != null && label == WheelsFirstLabel) { + gasolineTanker.SetWheels(new DrawingOrnamentWheelsFirst(countWheels)); + } else if (gasolineTanker != null && label == WheelsSecondLabel) { + gasolineTanker.SetWheels(new DrawingOrnamentWheelsSecond(countWheels)); + } + } + repaint(); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + if (gasolineTanker != null) { + g = PictureBox.getGraphics(); + gasolineTanker.SetPosition(10, 10, PictureBox.getWidth(), PictureBox.getHeight()); + gasolineTanker.DrawTransport((Graphics2D) g); + } + } + + private void createUIComponents() { + } +} diff --git a/src/FormMapWithSetGasolineTanker.java b/src/FormMapWithSetGasolineTanker.java index dc6d5f1..d5ee206 100644 --- a/src/FormMapWithSetGasolineTanker.java +++ b/src/FormMapWithSetGasolineTanker.java @@ -143,22 +143,21 @@ public class FormMapWithSetGasolineTanker extends JFrame{ { return; } - FormCreater dialog=new FormCreater(); - dialog.setSize(1200,700); - dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - dialog.setVisible(true); - if (dialog.getSelectedGasolineTanker()!=null) { - DrawingObjectGasolineTanker gasolineTanker = new DrawingObjectGasolineTanker(dialog.getSelectedGasolineTanker()); + FormGasolineTankerConfig dialog=new FormGasolineTankerConfig(); + dialog.addListener(obj -> { + if (obj!=null) { + DrawingObjectGasolineTanker gasolineTanker = new DrawingObjectGasolineTanker(obj); - if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).plus(gasolineTanker)>=0) { - JOptionPane.showMessageDialog(this, "Object added", "Success", JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); - repaint(); - } else { - JOptionPane.showMessageDialog(this, "Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE); + if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).plus(gasolineTanker) >= 0) { + JOptionPane.showMessageDialog(this, "Object added", "Success", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); + repaint(); + } else { + JOptionPane.showMessageDialog(this, "\n" + "Failed to add object", "Error", JOptionPane.INFORMATION_MESSAGE); + } } - } + }); + dialog.setVisible(true); }); ButtonRemoveGasolineTanker.addActionListener(e -> {