Pibd-23_Zhelovanov_D.Y._Bat.../FormMapWithSetBattleship.java

283 lines
11 KiB
Java
Raw Normal View History

2022-12-20 21:50:09 +04:00
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;
2022-12-22 22:27:39 +04:00
import java.util.HashMap;
2022-12-20 21:50:09 +04:00
public class FormMapWithSetBattleship extends JComponent {
private BufferedImage bufferedImage = null;
2022-12-22 22:27:39 +04:00
JList listBoxMaps;
Panel statusPanel;
DefaultListModel<String> mapsListModel;
JComboBox mapSelectComboBox;
2022-12-20 21:50:09 +04:00
2022-12-22 22:27:39 +04:00
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {
{
put("Простая карта", new SimpleMap());
put("Морская карта", new SeaMap());
}
};
private final MapsCollection _mapsCollection;
2022-12-20 21:50:09 +04:00
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);
2022-12-22 22:27:39 +04:00
statusPanel = new Panel();
2022-12-20 21:50:09 +04:00
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new GridLayout(0, 1, 20, 20));
setLayout(new BorderLayout());
add(statusPanel, BorderLayout.EAST);
String[] maps = {
"Простая карта",
"Морская карта",
};
2022-12-22 22:27:39 +04:00
mapSelectComboBox = new JComboBox(maps);
2022-12-20 21:50:09 +04:00
mapSelectComboBox.setEditable(true);
2022-12-22 22:27:39 +04:00
statusPanel.add(mapSelectComboBox);
mapsListModel = new DefaultListModel<>();
_mapsCollection = new MapsCollection(800, 500);
mapSelectComboBox.removeAllItems();
for (var elem : _mapsDict.keySet())
{
mapSelectComboBox.addItem(elem);
}
JScrollPane listMapPane = new JScrollPane();
listBoxMaps = new JList(mapsListModel);
listBoxMaps.addListSelectionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
});
statusPanel.add(listBoxMaps);
listMapPane.setViewportView(listBoxMaps);
listBoxMaps.setLayoutOrientation(JList.VERTICAL);
statusPanel.add(listMapPane);
TextField textFieldMapName = new TextField();
statusPanel.add(textFieldMapName);
statusPanel.add(mapSelectComboBox);
JButton addMapButton = new JButton("Добавить карту");
addMapButton.addActionListener(e -> {
if (mapSelectComboBox.getSelectedIndex() == -1 || textFieldMapName.getText() == null)
{
return;
2022-12-20 21:50:09 +04:00
}
2022-12-22 22:27:39 +04:00
if (!_mapsDict.containsKey(mapSelectComboBox.getSelectedItem().toString()))
2022-12-20 21:50:09 +04:00
{
2022-12-22 22:27:39 +04:00
return;
2022-12-20 21:50:09 +04:00
}
2022-12-22 22:27:39 +04:00
_mapsCollection.AddMap(textFieldMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
ReloadMaps();
});
statusPanel.add(addMapButton);
JButton deleteMapButton = new JButton("Удалить карту");
deleteMapButton.addActionListener(e -> {
// логика удаления
if (listBoxMaps.getSelectedIndex() == -1)
2022-12-20 21:50:09 +04:00
{
2022-12-22 22:27:39 +04:00
return;
2022-12-20 21:50:09 +04:00
}
2022-12-22 22:27:39 +04:00
if(listBoxMaps.getSelectedValue().toString() == null) return;
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
ReloadMaps();
repaint();
2022-12-20 21:50:09 +04:00
});
2022-12-22 22:27:39 +04:00
statusPanel.add(deleteMapButton);
2022-12-20 21:50:09 +04:00
2022-12-22 22:27:39 +04:00
JButton addBattleshipButton = new JButton("Добавить корабль");
addBattleshipButton.addActionListener(e -> {
2022-12-20 21:50:09 +04:00
2022-12-22 22:27:39 +04:00
if (listBoxMaps.getSelectedIndex() == -1)
2022-12-20 21:50:09 +04:00
{
return;
}
2022-12-22 22:27:39 +04:00
JDialog dialog = new JDialog(form, "Диалог", true);
2022-12-20 21:50:09 +04:00
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());
2022-12-22 22:27:39 +04:00
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Add(battleship)!= -1) {
2022-12-20 21:50:09 +04:00
JOptionPane.showMessageDialog(form, "Объект добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
2022-12-22 22:27:39 +04:00
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
2022-12-20 21:50:09 +04:00
repaint();
}
else {
JOptionPane.showMessageDialog(form, "Объект не добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
}
});
2022-12-22 22:27:39 +04:00
statusPanel.add(addBattleshipButton);
2022-12-20 21:50:09 +04:00
JFormattedTextField maskedTextFieldPosition = null;
try {
MaskFormatter positionMask = new MaskFormatter("##");
maskedTextFieldPosition = new JFormattedTextField(positionMask);
statusPanel.add(maskedTextFieldPosition);
}
catch (ParseException e) {
e.printStackTrace();
}
2022-12-22 22:27:39 +04:00
JButton deleteBattleshipButton = new JButton("Удалить корабль");
2022-12-20 21:50:09 +04:00
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
2022-12-22 22:27:39 +04:00
deleteBattleshipButton.addActionListener(e -> {
2022-12-20 21:50:09 +04:00
if ((String)mapSelectComboBox.getSelectedItem() == null) {
return;
}
if (finalMaskedTextFieldPosition == null) {
return;
}
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
2022-12-22 22:27:39 +04:00
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Subtraction(position) != null) {
JOptionPane.showMessageDialog(form, "Объект удален", "Успех", JOptionPane.OK_CANCEL_OPTION);
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
2022-12-20 21:50:09 +04:00
repaint();
}
else{
JOptionPane.showMessageDialog(form, "Объект не удален", "Удаление", JOptionPane.OK_CANCEL_OPTION);
}
});
2022-12-22 22:27:39 +04:00
statusPanel.add(deleteBattleshipButton);
2022-12-20 21:50:09 +04:00
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 -> {
2022-12-22 22:27:39 +04:00
if (listBoxMaps.getSelectedIndex() == -1)
2022-12-20 21:50:09 +04:00
{
return;
}
2022-12-22 22:27:39 +04:00
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
repaint();
2022-12-20 21:50:09 +04:00
});
statusPanel.add(showOnMapButton);
ActionListener moveButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2022-12-22 22:27:39 +04:00
if (listBoxMaps.getSelectedIndex() == -1)
2022-12-20 21:50:09 +04:00
{
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;
}
2022-12-22 22:27:39 +04:00
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).MoveObject(dir);
2022-12-20 21:50:09 +04:00
}
};
//Кнопки управления
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();
});
2022-12-22 22:27:39 +04:00
JDialog dialog = new JDialog(form, "Dialog", true);
2022-12-20 21:50:09 +04:00
statusPanel.add(showGalleryButton);
2022-12-22 22:27:39 +04:00
JButton showDeleteObjButton = new JButton("Удаленные корабли");
showDeleteObjButton.addActionListener(e -> {
DrawningObjectBattleship battleship = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).GetDeleted();
if (battleship == null) {
JOptionPane.showMessageDialog(null, "Больше нет");
}
if (battleship != null) {
FormBattleship formBattleship = new FormBattleship(dialog);
formBattleship.SetBattleship(battleship);
dialog.setSize(800, 500);
dialog.setContentPane(formBattleship);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
});
statusPanel.add(showDeleteObjButton);
2022-12-20 21:50:09 +04:00
statusPanel.add(moveUpButton);
statusPanel.add(moveDownButton);
statusPanel.add(moveLeftButton);
statusPanel.add(moveRightButton);
form.getContentPane().add(this);
form.setVisible(true);
}
2022-12-22 22:27:39 +04:00
private void ReloadMaps(){
int index = listBoxMaps.getSelectedIndex();
listBoxMaps.removeAll();
mapsListModel.removeAllElements();
for (int i = 0; i < _mapsCollection.Keys().size(); i++){
mapsListModel.addElement(_mapsCollection.Keys().get(i));
}
if (mapsListModel.size() > 0 && (index == -1 || index >= mapsListModel.size())){
listBoxMaps.setSelectedIndex(0);
}
else if (mapsListModel.size() > 0 && index > -1 && index < mapsListModel.size())
{
listBoxMaps.setSelectedIndex(index);
}
listBoxMaps.setModel(mapsListModel);
statusPanel.repaint();
}
2022-12-20 21:50:09 +04:00
@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();
}
}