310 lines
14 KiB
Java
310 lines
14 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 int picWidth=600;
|
||
private int picHeight=400;
|
||
private JButton ButtonAddMap;
|
||
private JList ListBoxMaps;
|
||
private JButton ButtonDeleteMap;
|
||
private JPanel GroupBoxMaps;
|
||
private JButton ButtonCheckDel;
|
||
private MapWithSetShipGeneric<DrawningObjectShip,AbstractMap> _mapShipsCollectionGeneric;
|
||
private final HashMap<String,AbstractMap> _mapsDict = new HashMap<>(){{
|
||
put("Простая карта",new SimpleMap());
|
||
put("Вторая карта",new SecondMap());
|
||
put("Последняя карта",new LastMap());
|
||
}};
|
||
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()
|
||
{
|
||
//picWidth = pictureBoxShip.getWidth();
|
||
//picHeight = pictureBoxShip.getHeight();
|
||
_mapsCollection = new MapsCollection(picWidth, picHeight);
|
||
ComboBoxSelectorMap.removeAllItems();
|
||
for (String elem : _mapsDict.keySet()) {
|
||
ComboBoxSelectorMap.addItem(elem);
|
||
}
|
||
|
||
try {
|
||
Image img = ImageIO.read(FormShip.class.getResource("/Images/totop.png"));
|
||
buttonUp.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(FormShip.class.getResource("/Images/toleft.png"));
|
||
buttonLeft.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(FormShip.class.getResource("/Images/todown.png"));
|
||
buttonDown.setIcon(new ImageIcon(img));
|
||
img = ImageIO.read(FormShip.class.getResource("/Images/toright.png"));
|
||
buttonRight.setIcon(new ImageIcon(img));
|
||
} catch (Exception ex) {
|
||
System.out.println(ex);
|
||
}
|
||
|
||
ButtonAddShip.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (ListBoxMaps.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
|
||
FormShip dialog = new FormShip();
|
||
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.GetSelectedShip() == null) return;
|
||
|
||
DrawningObjectShip ship = new DrawningObjectShip(dialog.GetSelectedShip());
|
||
|
||
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship) != -1)
|
||
{
|
||
JOptionPane.showMessageDialog(null,"Объект добавлен");
|
||
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||
}
|
||
}
|
||
});
|
||
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;
|
||
}
|
||
DrawningObjectShip ship = _mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).GetShipsDeleted();
|
||
if(ship == null)
|
||
{
|
||
JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE);
|
||
return;
|
||
}
|
||
FormShip dialog = new FormShip();
|
||
dialog.setShipIn(ship);
|
||
dialog.setSize(800, 600);
|
||
dialog.setLocation(500, 200);
|
||
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
|
||
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||
dialog.setVisible(true);
|
||
}
|
||
});
|
||
}
|
||
|
||
private void createUIComponents() {
|
||
DefaultListModel<String> dlm = new DefaultListModel<String>();
|
||
ListBoxMaps = new JList(dlm);
|
||
}
|
||
}
|