PIbd-12_Karamushko_M.K._Air.../FormMapWithSetAircrafts.java

176 lines
5.5 KiB
Java
Raw Normal View History

2022-11-15 18:20:34 +04:00
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.text.ParseException;
import java.util.Objects;
public class FormMapWithSetAircrafts implements Form {
private JPanel MainPane;
private JButton buttonAdd;
private JComboBox comboBoxSelectorMap;
private JTextField textBoxPosition;
private JButton buttonRemove;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton upButton;
private JButton leftButton;
private JButton rightButton;
private JButton downButton;
private JPanel drawPanel;
private MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap> _mapCarsCollectionGeneric;
private Canvas canv = new Canvas(this);
JFrame jFrame = getFrame();
Image img;
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setBounds(300, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
public void run() {
jFrame.add(MainPane);
drawPanel.add(canv);
comboBoxSelectorMap.setSelectedIndex(-1);
comboBoxSelectorMap.addActionListener(e -> {
AbstractMap map = null;
switch (comboBoxSelectorMap.getSelectedItem().toString())
{
case "Простая карта":
map = new SimpleMap();
break;
case "Моя карта":
map = new MyMap();
break;
}
if (map != null)
{
Dimension canvSize = canv.getSize();
_mapCarsCollectionGeneric = new MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>(
canvSize.width, canvSize.height, map);
}
else
{
_mapCarsCollectionGeneric = null;
}
});
buttonAdd.addActionListener(e -> {
if (_mapCarsCollectionGeneric == null) return;
FormAircraft dialog = new FormAircraft();
dialog.run();
dialog.setSize(800, 500);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
if (dialog.getSelectedAircraft() == null) return;
DrawingObjectAircraft aircraft = new DrawingObjectAircraft(dialog.getSelectedAircraft());
if (_mapCarsCollectionGeneric.addAircraft(aircraft) != -1)
{
JOptionPane.showMessageDialog(jFrame, "Объект добавлен");
img = _mapCarsCollectionGeneric.ShowSet();
canv.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект");
}
});
buttonRemove.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
String text = textBoxPosition.getText();
if(text.isEmpty()) return;
if(JOptionPane.showConfirmDialog(
jFrame,
"Вы действительно хотите удалить объект?",
"Удаление",
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return;
int pos;
try {
pos = Integer.parseInt(text);
} catch (Exception err) {
return;
}
pos = Integer.parseInt(text);
if(_mapCarsCollectionGeneric.removeAircraft(pos) != null) {
JOptionPane.showMessageDialog(jFrame, "Объект удален");
img = _mapCarsCollectionGeneric.ShowSet();
canv.repaint();
} else {
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
}
});
buttonShowStorage.addActionListener(e -> {
if (_mapCarsCollectionGeneric == null)
{
return;
}
img = _mapCarsCollectionGeneric.ShowSet();
canv.repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (_mapCarsCollectionGeneric == null)
{
return;
}
img = _mapCarsCollectionGeneric.ShowOnMap();
canv.repaint();
});
leftButton.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
img = _mapCarsCollectionGeneric.MoveObject(Direction.Left);
canv.repaint();
});
rightButton.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
img = _mapCarsCollectionGeneric.MoveObject(Direction.Right);
canv.repaint();
});
upButton.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
img = _mapCarsCollectionGeneric.MoveObject(Direction.Up);
canv.repaint();
});
downButton.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
img = _mapCarsCollectionGeneric.MoveObject(Direction.Down);
canv.repaint();
});
}
@Override
public void Draw(Graphics2D g) {
if(img == null) return;
g.drawImage(img, 0, 0, null);
}
}