193 lines
7.6 KiB
Java
193 lines
7.6 KiB
Java
import javax.imageio.ImageIO;
|
||
import javax.swing.*;
|
||
import java.awt.*;
|
||
|
||
public class FormMapWithSetBoats extends JFrame {
|
||
|
||
private MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap> _mapBoatsCollectionGeneric;
|
||
|
||
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 (_mapBoatsCollectionGeneric == 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(_mapBoatsCollectionGeneric.MoveObject(direction)));
|
||
PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
PictureBox.revalidate();
|
||
PictureBox.repaint();
|
||
}
|
||
|
||
public FormMapWithSetBoats() {
|
||
comboBoxSelectorMap.addItem("Простая карта");
|
||
comboBoxSelectorMap.addItem("Вторая карта");
|
||
try {
|
||
Image img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/up.jpg"));
|
||
buttonUp.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/down.jpg"));
|
||
buttonDown.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/left.jpg"));
|
||
buttonLeft.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/right.jpg"));
|
||
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)
|
||
{
|
||
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric(PictureBox.getWidth(), PictureBox.getHeight(), map);
|
||
}
|
||
else
|
||
{
|
||
_mapBoatsCollectionGeneric = null;
|
||
}
|
||
});
|
||
buttonAdd.addActionListener(e -> {
|
||
if (_mapBoatsCollectionGeneric == null)
|
||
{
|
||
return;
|
||
}
|
||
FormBoat dialog = new FormBoat();
|
||
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.GetSelectedBoat() == null) return;
|
||
|
||
DrawningObjectBoat boat = new DrawningObjectBoat(dialog.GetSelectedBoat());
|
||
|
||
if (_mapBoatsCollectionGeneric.addBoat(boat) != -1)
|
||
{
|
||
JOptionPane.showMessageDialog(jFrame, "Объект добавлен");
|
||
PictureBox.removeAll();
|
||
JLabel imageOfBoat = new JLabel();
|
||
imageOfBoat.setPreferredSize(PictureBox.getSize());
|
||
imageOfBoat.setMinimumSize(new Dimension(1, 1));
|
||
imageOfBoat.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet()));
|
||
PictureBox.add(imageOfBoat,BorderLayout.CENTER);
|
||
PictureBox.revalidate();
|
||
PictureBox.repaint();
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект");
|
||
}
|
||
});
|
||
buttonRemove.addActionListener(e -> {
|
||
if(_mapBoatsCollectionGeneric == 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 (_mapBoatsCollectionGeneric.removeBoat(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(_mapBoatsCollectionGeneric.ShowSet()));
|
||
PictureBox.add(imageOfShip,BorderLayout.CENTER);
|
||
PictureBox.revalidate();
|
||
PictureBox.repaint();
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
|
||
}
|
||
});
|
||
buttonShowStorage.addActionListener(e -> {
|
||
if (_mapBoatsCollectionGeneric == null) return;
|
||
PictureBox.removeAll();
|
||
JLabel imageOfShip = new JLabel();
|
||
imageOfShip.setPreferredSize(PictureBox.getSize());
|
||
imageOfShip.setMinimumSize(new Dimension(1, 1));
|
||
imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet()));
|
||
PictureBox.add(imageOfShip,BorderLayout.CENTER);
|
||
PictureBox.revalidate();
|
||
PictureBox.repaint();
|
||
});
|
||
buttonShowOnMap.addActionListener(e -> {
|
||
if (_mapBoatsCollectionGeneric == null) return;
|
||
PictureBox.removeAll();
|
||
JLabel imageOfShip = new JLabel();
|
||
imageOfShip.setPreferredSize(PictureBox.getSize());
|
||
imageOfShip.setMinimumSize(new Dimension(1, 1));
|
||
imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.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"));
|
||
}
|
||
}
|