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; import java.util.HashMap; public class FormMapWithSetBattleship extends JComponent { private BufferedImage bufferedImage = null; JList listBoxMaps; Panel statusPanel; DefaultListModel mapsListModel; JComboBox mapSelectComboBox; private final HashMap _mapsDict = new HashMap<>() { { put("Простая карта", new SimpleMap()); put("Морская карта", new SeaMap()); } }; private final MapsCollection _mapsCollection; private MapWithSetBattleshipsGeneric _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); statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); statusPanel.setLayout(new GridLayout(0, 1, 20, 20)); setLayout(new BorderLayout()); add(statusPanel, BorderLayout.EAST); String[] maps = { "Простая карта", "Морская карта", }; mapSelectComboBox = new JComboBox(maps); mapSelectComboBox.setEditable(true); 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; } if (!_mapsDict.containsKey(mapSelectComboBox.getSelectedItem().toString())) { return; } _mapsCollection.AddMap(textFieldMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString())); ReloadMaps(); }); statusPanel.add(addMapButton); JButton deleteMapButton = new JButton("Удалить карту"); deleteMapButton.addActionListener(e -> { // логика удаления if (listBoxMaps.getSelectedIndex() == -1) { return; } if(listBoxMaps.getSelectedValue().toString() == null) return; _mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString()); ReloadMaps(); repaint(); }); statusPanel.add(deleteMapButton); JButton addBattleshipButton = new JButton("Добавить корабль"); addBattleshipButton.addActionListener(e -> { if (listBoxMaps.getSelectedIndex() == -1) { 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 (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Add(battleship)!= -1) { JOptionPane.showMessageDialog(form, "Объект добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION); bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).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 (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Subtraction(position) != null) { JOptionPane.showMessageDialog(form, "Объект удален", "Успех", JOptionPane.OK_CANCEL_OPTION); bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).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 (listBoxMaps.getSelectedIndex() == -1) { return; } bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap(); repaint(); }); statusPanel.add(showOnMapButton); ActionListener moveButtonListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (listBoxMaps.getSelectedIndex() == -1) { 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 = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).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(); }); JDialog dialog = new JDialog(form, "Dialog", true); statusPanel.add(showGalleryButton); 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); statusPanel.add(moveUpButton); statusPanel.add(moveDownButton); statusPanel.add(moveLeftButton); statusPanel.add(moveRightButton); form.getContentPane().add(this); form.setVisible(true); } 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(); } @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(); } }