From 11be96c351086354cc07656241da991ed4e6a5b1 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Tue, 13 Dec 2022 13:21:24 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A8=D0=B5=D1=81=D1=82=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawingObjectAirBomber.java | 10 ++ .../AirBomberPackage/ExtentionAirBomber.java | 77 +++++++++ .../src/AirBomberPackage/IDrawingObject.java | 2 + .../JFrameMapWithSetAirBombers.form | 50 +++++- .../JFrameMapWithSetAirBombers.java | 102 ++++++++++- .../MapWithSetAirBombersGeneric.java | 26 +++ .../src/AirBomberPackage/MapsCollection.java | 162 +++++++++++++++++- .../SetAirBombersGeneric.java | 17 ++ 8 files changed, 435 insertions(+), 11 deletions(-) create mode 100644 AirBomber/src/AirBomberPackage/ExtentionAirBomber.java diff --git a/AirBomber/src/AirBomberPackage/DrawingObjectAirBomber.java b/AirBomber/src/AirBomberPackage/DrawingObjectAirBomber.java index 66ee1b7..b2c78c8 100644 --- a/AirBomber/src/AirBomberPackage/DrawingObjectAirBomber.java +++ b/AirBomber/src/AirBomberPackage/DrawingObjectAirBomber.java @@ -57,4 +57,14 @@ public class DrawingObjectAirBomber implements IDrawingObject { _airBomber.DrawTransport(g); } } + + @Override + public String GetInfo() { + if (_airBomber != null) return ExtentionAirBomber.ExtentionedAirBomber.GetDataForSave(_airBomber); + else return ""; + } + + public static IDrawingObject Create(String data){ + return new DrawingObjectAirBomber(ExtentionAirBomber.ExtentionedAirBomber.CreateDrawningAirBomber(data)); + } } diff --git a/AirBomber/src/AirBomberPackage/ExtentionAirBomber.java b/AirBomber/src/AirBomberPackage/ExtentionAirBomber.java new file mode 100644 index 0000000..5145857 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/ExtentionAirBomber.java @@ -0,0 +1,77 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; + +import java.awt.Color; + +/** + * + * @author Андрей + */ +public class ExtentionAirBomber{ + public static class ExtentionedAirBomber { + /// + /// Разделитель для записи информации по объекту в файл + /// + private static String _separatorForObject = ":"; + /// + /// Создание объекта из строки + /// + /// + /// + public static DrawingAirBomber CreateDrawningAirBomber(String info) + { + String[] strs = info.split(_separatorForObject); + if (strs.length == 7) //заменить на передачу RGB + { + EnginesType enginesType = null; + switch (strs[6]) { + case "DrawingEngines": + enginesType = EnginesType.RECTANGLE; + break; + case "DrawingEnginesTriangle": + enginesType = EnginesType.TRIANGLE; + break; + case "DrawingEnginesOval": + enginesType = EnginesType.OVAL; + break; + } + return new DrawingAirBomber(Integer.parseInt(strs[0]), + Integer.parseInt(strs[1]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])), Integer.parseInt(strs[5]), enginesType); + } + if (strs.length == 13) + { + EnginesType enginesType = null; + switch (strs[6]) { + case "DrawingEngines": + enginesType = EnginesType.RECTANGLE; + break; + case "DrawingEnginesTriangle": + enginesType = EnginesType.TRIANGLE; + break; + case "DrawingEnginesOval": + enginesType = EnginesType.OVAL; + break; + } + return new DrawingHeavyAirBomber(Integer.parseInt(strs[0]), Float.parseFloat(strs[1]), + new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]),Integer.parseInt(strs[4])), + Integer.parseInt(strs[5]), enginesType, + new Color(Integer.parseInt(strs[7]), Integer.parseInt(strs[8]), Integer.parseInt(strs[9])), + Boolean.parseBoolean(strs[10]),Boolean.parseBoolean(strs[11]), Boolean.parseBoolean(strs[12])); + } + return null; + } + public static String GetDataForSave(DrawingAirBomber drawningAirBomber) + { + var airBomber = drawningAirBomber.AirBomber; + var str = String.format("%s%s%s%s%s%s%s%s%s%s%s%s%s", airBomber.getSpeed(), _separatorForObject, Math.round(airBomber.getWeight()), _separatorForObject, airBomber.getBodyColor().getRed(), _separatorForObject, airBomber.getBodyColor().getGreen(), _separatorForObject, airBomber.getBodyColor().getBlue(), _separatorForObject, drawningAirBomber.drawingEngines.getNumberOfEngines(), _separatorForObject, drawningAirBomber.drawingEngines.getClass().getSimpleName()); + if (airBomber instanceof EntityHeavyAirBomber heavyAirBomber) + { + return String.format("%s%s%s%s%s%s%s%s%s%s%s%s%s", str,_separatorForObject,heavyAirBomber.getDopColor().getRed(),_separatorForObject, heavyAirBomber.getDopColor().getGreen(), _separatorForObject, heavyAirBomber.getDopColor().getBlue(), _separatorForObject, heavyAirBomber.getBombs(), _separatorForObject, heavyAirBomber.getFuelTank(), _separatorForObject, heavyAirBomber.getTailLine()); + } + else return str; + } + } +} diff --git a/AirBomber/src/AirBomberPackage/IDrawingObject.java b/AirBomber/src/AirBomberPackage/IDrawingObject.java index ba533bc..104bf8c 100644 --- a/AirBomber/src/AirBomberPackage/IDrawingObject.java +++ b/AirBomber/src/AirBomberPackage/IDrawingObject.java @@ -37,4 +37,6 @@ public interface IDrawingObject { /// /// float[] GetCurrentPosition(); + + String GetInfo(); } diff --git a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form index 6a13a04..b5b3911 100644 --- a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form +++ b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form @@ -1,10 +1,58 @@
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -86,7 +134,7 @@ - + diff --git a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java index 38b9d35..bcfbd99 100644 --- a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java +++ b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java @@ -46,6 +46,7 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { // //GEN-BEGIN:initComponents private void initComponents() { + jFileChooser1 = new javax.swing.JFileChooser(); airBomberCanvas = new AirBomberPackage.CanvasMy(); comboBoxSelectorMap = new javax.swing.JComboBox<>(); buttonAddAirBomber = new javax.swing.JButton(); @@ -65,6 +66,12 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { listBoxMaps = new javax.swing.JList<>(); buttonDeleteMap = new javax.swing.JButton(); buttonShowDeleted = new javax.swing.JButton(); + jMenuBar1 = new javax.swing.JMenuBar(); + jMenuSaveLoad = new javax.swing.JMenu(); + jMenuItemSave = new javax.swing.JMenuItem(); + jMenuItemLoad = new javax.swing.JMenuItem(); + jMenuItemSaveStorage = new javax.swing.JMenuItem(); + jMenuItemLoadStorage = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); @@ -182,6 +189,44 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { } }); + jMenuSaveLoad.setText("File"); + + jMenuItemSave.setText("Сохранить"); + jMenuItemSave.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jMenuItemSaveActionPerformed(evt); + } + }); + jMenuSaveLoad.add(jMenuItemSave); + + jMenuItemLoad.setText("Загрузить"); + jMenuItemLoad.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jMenuItemLoadActionPerformed(evt); + } + }); + jMenuSaveLoad.add(jMenuItemLoad); + + jMenuItemSaveStorage.setText("Сохранить хранилище"); + jMenuItemSaveStorage.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jMenuItemSaveStorageActionPerformed(evt); + } + }); + jMenuSaveLoad.add(jMenuItemSaveStorage); + + jMenuItemLoadStorage.setText("Загрузить хранилище"); + jMenuItemLoadStorage.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jMenuItemLoadStorageActionPerformed(evt); + } + }); + jMenuSaveLoad.add(jMenuItemLoadStorage); + + jMenuBar1.add(jMenuSaveLoad); + + setJMenuBar(jMenuBar1); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( @@ -238,7 +283,7 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { .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) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE) .addComponent(buttonShowDeleted, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(buttonAddAirBomber) @@ -308,7 +353,7 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { return; } int pos = Integer.parseInt(maskedTextBoxPosition.getText()); - DrawingObjectAirBomber deletedAirBomber = _mapsCollection.Get(listBoxMaps.getSelectedValue()).remove(pos); + DrawingObjectAirBomber deletedAirBomber = (DrawingObjectAirBomber) _mapsCollection.Get(listBoxMaps.getSelectedValue()).remove(pos); if (deletedAirBomber != null) { deletedAirBombers.add(deletedAirBomber); @@ -372,6 +417,52 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { } airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null); }//GEN-LAST:event_listBoxMapsValueChanged + + private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed + if (jFileChooser1.showDialog(this, "Сохранить") == JFileChooser.APPROVE_OPTION){ + if (_mapsCollection.SaveData(jFileChooser1.getSelectedFile().getPath())){ + JOptionPane.showMessageDialog(this, "Cохранено"); + } + else{ + JOptionPane.showMessageDialog(this, "Не сохранилось"); + } + } + }//GEN-LAST:event_jMenuItemSaveActionPerformed + + private void jMenuItemLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLoadActionPerformed + if (jFileChooser1.showDialog(this, "Загрузить") == JFileChooser.APPROVE_OPTION){ + if (_mapsCollection.LoadData(jFileChooser1.getSelectedFile().getPath())){ + JOptionPane.showMessageDialog(this, "Загрузилось"); + ReloadMaps(); + } + else{ + JOptionPane.showMessageDialog(this, "Не загрузилось"); + } + } + }//GEN-LAST:event_jMenuItemLoadActionPerformed + + private void jMenuItemSaveStorageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveStorageActionPerformed + if (jFileChooser1.showDialog(this, "Сохранить") == JFileChooser.APPROVE_OPTION){ + if (_mapsCollection.SaveStorage(jFileChooser1.getSelectedFile().getPath(), listBoxMaps.getSelectedValue())){ + JOptionPane.showMessageDialog(this, "Cохранено"); + } + else{ + JOptionPane.showMessageDialog(this, "Не сохранилось"); + } + } + }//GEN-LAST:event_jMenuItemSaveStorageActionPerformed + + private void jMenuItemLoadStorageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLoadStorageActionPerformed + if (jFileChooser1.showDialog(this, "Загрузить") == JFileChooser.APPROVE_OPTION){ + if (_mapsCollection.LoadStorage(jFileChooser1.getSelectedFile().getPath())){ + JOptionPane.showMessageDialog(this, "Загрузилось"); + ReloadMaps(); + } + else{ + JOptionPane.showMessageDialog(this, "Не загрузилось"); + } + } + }//GEN-LAST:event_jMenuItemLoadStorageActionPerformed private void ReloadMaps() { @@ -440,8 +531,15 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { private javax.swing.JButton buttonShowStorage; private javax.swing.JComboBox comboBoxSelectorMap; private javax.swing.JButton downButton; + private javax.swing.JFileChooser jFileChooser1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; + private javax.swing.JMenuBar jMenuBar1; + private javax.swing.JMenuItem jMenuItemLoad; + private javax.swing.JMenuItem jMenuItemLoadStorage; + private javax.swing.JMenuItem jMenuItemSave; + private javax.swing.JMenuItem jMenuItemSaveStorage; + private javax.swing.JMenu jMenuSaveLoad; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton leftButton; private javax.swing.JList listBoxMaps; diff --git a/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java b/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java index 7caf227..3c08840 100644 --- a/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java +++ b/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java @@ -70,6 +70,32 @@ public class MapWithSetAirBombersGeneric + /// Получение данных в виде строки + /// + /// + /// + public String GetData(String separatorType, String separatorData) + { + String data = _map.getClass().getSimpleName() + separatorType; + for (var airBomber : _setAirBombers.GetAirBombers()) + { + data += airBomber.GetInfo() + separatorData; + } + return data; + } + public void LoadData(String[] records) + { + for (var rec : records) + { + _setAirBombers.Insert((T) DrawingObjectAirBomber.Create(rec)); + } + } + + public void clearStorage(){ + _setAirBombers.clear(); + } + private void Shaking() { int j = _setAirBombers.getCount() - 1; diff --git a/AirBomber/src/AirBomberPackage/MapsCollection.java b/AirBomber/src/AirBomberPackage/MapsCollection.java index 5cc7401..7494698 100644 --- a/AirBomber/src/AirBomberPackage/MapsCollection.java +++ b/AirBomber/src/AirBomberPackage/MapsCollection.java @@ -5,6 +5,8 @@ package AirBomberPackage; import java.util.HashMap; import java.util.ArrayList; +import java.io.*; +import javax.swing.*; /** * * @author Андрей @@ -13,7 +15,11 @@ public class MapsCollection { /// /// Словарь (хранилище) с картами /// - HashMap> _mapStorages; + HashMap> _mapStorages; + + private String separatorDict = "-"; + private String separatorData = ";"; + /// /// Возвращение списка названий карт /// @@ -36,7 +42,7 @@ public class MapsCollection { /// public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new HashMap>(); + _mapStorages = new HashMap>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -51,7 +57,7 @@ public class MapsCollection { { return false; } - _mapStorages.put(name, new MapWithSetAirBombersGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.put(name, new MapWithSetAirBombersGeneric(_pictureWidth, _pictureHeight, map)); return true; } /// @@ -66,14 +72,154 @@ public class MapsCollection { } } - public MapWithSetAirBombersGeneric Get(String ind){ + 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); + public boolean SaveData(String filename) + { + File saveFile = new File(filename); + if (saveFile.exists()) + { + saveFile.delete(); + } + + try{ + BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile)); + bw.write("MapsCollection\n"); + for (var storage : _mapStorages.keySet()) + { + bw.write(storage + separatorDict + _mapStorages.get(storage).GetData(separatorDict, separatorData) + "\n"); + } + bw.close(); + } + catch (IOException ex){ + return false; + } + return true; + } + + /// + /// Загрузка нформации по автомобилям на парковках из файла + /// + /// + /// + public boolean LoadData(String filename) + { + File loadFile = new File(filename); + if (!loadFile.exists()) + { + return false; + } + try{ + BufferedReader br = new BufferedReader(new FileReader(loadFile)); + String line; + line = br.readLine(); + line.trim(); + if (!line.equals("MapsCollection")) return false; + _mapStorages.clear(); + while ((line = br.readLine()) != null) + { + line = line.trim(); + var elem = line.split(separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "CityMap": + map = new CityMap(); + break; + case "LineMap": + map = new LineMap(); + break; + } + _mapStorages.put(elem[0], new MapWithSetAirBombersGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.get(elem[0]).LoadData(elem[2].split(separatorData)); + } + br.close(); + return true; + } + catch (IOException ex){ + return false; + } + } + + public boolean SaveStorage(String filename, String storageKey) + { + File saveFile = new File(filename); + if (saveFile.exists()) + { + saveFile.delete(); + } + + try{ + BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile)); + bw.write("Storage\n"); + bw.write(storageKey + "\n"); + String[] storageStrings = _mapStorages.get(storageKey).GetData(separatorDict, separatorData).split(separatorDict); + bw.write(storageStrings[0] + "\n"); + String[] bombers = storageStrings[1].split(separatorData); + bw.write(bombers.length + "\n"); + for (String bomber : bombers ){ + bw.write(bomber + "\n"); + } + bw.close(); + } + catch (IOException ex){ + return false; + } + return true; + } + + public boolean LoadStorage(String filename){ + File loadFile = new File(filename); + if (!loadFile.exists()) + { + return false; + } + + try{ + BufferedReader br = new BufferedReader(new FileReader(loadFile)); + String line; + line = br.readLine(); + line.trim(); + if (!line.equals("Storage")) return false; + + String key = br.readLine(); + String mapType = br.readLine(); + if (_mapStorages.containsKey(key)){ + _mapStorages.get(key).clearStorage(); + } + else{ + AbstractMap map = null; + switch (mapType) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "CityMap": + map = new CityMap(); + break; + case "LineMap": + map = new LineMap(); + break; + } + _mapStorages.put(key, new MapWithSetAirBombersGeneric(_pictureWidth, _pictureHeight, map)); + } + + int numberOfBombers = Integer.parseInt( br.readLine()); + for (int i =0; i< numberOfBombers;i++){ + String bomber = br.readLine(); + int res = _mapStorages.get(key).add(new DrawingObjectAirBomber(ExtentionAirBomber.ExtentionedAirBomber.CreateDrawningAirBomber(bomber))); + } + br.close(); + return true; + } + catch (IOException ex){ + return false; + } } } diff --git a/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java b/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java index 37851c1..f711f60 100644 --- a/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java +++ b/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java @@ -92,4 +92,21 @@ public class SetAirBombersGeneric { } _places.set(position, value); } + + public ArrayList GetAirBombers(){ + ArrayList notNullAirBombers = new ArrayList<>(); + for(var airBomber : _places){ + if (airBomber != null){ + notNullAirBombers.add(airBomber); + } + else{ + break; + } + } + return notNullAirBombers; + } + + public void clear(){ + _places.clear(); + } } -- 2.25.1