diff --git a/.idea/misc.xml b/.idea/misc.xml index 0548357..5faa26a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/Action.java b/src/Action.java new file mode 100644 index 0000000..71c2f43 --- /dev/null +++ b/src/Action.java @@ -0,0 +1,22 @@ +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.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 Invoke(T warship) { + for (var listener : listeners) { + listener.accept(warship); + } + } +} \ No newline at end of file diff --git a/src/DrawingAdvancedWarship.java b/src/DrawingAdvancedWarship.java index 2628b46..023949a 100644 --- a/src/DrawingAdvancedWarship.java +++ b/src/DrawingAdvancedWarship.java @@ -8,6 +8,11 @@ public class DrawingAdvancedWarship extends DrawingWarship { Blocks= GetFormOfBlock(blockForm); Blocks.SetBlockCount(2*(int)(Math.random()*3+1)); } + public DrawingAdvancedWarship(int speed, float weight,int countblock, Color bodyColor, Color dopColor, boolean Helipad,boolean Antenna, boolean Missile) { + super(speed, weight, bodyColor, 120, 50); + Warship=new EntityAdvancedWarship(speed,weight,bodyColor,dopColor,Helipad,Antenna,Missile); + Blocks.SetBlockCount(countblock); + } public DrawingAdvancedWarship(int speed, float weight, Color bodyColor, Color dopColor, boolean Helipad,boolean Antenna, boolean Missile,IDrawingObjectBlock blockForm) { super(speed, weight, bodyColor, 120, 50); Warship=new EntityAdvancedWarship(speed,weight,bodyColor,dopColor,Helipad,Antenna,Missile); @@ -17,7 +22,15 @@ public class DrawingAdvancedWarship extends DrawingWarship { super(warship,block); Warship = warship; } - + @Override + public void SetBodyColor(Color color){ + var warship=(EntityAdvancedWarship) Warship; + Warship=new EntityAdvancedWarship(warship.GetSpeed(),warship.GetWeight(),color,warship.GetDopColor(), warship.GetHelipad(), warship.GetAntenna(), warship.GetMissile()); + } + public void SetDopColor(Color color){ + var warship=(EntityAdvancedWarship) Warship; + Warship=new EntityAdvancedWarship(warship.GetSpeed(),warship.GetWeight(),warship.GetBodyColor(),color, warship.GetHelipad(), warship.GetAntenna(), warship.GetMissile()); + } @Override public void DrawTransport(Graphics g) { diff --git a/src/DrawingBlock.java b/src/DrawingBlock.java index 5251684..c42daaf 100644 --- a/src/DrawingBlock.java +++ b/src/DrawingBlock.java @@ -7,6 +7,9 @@ public class DrawingBlock implements IDrawingObjectBlock{ public DrawingBlock(BlockCount block) { _block=block; } + public DrawingBlock(int count) { + SetBlockCount(count); + } @Override public void SetBlockCount(int count){ @@ -16,6 +19,7 @@ public class DrawingBlock implements IDrawingObjectBlock{ return; } } + @Override public void DrawBlock(Graphics2D g,int _startPosX, int _startPosY) { if (_block.GetBlockCount() >= 2) { diff --git a/src/DrawingRoundBlocks.java b/src/DrawingRoundBlocks.java index fc1e96f..fa171fb 100644 --- a/src/DrawingRoundBlocks.java +++ b/src/DrawingRoundBlocks.java @@ -6,6 +6,9 @@ public class DrawingRoundBlocks implements IDrawingObjectBlock{ public DrawingRoundBlocks(BlockCount block){ _block=block; } + public DrawingRoundBlocks(int count) { + SetBlockCount(count); + } @Override public void SetBlockCount(int count) { for (BlockCount temp: BlockCount.values()) diff --git a/src/DrawingTriangleBlocks.java b/src/DrawingTriangleBlocks.java index 0e955c8..6ae00cf 100644 --- a/src/DrawingTriangleBlocks.java +++ b/src/DrawingTriangleBlocks.java @@ -6,6 +6,9 @@ public class DrawingTriangleBlocks implements IDrawingObjectBlock{ public DrawingTriangleBlocks(BlockCount block){ _block=block; } + public DrawingTriangleBlocks(int count) { + SetBlockCount(count); + } @Override public void SetBlockCount(int count) { for (BlockCount temp: BlockCount.values()) diff --git a/src/DrawingWarship.java b/src/DrawingWarship.java index 7496ee8..605a8ec 100644 --- a/src/DrawingWarship.java +++ b/src/DrawingWarship.java @@ -25,11 +25,25 @@ public class DrawingWarship { Warship = new EntityWarship(speed, weight, bodyColor); Blocks= blockForm; } + public DrawingWarship(int speed, float weight,int countblock, Color bodyColor) + { + Warship = new EntityWarship(speed, weight, bodyColor); + Blocks= GetFormOfBlock((int)(Math.random()*3+1)); + Blocks.SetBlockCount(countblock); + } public DrawingWarship(EntityWarship warship,IDrawingObjectBlock block){ Warship = warship; Blocks = block; } + public void SetBlocks(IDrawingObjectBlock block){ + Blocks=block; + } + + public void SetBodyColor(Color color){ + Warship= new EntityWarship(Warship.GetSpeed(), Warship.GetWeight(), color); + } + public IDrawingObjectBlock GetFormOfBlock(int FormOfBlock){ BlockForm temp = null; for (BlockForm form:BlockForm.values()) { diff --git a/src/FormMapWithSetWarships.java b/src/FormMapWithSetWarships.java index 90ef3e5..c082874 100644 --- a/src/FormMapWithSetWarships.java +++ b/src/FormMapWithSetWarships.java @@ -137,22 +137,21 @@ public class FormMapWithSetWarships 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.getSelectedWarship()!=null) { - DrawingObjectWarship warship = new DrawingObjectWarship(dialog.getSelectedWarship()); + FormWarshipConfig dialog=new FormWarshipConfig(); + dialog.addListener(obj -> { + if (obj!=null) { + DrawingObjectWarship warship = new DrawingObjectWarship(obj); - if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).plus(warship)>=0) { - JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); - repaint(); - } else { - JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE); + if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).plus(warship)>=0) { + JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); + repaint(); + } else { + JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE); + } } - } + }); + dialog.setVisible(true); }); ButtonRemoveWarship.addActionListener(e -> { diff --git a/src/FormWarshipConfig.form b/src/FormWarshipConfig.form new file mode 100644 index 0000000..2e05822 --- /dev/null +++ b/src/FormWarshipConfig.form @@ -0,0 +1,328 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormWarshipConfig.java b/src/FormWarshipConfig.java new file mode 100644 index 0000000..69068da --- /dev/null +++ b/src/FormWarshipConfig.java @@ -0,0 +1,151 @@ +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 FormWarshipConfig extends JFrame { + private final Action event = new Action<>(); + private DrawingWarship warship; + BlockCount block; + private IDrawingObjectBlock _block=null; + private JPanel MainPanel; + private JPanel GroupBoxConfig; + private JSpinner SpinnerSpeed; + private JSpinner SpinnerWeight; + private JCheckBox CheckBoxAntenna; + private JCheckBox CheckBoxHelipad; + private JCheckBox CheckBoxMissile; + private JLabel SpeedLabel; + private JLabel WeightLabel; + private JPanel ColorPanel; + private JPanel RedPanel; + private JPanel OrangePanel; + private JPanel GreenPanel; + private JPanel BluePanel; + private JPanel YellowPanel; + private JPanel CianPanel; + private JPanel PinkPanel; + private JPanel GrayPanel; + private JLabel CountBlockLabel; + private JSpinner SpinnerBlock; + private JPanel TypeBlock; + private JLabel SquareBlockLabel; + private JLabel TriangleBlockLabel; + private JLabel RoundBlockLabel; + private JPanel PreviewPanel; + private JLabel BodyColorLabel; + private JLabel DopColorLabel; + private JLabel BaseWarshipLabel; + private JLabel AdvancedWarshipLabel; + private JButton ButtonAdd; + private JButton ButtonCancel; + private JPanel PictureBox; + + public FormWarshipConfig(){ + this.setTitle("Создание объекта"); + this.setSize(900,500); + this.setContentPane(MainPanel); + this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); + + SquareBlockLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + TriangleBlockLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + RoundBlockLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + BodyColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + DopColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + BaseWarshipLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + AdvancedWarshipLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + + SpinnerBlock.setModel(new SpinnerNumberModel(2, 2, 6, 2)); + SpinnerSpeed.setModel(new SpinnerNumberModel(100, 100, 1000, 10)); + SpinnerWeight.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); + + BaseWarshipLabel.addMouseListener(dragAdapter); + AdvancedWarshipLabel.addMouseListener(dragAdapter); + SquareBlockLabel.addMouseListener(dragAdapter); + TriangleBlockLabel.addMouseListener(dragAdapter); + RoundBlockLabel.addMouseListener(dragAdapter); + + ButtonAdd.addActionListener(e -> { + event.Invoke(warship); + 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) { + warship.SetBodyColor(panel.getBackground()); + } + if (DopColorLabel.getMousePosition() != null && warship instanceof DrawingAdvancedWarship advanced) { + advanced.SetDopColor(panel.getBackground()); + } + } + if (droppedComponent instanceof JLabel label && PictureBox.getMousePosition() != null) { + int speed = (Integer) SpinnerSpeed.getValue(); + int weight = (Integer) SpinnerWeight.getValue(); + int countBlock = (Integer) SpinnerBlock.getValue(); + boolean antenna = CheckBoxAntenna.isSelected(); + boolean helipad = CheckBoxHelipad.isSelected(); + boolean missile = CheckBoxMissile.isSelected(); + + if (label == BaseWarshipLabel) { + warship = new DrawingWarship(speed, weight, countBlock,Color.WHITE); + } else if (label == AdvancedWarshipLabel) { + warship = new DrawingAdvancedWarship(speed, weight,countBlock, Color.WHITE, Color.WHITE, helipad, antenna,missile); + } else if (warship != null && label == SquareBlockLabel) { + warship.SetBlocks(new DrawingBlock(countBlock)); + } else if (warship != null && label == TriangleBlockLabel) { + warship.SetBlocks(new DrawingTriangleBlocks(countBlock)); + } else if (warship != null && label == RoundBlockLabel) { + warship.SetBlocks(new DrawingRoundBlocks(countBlock)); + } + } + repaint(); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + if (warship != null) { + g = PictureBox.getGraphics(); + warship.SetPosition(10, 10, PictureBox.getWidth(), PictureBox.getHeight()); + warship.DrawTransport((Graphics2D) g); + } + } +}