381 lines
14 KiB
Java
381 lines
14 KiB
Java
import javax.imageio.ImageIO;
|
||
import javax.swing.*;
|
||
import javax.swing.event.ListSelectionEvent;
|
||
import javax.swing.event.ListSelectionListener;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
import java.awt.event.KeyAdapter;
|
||
import java.awt.event.KeyEvent;
|
||
import java.awt.image.BufferedImage;
|
||
import java.util.HashMap;
|
||
|
||
public class FormMapWithSetPlanesGeneric extends JFrame{
|
||
public JPanel MainPanel;
|
||
private JPanel PictureBoxPlane;
|
||
private JPanel GroupBoxTools;
|
||
private JComboBox ComboBoxSelectorMap;
|
||
private JButton ButtonLeft;
|
||
private JButton ButtonDown;
|
||
private JButton ButtonRight;
|
||
private JButton ButtonUp;
|
||
private JButton ButtonShowOnMap;
|
||
private JButton ButtonShowStorage;
|
||
private JButton ButtonAddPlane;
|
||
private JTextField MaskedTextBoxPosition;
|
||
private JButton ButtonRemovePlane;
|
||
private JTextField TextBoxNewMapName;
|
||
private JButton ButtonAddMap;
|
||
private JList ListBoxMaps;
|
||
private JButton ButtonDeleteMap;
|
||
private JPanel GroupBoxMap;
|
||
private JButton ButtonShowDelete;
|
||
|
||
//объект от коллекции карт
|
||
private final MapsCollection _mapsCollection;
|
||
|
||
//Для выпадающего списка
|
||
HashMap<String, AbstractMap> _mapsHashMap = new HashMap<>()
|
||
{{
|
||
put("Простая карта", new SimpleMap());
|
||
put("Буря в пустыне", new DesertStormMap());
|
||
put("Звёздные войны", new StarWarsMap());
|
||
}};
|
||
|
||
public FormMapWithSetPlanesGeneric()
|
||
{
|
||
super("Хранилище");
|
||
CreateWindow();
|
||
_mapsCollection = new MapsCollection(730, 650);
|
||
ComboBoxSelectorMap.removeAllItems();
|
||
|
||
for (String elem : _mapsHashMap.keySet())
|
||
{
|
||
ComboBoxSelectorMap.addItem(elem);
|
||
}
|
||
}
|
||
|
||
//заполнение ListBoxMaps
|
||
private void ReloadMaps()
|
||
{
|
||
int index = ListBoxMaps.getSelectedIndex();
|
||
|
||
DefaultListModel<String> model = (DefaultListModel<String>)ListBoxMaps.getModel();
|
||
model.removeAllElements();
|
||
|
||
for (int i = 0; i < _mapsCollection.Keys().size(); i++)
|
||
{
|
||
model.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);
|
||
}
|
||
}
|
||
|
||
//обновление отрисовки
|
||
public void UpdateWindow(BufferedImage bmp)
|
||
{
|
||
PictureBoxPlane.removeAll();
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(bmp));
|
||
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
PictureBoxPlane.revalidate();
|
||
}
|
||
|
||
public void CreateWindow()
|
||
{
|
||
//загрузка изображений для кнопок
|
||
try
|
||
{
|
||
Image img = ImageIO.read(getClass().getResource("resourses/Up.png"));
|
||
ButtonUp.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(getClass().getResource("resourses/Left.png"));
|
||
ButtonLeft.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(getClass().getResource("resourses/Down.png"));
|
||
ButtonDown.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(getClass().getResource("resourses/Right.png"));
|
||
ButtonRight.setIcon(new ImageIcon(img));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
System.out.println(ex.getMessage());
|
||
}
|
||
|
||
//добавить карту
|
||
ButtonAddMap.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(ComboBoxSelectorMap.getSelectedIndex() == -1 || TextBoxNewMapName.getText().isEmpty())
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||
|
||
return;
|
||
}
|
||
|
||
if(!_mapsHashMap.containsKey(ComboBoxSelectorMap.getSelectedItem()))
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Данная карта отсутсвует", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||
|
||
return;
|
||
}
|
||
|
||
_mapsCollection.AddMap(TextBoxNewMapName.getText(), _mapsHashMap.get(ComboBoxSelectorMap.getSelectedItem().toString()));
|
||
ReloadMaps();
|
||
}
|
||
});
|
||
|
||
//удалить карту
|
||
ButtonDeleteMap.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if(JOptionPane.showConfirmDialog(null, "Удалить карту" + ListBoxMaps.getSelectedValue().toString() + "?",
|
||
"Удаление", JOptionPane.YES_NO_OPTION) == 0)
|
||
{
|
||
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
|
||
ReloadMaps();
|
||
}
|
||
}
|
||
});
|
||
|
||
//список карт
|
||
ListBoxMaps.addListSelectionListener(new ListSelectionListener() {
|
||
@Override
|
||
public void valueChanged(ListSelectionEvent e) {
|
||
if(ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
|
||
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||
}
|
||
});
|
||
|
||
//Просмотр удалённых самолётов
|
||
ButtonShowDelete.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
|
||
DrawningObjectPlane plane = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetPlaneInDelete();
|
||
|
||
if(plane == null)
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Список пуст", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||
|
||
return;
|
||
}
|
||
|
||
FormPlane form = new FormPlane(plane);
|
||
form.setSize(700, 500);
|
||
form.setVisible(true);
|
||
}
|
||
});
|
||
|
||
//добавление самолёта с использованием делегата
|
||
ButtonAddPlane.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
FormPlaneConfig form = new FormPlaneConfig();
|
||
|
||
form.AddEvent(newPlane ->{
|
||
if(ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if(newPlane != null)
|
||
{
|
||
DrawningObjectPlane plane = new DrawningObjectPlane(newPlane);
|
||
|
||
if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Add(plane) != -1)
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||
}
|
||
}
|
||
});
|
||
form.setSize(975, 360);
|
||
form.setVisible(true);
|
||
}
|
||
});
|
||
|
||
//удаление самолёта
|
||
ButtonRemovePlane.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (MaskedTextBoxPosition.getText().isEmpty())
|
||
{
|
||
return;
|
||
}
|
||
|
||
int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",
|
||
JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
|
||
|
||
if(result == JOptionPane.NO_OPTION)
|
||
{
|
||
return;
|
||
}
|
||
|
||
int pos = Integer.parseInt(MaskedTextBoxPosition.getText());
|
||
|
||
if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Delete(pos) != null)
|
||
{
|
||
JOptionPane.showMessageDialog(null,"Объект удалён");
|
||
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(null,"Не удалось удалить объект");
|
||
}
|
||
}
|
||
});
|
||
|
||
//показ хранилища
|
||
ButtonShowStorage.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(_mapsCollection == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||
}
|
||
});
|
||
|
||
//показ карты
|
||
ButtonShowOnMap.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (_mapsCollection == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap());
|
||
}
|
||
});
|
||
|
||
ButtonUp.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(_mapsCollection== null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
PictureBoxPlane.removeAll();
|
||
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Up))));
|
||
|
||
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
PictureBoxPlane.revalidate();
|
||
PictureBoxPlane.repaint();
|
||
}
|
||
});
|
||
|
||
ButtonLeft.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(_mapsCollection == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
PictureBoxPlane.removeAll();
|
||
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Left))));
|
||
|
||
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
PictureBoxPlane.revalidate();
|
||
PictureBoxPlane.repaint();
|
||
}
|
||
});
|
||
|
||
ButtonDown.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(_mapsCollection == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
PictureBoxPlane.removeAll();
|
||
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Down))));
|
||
|
||
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
PictureBoxPlane.revalidate();
|
||
PictureBoxPlane.repaint();
|
||
}
|
||
});
|
||
|
||
ButtonRight.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(_mapsCollection == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
PictureBoxPlane.removeAll();
|
||
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Right))));
|
||
|
||
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
PictureBoxPlane.revalidate();
|
||
PictureBoxPlane.repaint();
|
||
}
|
||
});
|
||
|
||
MaskedTextBoxPosition.addKeyListener(new KeyAdapter() {
|
||
@Override
|
||
public void keyTyped(KeyEvent e) {
|
||
char c = e.getKeyChar();
|
||
|
||
if ((c < '0') || (c > '9')) {
|
||
e.consume();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
private void createUIComponents()
|
||
{
|
||
DefaultListModel<String> defListMod = new DefaultListModel<String>();
|
||
ListBoxMaps = new JList(defListMod);
|
||
}
|
||
}
|