PIbd-23_Bogdanov_D.S._Ship..../FormMapWithSetShips.java

150 lines
5.4 KiB
Java
Raw Normal View History

2022-12-06 12:08:04 +04:00
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 FormMapWithSetShips extends JFrame {
private JPanel pictureBox;
private JPanel toolsGroup;
private JLabel toolsLabel;
private JComboBox comboBoxMapSelector;
private JButton buttonAddShip;
private JPanel paneShips;
private JFormattedTextField textBoxPosition;
private JButton buttonRemoveShip;
private JButton buttonShowStorage;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonShowOnMap;
private Image bufferedImage;
private MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap> _mapShipsCollectionGeneric;
public FormMapWithSetShips() {
this.setTitle("Ship");
this.setContentPane(paneShips);
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 WaterMap();
default -> null;
};
if (map != null) {
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map);
} else {
_mapShipsCollectionGeneric = null;
}
});
buttonAddShip.addActionListener(e -> {
if (_mapShipsCollectionGeneric == null) {
return;
}
FormShip dialog = new FormShip();
dialog.setSize(800, 500);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
if (dialog.getSelectedShip() != null) {
DrawingObjectShip ship = new DrawingObjectShip(dialog.getSelectedShip());
if (_mapShipsCollectionGeneric.addShip(ship) != -1)
{
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapShipsCollectionGeneric.showSet();
repaint();
}
else
{
JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
}
});
buttonRemoveShip.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 (_mapShipsCollectionGeneric.removeShipAt(position) != null) {
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapShipsCollectionGeneric.showSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
});
buttonShowStorage.addActionListener(e -> {
if (_mapShipsCollectionGeneric == null) {
return;
}
bufferedImage = _mapShipsCollectionGeneric.showSet();
repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (_mapShipsCollectionGeneric == null) {
return;
}
bufferedImage = _mapShipsCollectionGeneric.showOnMap();
repaint();
});
buttonLeft.addActionListener(e -> {
if (_mapShipsCollectionGeneric != null) {
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Left);
repaint();
}
});
buttonRight.addActionListener(e -> {
if (_mapShipsCollectionGeneric != null) {
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Right);
repaint();
}
});
buttonUp.addActionListener(e -> {
if (_mapShipsCollectionGeneric != null) {
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Up);
repaint();
}
});
buttonDown.addActionListener(e -> {
if (_mapShipsCollectionGeneric != null) {
bufferedImage = _mapShipsCollectionGeneric.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);
}
}
}