PIbd-21_Markov_D.P._Contain.../FormMapWithSetShipsGeneric.java

227 lines
9.4 KiB
Java
Raw Permalink Normal View History

2022-12-08 21:14:01 +04:00
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;
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 MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap> _mapShipsCollectionGeneric;
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();
}
public FormMapWithSetShipsGeneric()
{
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);
}
ComboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AbstractMap map = null;
ComboBoxSelectorMap = (JComboBox)e.getSource();
String item = (String)ComboBoxSelectorMap.getSelectedItem();
switch (item) {
case "Простая карта": {
map = new SimpleMap();
break;
}
case "Карта острова": {
map = new IslandsMap();
break;
}
case "Карта скалы": {
map = new RocksMap();
break;
}
}
if(map!=null)
{
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap>(pictureBoxShip.getWidth(),pictureBoxShip.getHeight(),map);
}
else
{
_mapShipsCollectionGeneric=null;
}
}
});
ButtonAddShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapShipsCollectionGeneric==null)
{
return;
}
FormShip form = new FormShip();
form.setSize(1000,700);
form.setVisible(true);
DrawingObjectShip ship = new DrawingObjectShip(form.GetSelectedShip());
if(_mapShipsCollectionGeneric.Add(ship)!=-1)
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
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(_mapShipsCollectionGeneric.Delete(pos)!=null)
{
JOptionPane.showMessageDialog(null, "Объект удален");
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
}
});
ButtonShowStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
}
});
ButtonShowMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
UpdateWindow(_mapShipsCollectionGeneric.ShowOnMap());
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Up)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Down)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Right)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.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();
}
}
});
}
}