198 lines
7.5 KiB
Java
198 lines
7.5 KiB
Java
import javax.swing.*;
|
|
import javax.swing.text.MaskFormatter;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.image.BufferedImage;
|
|
import java.text.ParseException;
|
|
|
|
public class FormMapWithSetBattleship extends JComponent {
|
|
private BufferedImage bufferedImage = null;
|
|
|
|
private MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap> _mapBattleshipsCollectionGeneric;
|
|
public static void main(String[] args) {
|
|
FormMapWithSetBattleship formMap = new FormMapWithSetBattleship();
|
|
}
|
|
public FormMapWithSetBattleship(){
|
|
JFrame form = new JFrame("FormMapWithSetBattleship");
|
|
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
form.setSize(750, 500);
|
|
form.setLocationRelativeTo(null);
|
|
|
|
Panel statusPanel = new Panel();
|
|
statusPanel.setBackground(Color.WHITE);
|
|
statusPanel.setLayout(new GridLayout(0, 1, 20, 20));
|
|
setLayout(new BorderLayout());
|
|
add(statusPanel, BorderLayout.EAST);
|
|
String[] maps = {
|
|
"Простая карта",
|
|
"Морская карта",
|
|
};
|
|
JComboBox mapSelectComboBox = new JComboBox(maps);
|
|
mapSelectComboBox.setEditable(true);
|
|
mapSelectComboBox.addActionListener(e -> {
|
|
AbstractMap map = null;
|
|
String item = (String)mapSelectComboBox.getSelectedItem();
|
|
switch (item) {
|
|
case "Простая карта":
|
|
map = new SimpleMap();
|
|
break;
|
|
case "Морская карта":
|
|
map = new SeaMap();
|
|
break;
|
|
}
|
|
if (map != null)
|
|
{
|
|
_mapBattleshipsCollectionGeneric = new MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap>
|
|
(1000, 700, map);
|
|
}
|
|
else
|
|
{
|
|
_mapBattleshipsCollectionGeneric = null;
|
|
}
|
|
});
|
|
statusPanel.add(mapSelectComboBox);
|
|
|
|
|
|
JButton addBattleshipButton = new JButton("Добавить корабль");
|
|
addBattleshipButton.addActionListener(e -> {
|
|
|
|
if (_mapBattleshipsCollectionGeneric == null)
|
|
{
|
|
return;
|
|
}
|
|
JDialog dialog = new JDialog(form, "Диалого", true);
|
|
FormBattleship formShip = new FormBattleship(dialog);
|
|
dialog.setSize(800, 500);
|
|
dialog.setContentPane(formShip);
|
|
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
|
dialog.setVisible(true);
|
|
DrawningObjectBattleship battleship = new DrawningObjectBattleship(formShip.GetSelectedBattleship());
|
|
if (_mapBattleshipsCollectionGeneric.Add(battleship) != -1) {
|
|
JOptionPane.showMessageDialog(form, "Объект добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
|
|
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
|
|
repaint();
|
|
}
|
|
else {
|
|
JOptionPane.showMessageDialog(form, "Объект не добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
|
|
}
|
|
});
|
|
statusPanel.add(addBattleshipButton);
|
|
|
|
JFormattedTextField maskedTextFieldPosition = null;
|
|
try {
|
|
MaskFormatter positionMask = new MaskFormatter("##");
|
|
maskedTextFieldPosition = new JFormattedTextField(positionMask);
|
|
statusPanel.add(maskedTextFieldPosition);
|
|
}
|
|
catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
JButton deleteBattleshipButton = new JButton("Удалить корабль");
|
|
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
|
|
deleteBattleshipButton.addActionListener(e -> {
|
|
if ((String)mapSelectComboBox.getSelectedItem() == null) {
|
|
return;
|
|
}
|
|
if (finalMaskedTextFieldPosition == null) {
|
|
return;
|
|
}
|
|
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
|
|
if (_mapBattleshipsCollectionGeneric.Subtraction(position) != null) {
|
|
JOptionPane.showMessageDialog(form, "Объект удален", "Удаление", JOptionPane.OK_CANCEL_OPTION);
|
|
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
|
|
repaint();
|
|
}
|
|
else{
|
|
JOptionPane.showMessageDialog(form, "Объект не удален", "Удаление", JOptionPane.OK_CANCEL_OPTION);
|
|
}
|
|
});
|
|
statusPanel.add(deleteBattleshipButton);
|
|
JButton showStorageButton = new JButton("Хранилище");
|
|
showStorageButton.addActionListener(e -> {
|
|
|
|
if (_mapBattleshipsCollectionGeneric == null)
|
|
{
|
|
return;
|
|
}
|
|
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
|
|
repaint();
|
|
});
|
|
statusPanel.add(showStorageButton);
|
|
|
|
JButton showOnMapButton = new JButton("Карта");
|
|
showOnMapButton.addActionListener(e -> {
|
|
|
|
if (_mapBattleshipsCollectionGeneric == null)
|
|
{
|
|
return;
|
|
}
|
|
bufferedImage = _mapBattleshipsCollectionGeneric.ShowOnMap();
|
|
});
|
|
statusPanel.add(showOnMapButton);
|
|
|
|
ActionListener moveButtonListener = new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if (_mapBattleshipsCollectionGeneric == null)
|
|
{
|
|
return;
|
|
}
|
|
String name = e.getActionCommand();
|
|
Direction dir = Direction.None;
|
|
switch (name)
|
|
{
|
|
case "↑":
|
|
dir = Direction.Up;
|
|
break;
|
|
case "↓":
|
|
dir = Direction.Down;
|
|
break;
|
|
case "←":
|
|
dir = Direction.Left;
|
|
break;
|
|
case "→":
|
|
dir = Direction.Right;
|
|
break;
|
|
}
|
|
bufferedImage = _mapBattleshipsCollectionGeneric.MoveObject(dir);
|
|
}
|
|
};
|
|
|
|
//Кнопки управления
|
|
JButton moveDownButton = new JButton("↓");
|
|
moveDownButton.addActionListener(moveButtonListener);
|
|
|
|
JButton moveUpButton = new JButton("↑");
|
|
moveUpButton.addActionListener(moveButtonListener);
|
|
|
|
JButton moveLeftButton = new JButton("←");
|
|
moveLeftButton.addActionListener(moveButtonListener);
|
|
|
|
JButton moveRightButton = new JButton("→");
|
|
moveRightButton.addActionListener(moveButtonListener);
|
|
|
|
JButton showGalleryButton = new JButton("Галерея");
|
|
showGalleryButton.addActionListener(e -> {
|
|
new FormParameterClass();
|
|
});
|
|
statusPanel.add(showGalleryButton);
|
|
statusPanel.add(moveUpButton);
|
|
statusPanel.add(moveDownButton);
|
|
statusPanel.add(moveLeftButton);
|
|
statusPanel.add(moveRightButton);
|
|
form.getContentPane().add(this);
|
|
form.setVisible(true);
|
|
}
|
|
|
|
@Override
|
|
protected void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
Graphics2D g2 = (Graphics2D)g;
|
|
if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,600,500,null);
|
|
super.repaint();
|
|
}
|
|
}
|
|
|