From cd2b9474d47cb47b67348a8d44994c15520be1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Tue, 25 Oct 2022 01:58:38 +0400 Subject: [PATCH] Fixes --- FormArtillery.java | 6 +- FormMapWithSetArtilleries.form | 161 +++++++++++++++++++++++++++++++++ FormMapWithSetArtilleries.java | 150 ++++++++++++++++++++++++++++++ Program.java | 2 +- 4 files changed, 315 insertions(+), 4 deletions(-) create mode 100644 FormMapWithSetArtilleries.form create mode 100644 FormMapWithSetArtilleries.java diff --git a/FormArtillery.java b/FormArtillery.java index 8584b14..4bf9cb8 100644 --- a/FormArtillery.java +++ b/FormArtillery.java @@ -3,7 +3,7 @@ import java.awt.*; import java.awt.event.*; import java.util.Random; -public class FormArtillery extends JFrame { +public class FormArtillery extends JDialog { private JPanel artilleryPane; private JLabel speedLabel; private JLabel weightLabel; @@ -15,7 +15,7 @@ public class FormArtillery extends JFrame { private JButton buttonLeft; private JButton buttonRight; private JButton createAdvancedButton; - private JButton buttonSelect; + public JButton buttonSelect; private DrawingArtillery _artillery; private DrawingArtillery selectedArtillery; @@ -86,7 +86,7 @@ public class FormArtillery extends JFrame { }); buttonSelect.addActionListener(e -> { selectedArtillery = _artillery; - dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); + dispose(); }); } diff --git a/FormMapWithSetArtilleries.form b/FormMapWithSetArtilleries.form new file mode 100644 index 0000000..7cc983b --- /dev/null +++ b/FormMapWithSetArtilleries.form @@ -0,0 +1,161 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormMapWithSetArtilleries.java b/FormMapWithSetArtilleries.java new file mode 100644 index 0000000..f29ca56 --- /dev/null +++ b/FormMapWithSetArtilleries.java @@ -0,0 +1,150 @@ +import javax.swing.*; +import javax.swing.text.DefaultFormatterFactory; +import javax.swing.text.MaskFormatter; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.text.ParseException; + +public class FormMapWithSetArtilleries extends JFrame { + private JPanel pictureBox; + private JPanel toolsGroup; + private JLabel toolsLabel; + private JComboBox comboBoxMapSelector; + private JButton buttonAddArtillery; + private JPanel paneArtilleries; + private JFormattedTextField textBoxPosition; + private JButton buttonRemoveArtillery; + private JButton buttonShowStorage; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + private JButton buttonShowOnMap; + + private Image bufferedImage; + private MapWithSetArtilleriesGeneric _mapArtilleriesCollectionGeneric; + + public FormMapWithSetArtilleries() { + this.setTitle("Artillery"); + this.setContentPane(paneArtilleries); + + try { + textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##"))); + } catch (ParseException e) { + e.printStackTrace(); + } + + comboBoxMapSelector.addActionListener(e -> { + AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) { + case "Простая карта" -> new SimpleMap(); + case "Лесная карта" -> new ForestMap(); + default -> null; + }; + + if (map != null) { + _mapArtilleriesCollectionGeneric = new MapWithSetArtilleriesGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map); + } else { + _mapArtilleriesCollectionGeneric = null; + } + }); + + buttonAddArtillery.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric == null) { + return; + } + + FormArtillery dialog = new FormArtillery(); + dialog.setSize(800, 500); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + + dialog.setVisible(true); + + if (dialog.getSelectedArtillery() != null) { + DrawingObjectArtillery car = new DrawingObjectArtillery(dialog.getSelectedArtillery()); + if (_mapArtilleriesCollectionGeneric.addArtillery(car) != -1) + { + JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); + repaint(); + } + else + { + JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE); + } + } + }); + + buttonRemoveArtillery.addActionListener(e -> { + String text = textBoxPosition.getText(); + if (text == null || text.isEmpty()) { + return; + } + + if (JOptionPane.showConfirmDialog(this, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { + return; + } + + int position = Integer.parseInt(text); + + if (_mapArtilleriesCollectionGeneric.removeArtilleryAt(position) != null) { + JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); + repaint(); + } else { + JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE); + } + }); + + buttonShowStorage.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric == null) { + return; + } + bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); + repaint(); + }); + + buttonShowOnMap.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric == null) { + return; + } + bufferedImage = _mapArtilleriesCollectionGeneric.showOnMap(); + repaint(); + }); + + buttonLeft.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric != null) { + bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Left); + repaint(); + } + }); + buttonRight.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric != null) { + bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Right); + repaint(); + } + }); + buttonUp.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric != null) { + bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Up); + repaint(); + } + }); + buttonDown.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric != null) { + bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Down); + repaint(); + } + }); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + + if (bufferedImage != null) { + pictureBox.paintComponents(bufferedImage.getGraphics()); + pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null); + } + } +} diff --git a/Program.java b/Program.java index 8d8002d..35feedc 100644 --- a/Program.java +++ b/Program.java @@ -2,7 +2,7 @@ import javax.swing.*; public class Program { public static void main(String[] args) { - FormMap form = new FormMap(); + FormMapWithSetArtilleries form = new FormMapWithSetArtilleries(); form.setSize(640, 480); form.setVisible(true); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);