151 lines
5.7 KiB
Java
151 lines
5.7 KiB
Java
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<DrawingObjectArtillery, AbstractMap> _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);
|
||
}
|
||
}
|
||
}
|