2022-12-23 10:22:06 +04:00
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
2022-12-23 11:15:23 +04:00
|
|
|
|
import java.awt.event.KeyAdapter;
|
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.util.HashMap;
|
2022-12-23 10:22:06 +04:00
|
|
|
|
|
2022-12-23 12:16:56 +04:00
|
|
|
|
public class FormMapWithSetBoats extends JFrame{
|
2022-12-23 10:22:06 +04:00
|
|
|
|
public JPanel Mainpanel;
|
2022-12-23 12:16:56 +04:00
|
|
|
|
private JPanel pictureBoxBoat;
|
|
|
|
|
private JPanel GroupBoxTools;
|
|
|
|
|
private JComboBox ComboBoxSelectorMap;
|
|
|
|
|
private JButton ButtonAddBoat;
|
|
|
|
|
private JButton ButtonDeleteBoat;
|
|
|
|
|
private JButton ButtonShowStorage;
|
|
|
|
|
private JButton ButtonShowMap;
|
|
|
|
|
private JButton ButtonLeft;
|
|
|
|
|
private JButton ButtonDown;
|
|
|
|
|
private JButton ButtonUp;
|
|
|
|
|
private JButton ButtonRight;
|
|
|
|
|
private JTextField maskedTextBoxPosition;
|
|
|
|
|
private JTextField textBoxNewMapName;
|
|
|
|
|
private int picWidth=600;
|
|
|
|
|
private int picHeight=500;
|
|
|
|
|
private JButton ButtonAddMap;
|
|
|
|
|
private JList ListBoxMaps;
|
|
|
|
|
private JButton ButtonDeleteMap;
|
|
|
|
|
private JPanel GroupBoxMaps;
|
|
|
|
|
private JButton ButtonCheckDel;
|
|
|
|
|
private MapWithSetBoatsGeneric<DrawningObjectBoat,AbstractMap> _mapBoatsCollectionGeneric;
|
|
|
|
|
private final HashMap<String,AbstractMap> _mapsDict = new HashMap<>(){{
|
|
|
|
|
put("Простая карта",new SimpleMap());
|
|
|
|
|
put("Вторая карта",new MyMap());
|
|
|
|
|
}};
|
|
|
|
|
private final MapsCollection _mapsCollection;
|
|
|
|
|
public void UpdateWindow(BufferedImage bmp)
|
|
|
|
|
{
|
|
|
|
|
pictureBoxBoat.removeAll();
|
|
|
|
|
JLabel imageWithMapAndObject = new JLabel();
|
|
|
|
|
imageWithMapAndObject.setPreferredSize(pictureBoxBoat.getSize());
|
|
|
|
|
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
|
|
|
|
imageWithMapAndObject.setIcon(new ImageIcon(bmp));
|
|
|
|
|
pictureBoxBoat.add(imageWithMapAndObject, BorderLayout.CENTER);
|
|
|
|
|
pictureBoxBoat.revalidate();
|
|
|
|
|
}
|
|
|
|
|
private void ReloadMaps(){
|
|
|
|
|
int index = ListBoxMaps.getSelectedIndex();
|
|
|
|
|
DefaultListModel<String> model1 = (DefaultListModel<String>) ListBoxMaps.getModel();
|
|
|
|
|
model1.removeAllElements();
|
|
|
|
|
for(int i=0;i<_mapsCollection.Keys().size();i++)
|
|
|
|
|
{
|
|
|
|
|
model1.addElement(_mapsCollection.Keys().get(i));
|
|
|
|
|
}
|
|
|
|
|
if (ListBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= ListBoxMaps.getModel().getSize()))
|
|
|
|
|
{
|
|
|
|
|
ListBoxMaps.setSelectedIndex(0);
|
|
|
|
|
}
|
|
|
|
|
else if (ListBoxMaps.getModel().getSize() > 0 && index > -1 && index < ListBoxMaps.getModel().getSize())
|
|
|
|
|
{
|
|
|
|
|
ListBoxMaps.setSelectedIndex(index);
|
|
|
|
|
}
|
2022-12-23 10:22:06 +04:00
|
|
|
|
}
|
|
|
|
|
private void ButtonMove_Click(String name)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2022-12-23 12:16:56 +04:00
|
|
|
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(direction));
|
2022-12-23 10:22:06 +04:00
|
|
|
|
}
|
2022-12-23 12:16:56 +04:00
|
|
|
|
public FormMapWithSetBoats()
|
|
|
|
|
{
|
|
|
|
|
_mapsCollection = new MapsCollection(picWidth, picHeight);
|
|
|
|
|
ComboBoxSelectorMap.removeAllItems();
|
|
|
|
|
for (String elem : _mapsDict.keySet()) {
|
|
|
|
|
ComboBoxSelectorMap.addItem(elem);
|
|
|
|
|
}
|
2022-12-23 10:22:06 +04:00
|
|
|
|
try {
|
2022-12-23 12:16:56 +04:00
|
|
|
|
Image img = ImageIO.read(FormBoat.class.getResource("/Resource/up.jpg"));
|
|
|
|
|
ButtonUp.setIcon(new ImageIcon(img));
|
|
|
|
|
img = ImageIO.read(FormBoat.class.getResource("/Resource/down.jpg"));
|
|
|
|
|
ButtonDown.setIcon(new ImageIcon(img));
|
|
|
|
|
img = ImageIO.read(FormBoat.class.getResource("/Resource/left.jpg"));
|
|
|
|
|
ButtonLeft.setIcon(new ImageIcon(img));
|
|
|
|
|
img = ImageIO.read(FormBoat.class.getResource("/Resource/right.jpg"));
|
|
|
|
|
ButtonRight.setIcon(new ImageIcon(img));
|
2022-12-23 10:22:06 +04:00
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
System.out.println(ex);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 12:16:56 +04:00
|
|
|
|
ButtonAddBoat.addActionListener(e -> {
|
|
|
|
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
2022-12-23 10:22:06 +04:00
|
|
|
|
{
|
|
|
|
|
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;
|
2022-12-23 12:16:56 +04:00
|
|
|
|
DrawningObjectBoat Boat = new DrawningObjectBoat(dialog.GetSelectedBoat());
|
|
|
|
|
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).addBoat(Boat)!=-1)
|
2022-12-23 10:22:06 +04:00
|
|
|
|
{
|
2022-12-23 12:16:56 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null,"Объект добавлен");
|
|
|
|
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
2022-12-23 10:22:06 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-12-23 12:16:56 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
2022-12-23 10:22:06 +04:00
|
|
|
|
}
|
|
|
|
|
});
|
2022-12-23 12:16:56 +04:00
|
|
|
|
ListBoxMaps.addListSelectionListener(e -> {
|
|
|
|
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
|
|
|
|
return;
|
|
|
|
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
|
|
|
|
});
|
|
|
|
|
ButtonDeleteBoat.addActionListener(e -> {
|
|
|
|
|
if (maskedTextBoxPosition.getText().isEmpty())
|
|
|
|
|
{
|
|
|
|
|
return;
|
2022-12-23 10:22:06 +04:00
|
|
|
|
}
|
2022-12-23 12:16:56 +04:00
|
|
|
|
int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
|
|
|
|
|
if(result==JOptionPane.NO_OPTION)
|
|
|
|
|
{
|
2022-12-23 10:22:06 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-23 12:16:56 +04:00
|
|
|
|
int pos=Integer.parseInt(maskedTextBoxPosition.getText());
|
|
|
|
|
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).removeBoat(pos)!=null)
|
2022-12-23 10:22:06 +04:00
|
|
|
|
{
|
2022-12-23 12:16:56 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Объект удален");
|
|
|
|
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
2022-12-23 10:22:06 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-12-23 12:16:56 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
|
2022-12-23 10:22:06 +04:00
|
|
|
|
}
|
|
|
|
|
});
|
2022-12-23 12:16:56 +04:00
|
|
|
|
ButtonShowStorage.addActionListener(e -> {
|
|
|
|
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
2022-12-23 10:22:06 +04:00
|
|
|
|
});
|
2022-12-23 12:16:56 +04:00
|
|
|
|
ButtonShowMap.addActionListener(e -> {
|
|
|
|
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap());
|
|
|
|
|
});
|
|
|
|
|
ButtonUp.addActionListener(e -> ButtonMove_Click("buttonUp"));
|
|
|
|
|
ButtonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft"));
|
|
|
|
|
ButtonDown.addActionListener(e -> ButtonMove_Click("buttonDown"));
|
|
|
|
|
ButtonRight.addActionListener(e -> ButtonMove_Click("buttonRight"));
|
|
|
|
|
maskedTextBoxPosition.addKeyListener(new KeyAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void keyTyped(KeyEvent e) {
|
|
|
|
|
char c = e.getKeyChar();
|
|
|
|
|
if ( ((c < '0') || (c > '9')) || maskedTextBoxPosition.getText().length() >= 2) {
|
|
|
|
|
e.consume();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-23 10:22:06 +04:00
|
|
|
|
});
|
2022-12-23 12:16:56 +04:00
|
|
|
|
ButtonAddMap.addActionListener(e -> {
|
|
|
|
|
if (ComboBoxSelectorMap.getSelectedIndex() == -1 || textBoxNewMapName.getText().isEmpty())
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(!_mapsDict.containsKey(ComboBoxSelectorMap.getSelectedItem()))
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
_mapsCollection.AddMap(textBoxNewMapName.getText(),_mapsDict.get(ComboBoxSelectorMap.getSelectedItem().toString()));
|
|
|
|
|
ReloadMaps();
|
|
|
|
|
});
|
|
|
|
|
ButtonDeleteMap.addActionListener(e -> {
|
|
|
|
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(JOptionPane.showConfirmDialog(null,"Удалить карту"+ListBoxMaps.getSelectedValue().toString()+"?","Удаление",JOptionPane.YES_NO_OPTION)==0)
|
|
|
|
|
{
|
|
|
|
|
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
|
|
|
|
|
ReloadMaps();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
ButtonCheckDel.addActionListener(e -> {
|
|
|
|
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DrawningObjectBoat Boat=_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).GetBoatsDeleted();
|
|
|
|
|
if(Boat==null)
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
FormBoat formBoat=new FormBoat(Boat);
|
|
|
|
|
formBoat.setSize(1000,800);
|
|
|
|
|
formBoat.setVisible(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createUIComponents() {
|
|
|
|
|
DefaultListModel<String> dlm = new DefaultListModel<String>();
|
|
|
|
|
ListBoxMaps = new JList(dlm);
|
2022-12-23 10:22:06 +04:00
|
|
|
|
}
|
|
|
|
|
}
|