PIbd-22_Bondarenko_M.S._War.../FormMapWithSetShip.java
Макс Бондаренко 30e3d67aeb готово 3
2022-12-15 22:28:43 +04:00

239 lines
9.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormMapWithSetShip extends JFrame {
private MapWithSetShipGeneric<DrawningObjectShip, AbstractMap> _mapShipCollectionGeneric;
public boolean ShipOnMap = false;
public JPanel Mainpanel;
private JButton buttonAddShip;
private JComboBox comboBoxSelectorMap;
private JTextField maskedTextBoxPosition;
private JButton buttonRemoveShip;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonDown;
private JPanel GraphicsOutput;
private JPanel groupBox;
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(false);
frame.setBounds(300, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
JFrame jFrame = getFrame();
private void ButtonMove_Click(String name)
{
if (_mapShipCollectionGeneric == null || !ShipOnMap) return;
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;
}
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.MoveObject(direction)));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
}
public FormMapWithSetShip() {
comboBoxSelectorMap.addItem("Простая карта");
comboBoxSelectorMap.addItem("Вторая карта");
comboBoxSelectorMap.addItem("Последняя карта");
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);
}
comboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AbstractMap map = null;
switch (comboBoxSelectorMap.getSelectedItem().toString())
{
case "Простая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
case "Последняя карта":
map = new LastMap();
break;
}
if (map != null)
{
_mapShipCollectionGeneric = new MapWithSetShipGeneric<DrawningObjectShip, AbstractMap>(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(), map);
}
else
{
_mapShipCollectionGeneric = null;
}
}
});
buttonAddShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipCollectionGeneric == null)
{
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 (_mapShipCollectionGeneric.Add(ship) > -1)
{
JOptionPane.showMessageDialog(jFrame, "Объект добавлен");
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowSet()));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект");
}
}
});
buttonRemoveShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapShipCollectionGeneric == null) return;
String text = maskedTextBoxPosition.getText();
if(text.isEmpty()) return;
if(JOptionPane.showConfirmDialog(jFrame, "Вы действительно хотите удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return;
try {
Integer.parseInt(text);
}
catch (Exception ex){
return;
}
int pos = Integer.parseInt(text);
if (_mapShipCollectionGeneric.Delete(pos) != null)
{
JOptionPane.showMessageDialog(jFrame, "Объект удален");
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowSet()));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
}
}
});
buttonShowStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipCollectionGeneric == null) return;
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowSet()));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
ShipOnMap = false;
}
});
buttonShowOnMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipCollectionGeneric == null) return;
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowOnMap()));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
ShipOnMap = true;
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonUp");
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonLeft");
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonDown");
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonRight");
}
});
}
}