297 lines
13 KiB
Java
297 lines
13 KiB
Java
import javax.imageio.ImageIO;
|
||
import javax.swing.*;
|
||
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;
|
||
import javax.swing.event.ListSelectionEvent;
|
||
import javax.swing.event.ListSelectionListener;
|
||
public class FormMapWithSetShipsGeneric extends JFrame{
|
||
public JPanel Mainpanel;
|
||
private JPanel pictureBoxShip;
|
||
private JPanel GroupBoxTools;
|
||
private JComboBox ComboBoxSelectorMap;
|
||
private JButton ButtonAddShip;
|
||
private JButton ButtonDeleteShip;
|
||
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 final int picWidth=600;
|
||
|
||
private final int picHeight=400;
|
||
private JButton ButtonAddMap;
|
||
private JList ListBoxMaps;
|
||
private JButton ButtonDeleteMap;
|
||
private JPanel GroupBoxMaps;
|
||
private JButton ButtonCheckDel;
|
||
private MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap> _mapShipsCollectionGeneric;
|
||
private final HashMap<String,AbstractMap> _mapsDict = new HashMap<>(){{
|
||
put("Простая карта",new SimpleMap());
|
||
put("Карта острова",new IslandsMap());
|
||
put("Карта скалы",new RocksMap());
|
||
}};
|
||
private final MapsCollection _mapsCollection;
|
||
public void UpdateWindow(BufferedImage bmp)
|
||
{
|
||
pictureBoxShip.removeAll();
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(bmp));
|
||
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
pictureBoxShip.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);
|
||
}
|
||
}
|
||
public FormMapWithSetShipsGeneric()
|
||
{
|
||
_mapsCollection = new MapsCollection(picWidth, picHeight);
|
||
ComboBoxSelectorMap.removeAllItems();
|
||
for (String elem : _mapsDict.keySet()) {
|
||
ComboBoxSelectorMap.addItem(elem);
|
||
}
|
||
try {
|
||
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
|
||
ButtonUp.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(FormShip.class.getResource("Images/2.png"));
|
||
ButtonDown.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(FormShip.class.getResource("Images/1.png"));
|
||
ButtonLeft.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(FormShip.class.getResource("Images/3.png"));
|
||
ButtonRight.setIcon(new ImageIcon(img));
|
||
} catch (Exception ex) {
|
||
System.out.println(ex);
|
||
}
|
||
|
||
ButtonAddShip.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
FormShipConfig formShipConfig = new FormShipConfig();
|
||
formShipConfig.AddEvent(newShip -> {
|
||
if (ListBoxMaps.getSelectedIndex() == -1) {
|
||
return;
|
||
}
|
||
if (newShip != null) {
|
||
DrawingObjectShip ship = new DrawingObjectShip(newShip);
|
||
if (_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship) != -1) {
|
||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||
} else {
|
||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||
}
|
||
}
|
||
|
||
});
|
||
formShipConfig.setSize(850, 300);
|
||
formShipConfig.setVisible(true);
|
||
}
|
||
});
|
||
ListBoxMaps.addListSelectionListener(new ListSelectionListener() {
|
||
@Override
|
||
public void valueChanged(ListSelectionEvent e) {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
return;
|
||
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||
}
|
||
});
|
||
ButtonDeleteShip.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 (ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||
}
|
||
});
|
||
ButtonShowMap.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap());
|
||
}
|
||
});
|
||
ButtonUp.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
pictureBoxShip.removeAll();
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up)));
|
||
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
pictureBoxShip.revalidate();
|
||
pictureBoxShip.repaint();
|
||
}
|
||
});
|
||
ButtonDown.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
pictureBoxShip.removeAll();
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down)));
|
||
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
pictureBoxShip.revalidate();
|
||
pictureBoxShip.repaint();
|
||
}
|
||
});
|
||
ButtonRight.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
pictureBoxShip.removeAll();
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right)));
|
||
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
pictureBoxShip.revalidate();
|
||
pictureBoxShip.repaint();
|
||
}
|
||
});
|
||
ButtonLeft.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
pictureBoxShip.removeAll();
|
||
JLabel imageWithMapAndObject = new JLabel();
|
||
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left)));
|
||
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||
pictureBoxShip.revalidate();
|
||
pictureBoxShip.repaint();
|
||
|
||
}
|
||
});
|
||
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();
|
||
}
|
||
}
|
||
});
|
||
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(!_mapsDict.containsKey(ComboBoxSelectorMap.getSelectedItem()))
|
||
{
|
||
JOptionPane.showMessageDialog(null,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
_mapsCollection.AddMap(textBoxNewMapName.getText(),_mapsDict.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();
|
||
}
|
||
}
|
||
});
|
||
ButtonCheckDel.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
DrawingObjectShip ship=_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).GetShipsDeleted();
|
||
if(ship==null)
|
||
{
|
||
JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE);
|
||
return;
|
||
}
|
||
FormShip formShip=new FormShip(ship);
|
||
formShip.setSize(1000,800);
|
||
formShip.setVisible(true);
|
||
}
|
||
});
|
||
}
|
||
|
||
private void createUIComponents() {
|
||
DefaultListModel<String> dlm = new DefaultListModel<String>();
|
||
ListBoxMaps = new JList(dlm);
|
||
}
|
||
}
|