From de6f7d3342eab5947987ec40df35f9cb06649fb9 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Sun, 13 Nov 2022 17:26:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=D1=87=D0=B5=D1=82=D1=8B=D1=80=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawingObjectAirBomber.java | 4 + .../src/AirBomberPackage/JFrameAirBomber.java | 11 +- .../JFrameMapWithSetAirBombers.form | 116 +++++++-- .../JFrameMapWithSetAirBombers.java | 237 +++++++++++++----- .../MapWithSetAirBombersGeneric.java | 12 +- .../src/AirBomberPackage/MapsCollection.java | 79 ++++++ .../SetAirBombersGeneric.java | 57 ++--- 7 files changed, 391 insertions(+), 125 deletions(-) create mode 100644 AirBomber/src/AirBomberPackage/MapsCollection.java diff --git a/AirBomber/src/AirBomberPackage/DrawingObjectAirBomber.java b/AirBomber/src/AirBomberPackage/DrawingObjectAirBomber.java index c27eccb..66ee1b7 100644 --- a/AirBomber/src/AirBomberPackage/DrawingObjectAirBomber.java +++ b/AirBomber/src/AirBomberPackage/DrawingObjectAirBomber.java @@ -40,6 +40,10 @@ public class DrawingObjectAirBomber implements IDrawingObject { _airBomber.SetPosition(x, y, width, height); } + public DrawingAirBomber getAirBomber(){ + return _airBomber; + } + @Override public void DrawingObject(Graphics2D g) { diff --git a/AirBomber/src/AirBomberPackage/JFrameAirBomber.java b/AirBomber/src/AirBomberPackage/JFrameAirBomber.java index ea18373..bea9ead 100644 --- a/AirBomber/src/AirBomberPackage/JFrameAirBomber.java +++ b/AirBomber/src/AirBomberPackage/JFrameAirBomber.java @@ -5,7 +5,6 @@ package AirBomberPackage; import java.awt.*; import javax.swing.*; -import javax.swing.JOptionPane; import java.util.Random; /** @@ -22,6 +21,14 @@ public class JFrameAirBomber extends javax.swing.JDialog { initComponents(); } + public JFrameAirBomber(JFrame parent, DrawingObjectAirBomber airBomber) { + super(parent, "Бомбардировщик", true); + initComponents(); + _airBomber = airBomber.getAirBomber(); + SetData(); + Draw(); + } + private DrawingAirBomber _airBomber; private EnginesType enginesType = EnginesType.RECTANGLE; private DrawingAirBomber _airBomberResult; @@ -327,7 +334,7 @@ public class JFrameAirBomber extends javax.swing.JDialog { /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { - new JFrameDopGenerics().setVisible(true); + new JFrameMapWithSetAirBombers().setVisible(true); } }); } diff --git a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form index 2abbdcc..6a13a04 100644 --- a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form +++ b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form @@ -25,19 +25,29 @@ - + - - - - - - - + + + + + + + + + + + + + + + + + + - @@ -65,18 +75,30 @@ + + + + - + + + + + + + + + - + - + - + - + - + @@ -99,17 +121,9 @@ - - - - - + - - - - @@ -243,5 +257,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java index 8d153de..c2d83d9 100644 --- a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java +++ b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java @@ -4,7 +4,11 @@ */ package AirBomberPackage; import java.awt.*; +import javax.swing.DefaultListModel; +import java.util.HashMap; +import java.util.LinkedList; import javax.swing.*; + /** * * @author Андрей @@ -16,8 +20,22 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { */ public JFrameMapWithSetAirBombers() { initComponents(); + _mapsDict.put("Простая карта", new SimpleMap()); + _mapsDict.put("Городская карта", new CityMap()); + _mapsDict.put("Линейная карта", new LineMap()); + _mapsCollection = new MapsCollection(airBomberCanvas.getWidth(),airBomberCanvas.getHeight()); + DefaultComboBoxModel defaultcombModel = new DefaultComboBoxModel(); + comboBoxSelectorMap.setModel(defaultcombModel); + for (var elem : _mapsDict.keySet()){ + comboBoxSelectorMap.addItem(elem); + } + comboBoxSelectorMap.setSelectedIndex(-1); } - private MapWithSetAirBombersGeneric _mapAirBombersCollectionGeneric; + private HashMap _mapsDict = new HashMap<>(); + + private MapsCollection _mapsCollection; + + private LinkedList deletedAirBombers = new LinkedList<>(); /** * This method is called from within the constructor to initialize the form. @@ -40,17 +58,16 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { rightButton = new javax.swing.JButton(); upButton = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); + jLabel2 = new javax.swing.JLabel(); + textBoxNewMapName = new javax.swing.JTextField(); + buttonAddMap = new javax.swing.JButton(); + jScrollPane1 = new javax.swing.JScrollPane(); + listBoxMaps = new javax.swing.JList<>(); + buttonDeleteMap = new javax.swing.JButton(); + buttonShowDeleted = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); - comboBoxSelectorMap.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Простая карта", "Городская карта", "Линейная карта" })); - comboBoxSelectorMap.setSelectedIndex(-1); - comboBoxSelectorMap.addItemListener(new java.awt.event.ItemListener() { - public void itemStateChanged(java.awt.event.ItemEvent evt) { - comboBoxSelectorMapItemStateChanged(evt); - } - }); - buttonAddAirBomber.setText("Добавить бомбардировщик"); buttonAddAirBomber.setToolTipText(""); buttonAddAirBomber.addActionListener(new java.awt.event.ActionListener() { @@ -134,24 +151,63 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { jLabel1.setText("Инструменты"); + jLabel2.setText("Карты"); + + buttonAddMap.setText("Добавить карту"); + buttonAddMap.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonAddMapActionPerformed(evt); + } + }); + + listBoxMaps.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); + listBoxMaps.addListSelectionListener(new javax.swing.event.ListSelectionListener() { + public void valueChanged(javax.swing.event.ListSelectionEvent evt) { + listBoxMapsValueChanged(evt); + } + }); + jScrollPane1.setViewportView(listBoxMaps); + + buttonDeleteMap.setText("Удалить карту"); + buttonDeleteMap.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonDeleteMapActionPerformed(evt); + } + }); + + buttonShowDeleted.setText("Показать удалённый"); + buttonShowDeleted.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonShowDeletedActionPerformed(evt); + } + }); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(airBomberCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 664, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(comboBoxSelectorMap, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(buttonAddAirBomber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(maskedTextBoxPosition) - .addComponent(buttonRemoveAirBomber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(buttonShowStorage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(buttonShowOnMap, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - .addComponent(jLabel1)) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(buttonDeleteMap, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(buttonAddAirBomber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(maskedTextBoxPosition) + .addComponent(buttonRemoveAirBomber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(buttonShowStorage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(buttonShowOnMap, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(buttonAddMap, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(comboBoxSelectorMap, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(textBoxNewMapName, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(buttonShowDeleted, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel1))) .addGap(23, 23, 23)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(leftButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) @@ -171,18 +227,30 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { .addGap(15, 15, 15) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jLabel2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(textBoxNewMapName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(comboBoxSelectorMap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(60, 60, 60) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(buttonAddMap, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(buttonDeleteMap, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE) + .addComponent(buttonShowDeleted, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(buttonAddAirBomber) - .addGap(38, 38, 38) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(maskedTextBoxPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(18, 18, 18) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(buttonRemoveAirBomber) - .addGap(96, 96, 96) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(buttonShowStorage) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(buttonShowOnMap) - .addGap(108, 108, 108) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) @@ -213,50 +281,22 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { dir = Direction.RIGHT; break; } - airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.MoveObject(dir), 0, 0, null); + airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).MoveObject(dir), 0, 0, null); }//GEN-LAST:event_moveButtonActionPerformed - private void comboBoxSelectorMapItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboBoxSelectorMapItemStateChanged - AbstractMap map = null; - switch (comboBoxSelectorMap.getSelectedIndex()) - { - case 0: - map = new SimpleMap(); - break; - case 1: - map = new CityMap(); - break; - case 2: - map = new LineMap(); - break; - } - if (map != null) - { - _mapAirBombersCollectionGeneric = new MapWithSetAirBombersGeneric( - airBomberCanvas.getWidth(), airBomberCanvas.getHeight(), map); - airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowSet(), 0, 0, null); - } - else - { - _mapAirBombersCollectionGeneric = null; - } - }//GEN-LAST:event_comboBoxSelectorMapItemStateChanged - private void buttonAddAirBomberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddAirBomberActionPerformed - if (_mapAirBombersCollectionGeneric == null) - { + if (listBoxMaps.getSelectedIndex() == -1){ return; } - JFrameAirBomber form = new JFrameAirBomber(this); DrawingAirBomber selectedAirBomber = form.run(); if (selectedAirBomber != null) { DrawingObjectAirBomber objectAirBomber = new DrawingObjectAirBomber(selectedAirBomber); - if (_mapAirBombersCollectionGeneric.add(objectAirBomber) != -1) + if (_mapsCollection.Get(listBoxMaps.getSelectedValue()).add(objectAirBomber) != -1) { JOptionPane.showMessageDialog(this, "Объект добавлен"); - airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowSet(), 0, 0, null); + airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null); } else { @@ -266,6 +306,9 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { }//GEN-LAST:event_buttonAddAirBomberActionPerformed private void buttonRemoveAirBomberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRemoveAirBomberActionPerformed + if (listBoxMaps.getSelectedIndex() == -1){ + return; + } if (maskedTextBoxPosition.getText().isBlank() || maskedTextBoxPosition.getText().isEmpty()) { return; @@ -276,10 +319,12 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { return; } int pos = Integer.parseInt(maskedTextBoxPosition.getText()); - if (_mapAirBombersCollectionGeneric.remove(pos) != null) + DrawingObjectAirBomber deletedAirBomber = _mapsCollection.Get(listBoxMaps.getSelectedValue()).remove(pos); + if (deletedAirBomber != null) { + deletedAirBombers.add(deletedAirBomber); JOptionPane.showMessageDialog(null,"Объект удален"); - airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowSet(), 0, 0, null); + airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null); } else { @@ -288,21 +333,78 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { }//GEN-LAST:event_buttonRemoveAirBomberActionPerformed private void buttonShowStorageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonShowStorageActionPerformed - if (_mapAirBombersCollectionGeneric == null) - { + if (listBoxMaps.getSelectedIndex() == -1){ return; } - airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowSet(), 0, 0, null); + airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null); }//GEN-LAST:event_buttonShowStorageActionPerformed private void buttonShowOnMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonShowOnMapActionPerformed - if (_mapAirBombersCollectionGeneric == null) - { + if (listBoxMaps.getSelectedIndex() == -1){ return; } - airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowOnMap(), 0, 0, null); + airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowOnMap(), 0, 0, null); }//GEN-LAST:event_buttonShowOnMapActionPerformed + + private void buttonAddMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddMapActionPerformed + if (comboBoxSelectorMap.getSelectedIndex() == -1 || textBoxNewMapName.getText().isBlank()){ + JOptionPane.showMessageDialog(this, "Не все данные заполнены!"); + return; + } + if (!_mapsDict.containsKey((String) comboBoxSelectorMap.getSelectedItem())){ + JOptionPane.showMessageDialog(this, "Нет такой карты!"); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.getText(), _mapsDict.get((String) comboBoxSelectorMap.getSelectedItem())); + ReloadMaps(); + }//GEN-LAST:event_buttonAddMapActionPerformed + + private void buttonDeleteMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteMapActionPerformed + if (listBoxMaps.getSelectedIndex() == -1){ + return; + } + if (JOptionPane.showConfirmDialog(this, (Object) ("Удалить карту " + listBoxMaps.getSelectedValue() + "?") , "Удаление", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION){ + _mapsCollection.DelMap(listBoxMaps.getSelectedValue()); + ReloadMaps(); + } + }//GEN-LAST:event_buttonDeleteMapActionPerformed + + private void buttonShowDeletedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonShowDeletedActionPerformed + if (deletedAirBombers.isEmpty()){ + return; + } + JFrameAirBomber form = new JFrameAirBomber(this, deletedAirBombers.poll()); + form.run(); + }//GEN-LAST:event_buttonShowDeletedActionPerformed + + private void listBoxMapsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listBoxMapsValueChanged + if (listBoxMaps.getSelectedIndex() == -1){ + return; + } + airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null); + }//GEN-LAST:event_listBoxMapsValueChanged + private void ReloadMaps() + { + int index = listBoxMaps.getSelectedIndex(); + + DefaultListModel listBoxMapsModel = new DefaultListModel(); + listBoxMapsModel.clear(); + for (int i = 0; i < _mapsCollection.getKeys().size(); i++) + { + listBoxMapsModel.addElement(_mapsCollection.getKeys().get(i)); + } + listBoxMaps.setModel(listBoxMapsModel); + if (listBoxMapsModel.size() > 0 && (index == -1 || index >= listBoxMapsModel.size())) + { + listBoxMaps.setSelectedIndex(0); + } + else if (listBoxMapsModel.size() > 0 && index > -1 && index < listBoxMapsModel.size()) + { + listBoxMaps.setSelectedIndex(index); + } + } + /** * @param args the command line arguments */ @@ -341,15 +443,22 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { // Variables declaration - do not modify//GEN-BEGIN:variables private AirBomberPackage.CanvasMy airBomberCanvas; private javax.swing.JButton buttonAddAirBomber; + private javax.swing.JButton buttonAddMap; + private javax.swing.JButton buttonDeleteMap; private javax.swing.JButton buttonRemoveAirBomber; + private javax.swing.JButton buttonShowDeleted; private javax.swing.JButton buttonShowOnMap; private javax.swing.JButton buttonShowStorage; private javax.swing.JComboBox comboBoxSelectorMap; private javax.swing.JButton downButton; private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton leftButton; + private javax.swing.JList listBoxMaps; private javax.swing.JFormattedTextField maskedTextBoxPosition; private javax.swing.JButton rightButton; + private javax.swing.JTextField textBoxNewMapName; private javax.swing.JButton upButton; // End of variables declaration//GEN-END:variables } diff --git a/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java b/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java index 71e788d..7caf227 100644 --- a/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java +++ b/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java @@ -26,6 +26,10 @@ public class MapWithSetAirBombersGeneric getSetAirBombers(){ + return _setAirBombers; + } + public int add(T airBomber){ return _setAirBombers.Insert(airBomber); } @@ -46,7 +50,7 @@ public class MapWithSetAirBombersGeneric + /// Словарь (хранилище) с картами + /// + HashMap> _mapStorages; + /// + /// Возвращение списка названий карт + /// + public ArrayList getKeys(){ + ArrayList keys = new ArrayList<>(_mapStorages.keySet()); + return keys; + } + /// + /// Ширина окна отрисовки + /// + private int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new HashMap>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление карты + /// + /// Название карты + /// Карта + public boolean AddMap(String name, AbstractMap map) + { + if (_mapStorages.containsKey(name)) + { + return false; + } + _mapStorages.put(name, new MapWithSetAirBombersGeneric(_pictureWidth, _pictureHeight, map)); + return true; + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(String name) + { + if (_mapStorages.containsKey(name)) + { + _mapStorages.remove(name); + } + } + + public MapWithSetAirBombersGeneric Get(String ind){ + if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind); + else return null; + } + + public DrawingObjectAirBomber Get(String ind, int index){ + if (!_mapStorages.containsKey(ind)) return null; + MapWithSetAirBombersGeneric map = _mapStorages.get(ind); + return map.getSetAirBombers().Get(index); + } +} diff --git a/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java b/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java index b425ac4..37851c1 100644 --- a/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java +++ b/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java @@ -4,6 +4,8 @@ */ package AirBomberPackage; +import java.util.ArrayList; + /** * * @author Андрей @@ -12,19 +14,23 @@ public class SetAirBombersGeneric { /// /// Массив объектов, которые храним /// - private T[] _places; + private ArrayList _places; /// /// Количество объектов в массиве /// - public int Count; + private int _maxCount; /// /// Конструктор /// /// public SetAirBombersGeneric(int count) { - _places = (T[]) new Object[count]; - Count = count; + _places = new ArrayList(count); + _maxCount = count; + } + + public int getCount(){ + return _places.size(); } /// /// Добавление объекта в набор @@ -43,30 +49,11 @@ public class SetAirBombersGeneric { /// public int Insert(T airBomber, int position) { - if (position >= _places.length) + if (position < 0 || position >= _maxCount) { return -1; } - if (_places[position] != null) - { - int indexNull = -1; - for (int i = position; i < _places.length; i++) - { - if (_places[i] == null) - { - indexNull = i; - break; - } - } - if (indexNull == -1) return -1; - for (int i = indexNull; i > position; i--) - { - T tmp = _places[i]; - _places[i] = _places[i - 1]; - _places[i - 1] = tmp; - } - } - _places[position] = airBomber; + _places.add(position, airBomber); return position; } /// @@ -76,12 +63,12 @@ public class SetAirBombersGeneric { /// public T Remove(int position) { - if (position >= _places.length) + if (position <0 || position >= _maxCount) { return null; - } - T removedObject = _places[position]; - _places[position] = null; + } + T removedObject = _places.get(position); + _places.remove(position); return removedObject; } /// @@ -91,10 +78,18 @@ public class SetAirBombersGeneric { /// public T Get(int position) { - if (position >= _places.length) + if (position >= _maxCount || position < 0) { return null; } - return _places[position]; + return _places.get(position); + } + + public void Set(int position, T value){ + if (position >= _maxCount || position < 0) + { + return; + } + _places.set(position, value); } }