PIbd-23_Polevoy_S.V._SelfPr.../FormMapWithSetArtilleries.java

203 lines
8.3 KiB
Java
Raw Normal View History

2022-10-25 01:58:38 +04:00
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
2022-10-25 01:58:38 +04:00
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Optional;
2022-10-25 01:58:38 +04:00
public class FormMapWithSetArtilleries extends JFrame {
private JPanel pictureBox;
private JPanel toolsGroup;
private JLabel toolsLabel;
private JComboBox<String> comboBoxMapSelector;
2022-10-25 01:58:38 +04:00
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 JLabel mapsLabel;
private JTextField textFieldMapName;
private JButton buttonAddMap;
private JList<String> listBoxMaps;
private JPanel mapsGroup;
private JButton buttonDeleteMap;
2022-10-25 01:58:38 +04:00
private Image bufferedImage;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
put("Простая карта", new SimpleMap());
put("Лесная карта", new ForestMap());
}};
private final MapsCollection _mapsCollection;
2022-10-25 01:58:38 +04:00
public FormMapWithSetArtilleries() {
this.setTitle("Artillery");
this.setContentPane(paneArtilleries);
this.setSize(640, 530);
this.setVisible(true);
2022-10-25 01:58:38 +04:00
try {
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
} catch (ParseException e) {
e.printStackTrace();
}
_mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight());
2022-10-25 01:58:38 +04:00
comboBoxMapSelector.removeAllItems();
for (var key : _mapsDict.keySet()) {
comboBoxMapSelector.addItem(key);
}
buttonAddMap.addActionListener(e -> {
if (comboBoxMapSelector.getSelectedIndex() == -1 || textFieldMapName.getText() == null || textFieldMapName.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
if (!_mapsDict.containsKey((String)comboBoxMapSelector.getSelectedItem())) {
JOptionPane.showMessageDialog(this, "Нет такой карты", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
_mapsCollection.addMap(textFieldMapName.getText(), _mapsDict.get((String)comboBoxMapSelector.getSelectedItem()));
reloadMaps();
});
buttonDeleteMap.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
if (JOptionPane.showConfirmDialog(this, "Удалить карту " + listBoxMaps.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
_mapsCollection.deleteMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse(""));
reloadMaps();
}
});
listBoxMaps.addListSelectionListener(e -> {
if (listBoxMaps.getSelectedValue() != null) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
2022-10-25 01:58:38 +04:00
}
});
buttonAddArtillery.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) {
2022-10-25 01:58:38 +04:00
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 artillery = new DrawingObjectArtillery(dialog.getSelectedArtillery());
if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addArtillery(artillery) != -1)
2022-10-25 01:58:38 +04:00
{
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
2022-10-25 01:58:38 +04:00
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 (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).removeArtilleryAt(position) != null) {
2022-10-25 01:58:38 +04:00
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
2022-10-25 01:58:38 +04:00
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
});
buttonShowStorage.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) {
2022-10-25 01:58:38 +04:00
return;
}
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
2022-10-25 01:58:38 +04:00
repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) {
2022-10-25 01:58:38 +04:00
return;
}
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showOnMap();
2022-10-25 01:58:38 +04:00
repaint();
});
buttonLeft.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left);
2022-10-25 01:58:38 +04:00
repaint();
}
});
buttonRight.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Right);
2022-10-25 01:58:38 +04:00
repaint();
}
});
buttonUp.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Up);
2022-10-25 01:58:38 +04:00
repaint();
}
});
buttonDown.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Down);
2022-10-25 01:58:38 +04:00
repaint();
}
});
}
private void reloadMaps() {
int index = listBoxMaps.getSelectedIndex();
listBoxMaps.setListData(_mapsCollection.getKeys().toArray(new String[0]));
int size = listBoxMaps.getModel().getSize();
if (size > 0 && (index == -1 || index >= size)) {
listBoxMaps.setSelectedIndex(0);
} else if (index > -1 && index < size) {
listBoxMaps.setSelectedIndex(index);
}
}
2022-10-25 01:58:38 +04:00
@Override
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
pictureBox.paintComponents(bufferedImage.getGraphics());
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
}