Pibd-22_Emelyanov_A.S._Airb.../FormMapWithSetPlanes.java

193 lines
7.7 KiB
Java
Raw Normal View History

2022-11-30 19:25:43 +04:00
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
public class FormMapWithSetPlanes extends JFrame {
private MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap> _mapPlanesCollectionGeneric;
public JPanel Mainpanel;
private JButton buttonAdd;
private JComboBox comboBoxSelectorMap;
private JTextField textBoxPosition;
private JButton buttonRemove;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonDown;
private JPanel PictureBox;
private JPanel groupBox;
AbstractMap _abstractMap;
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(false);
frame.setBounds(300, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
JFrame jFrame = getFrame();
private void ButtonMove_Click(String name)
{
if (_mapPlanesCollectionGeneric == null) return;
Direction direction = Direction.None;
switch (name)
{
case "buttonLeft":
direction = Direction.Left;
break;
case "buttonUp":
direction = Direction.Up;
break;
case "buttonRight":
direction = Direction.Right;
break;
case "buttonDown":
direction = Direction.Down;
break;
}
PictureBox.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBox.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject(direction)));
PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
}
public FormMapWithSetPlanes() {
comboBoxSelectorMap.addItem("Простая карта");
comboBoxSelectorMap.addItem("Вторая карта");
try {
2022-11-30 19:29:28 +04:00
Image img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowUp.jpg"));
2022-11-30 19:25:43 +04:00
buttonUp.setIcon(new ImageIcon(img));
2022-11-30 19:29:28 +04:00
img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowDown.jpg"));
2022-11-30 19:25:43 +04:00
buttonDown.setIcon(new ImageIcon(img));
2022-11-30 19:29:28 +04:00
img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowLeft.jpg"));
2022-11-30 19:25:43 +04:00
buttonLeft.setIcon(new ImageIcon(img));
2022-11-30 19:29:28 +04:00
img = ImageIO.read(FormMapWithSetPlanes.class.getResource("/Resource/arrowRight.jpg"));
2022-11-30 19:25:43 +04:00
buttonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
comboBoxSelectorMap.addActionListener(e -> {
AbstractMap map = null;
switch (comboBoxSelectorMap.getSelectedItem().toString())
{
case "Простая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new MyMap();
break;
}
if (map != null)
{
_mapPlanesCollectionGeneric = new MapWithSetPlanesGeneric(PictureBox.getWidth(), PictureBox.getHeight(), map);
}
else
{
_mapPlanesCollectionGeneric = null;
}
});
buttonAdd.addActionListener(e -> {
if (_mapPlanesCollectionGeneric == null)
{
return;
}
FormPlane dialog = new FormPlane();
dialog.setSize(800, 600);
dialog.setLocation(500, 200);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
if (dialog.GetSelectedPlane() == null) return;
DrawningObjectPlane plane = new DrawningObjectPlane(dialog.GetSelectedPlane());
if (_mapPlanesCollectionGeneric.addPlane(plane) != -1)
{
JOptionPane.showMessageDialog(jFrame, "Объект добавлен");
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowSet()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект");
}
});
buttonRemove.addActionListener(e -> {
if(_mapPlanesCollectionGeneric == null) return;
String text = textBoxPosition.getText();
if(text.isEmpty()) return;
if(JOptionPane.showConfirmDialog(jFrame, "Вы действительно хотите удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return;
try {
Integer.parseInt(text);
}
catch (Exception ex){
return;
}
int pos = Integer.parseInt(text);
if (_mapPlanesCollectionGeneric.removePlane(pos) != null)
{
JOptionPane.showMessageDialog(jFrame, "Объект удален");
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowSet()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
}
});
buttonShowStorage.addActionListener(e -> {
if (_mapPlanesCollectionGeneric == null) return;
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowSet()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (_mapPlanesCollectionGeneric == null) return;
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.ShowOnMap()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
});
buttonUp.addActionListener(e -> ButtonMove_Click("buttonUp"));
buttonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft"));
buttonDown.addActionListener(e -> ButtonMove_Click("buttonDown"));
buttonRight.addActionListener(e -> ButtonMove_Click("buttonRight"));
}
}