From 7789df7732c8eb201ff905b23a1512d7394d5b5b Mon Sep 17 00:00:00 2001 From: goblinrf Date: Mon, 11 Dec 2023 23:29:20 +0300 Subject: [PATCH] Done --- src/DrawningObjects/DrawningAirplane.java | 23 +++- .../ExtentionDrawningAirplane.java | 87 ++++++++++++ src/FormAirFighterCollection.java | 121 ++++++++++++++++- src/Generics/AirplaneGenericCollection.java | 44 +++++- src/Generics/AirplaneGenericStorage.java | 127 +++++++++++++++++- src/Generics/SetGeneric.java | 4 +- 6 files changed, 393 insertions(+), 13 deletions(-) create mode 100644 src/DrawningObjects/ExtentionDrawningAirplane.java diff --git a/src/DrawningObjects/DrawningAirplane.java b/src/DrawningObjects/DrawningAirplane.java index 7bc19be..ef2fe0a 100644 --- a/src/DrawningObjects/DrawningAirplane.java +++ b/src/DrawningObjects/DrawningAirplane.java @@ -44,9 +44,30 @@ public class DrawningAirplane { DrawningEngines = new DrawningEnginesOval(_airplaneWidth, _airplaneHeight, _startPosX, _startPosY); } } - DrawningEngines.ChangeEnginesNumb(rand.nextInt(1, 5)); + DrawningEngines.ChangeEnginesNumb(rand.nextInt(4)); } + public int GetIDrawType(){ + if(DrawningEngines instanceof DrawningEngines) + return 0; + if(DrawningEngines instanceof DrawningEnginesQuad) + return 1; + if(DrawningEngines instanceof DrawningEnginesOval) + return 2; + return -1; + } + public void ChangeIDraw(int variant){ + IDraw cur; + int dif = _airplaneWidth / 10; + if(variant == 0) + cur = new DrawningEngines(_airplaneWidth - dif, _airplaneHeight,_startPosX,_startPosY); + else if(variant == 1) + cur = new DrawningEnginesOval(_airplaneWidth - dif, _airplaneHeight,_startPosX,_startPosY); + else + cur = new DrawningEnginesQuad(_airplaneWidth - dif, _airplaneHeight,_startPosX,_startPosY); + ChangeIDraw(cur); + + } public void ChangeIDraw(IDraw obj){ DrawningEngines = obj; obj.ChangeEnginesNumb(_engineNumb); diff --git a/src/DrawningObjects/ExtentionDrawningAirplane.java b/src/DrawningObjects/ExtentionDrawningAirplane.java new file mode 100644 index 0000000..60a3219 --- /dev/null +++ b/src/DrawningObjects/ExtentionDrawningAirplane.java @@ -0,0 +1,87 @@ +package src.DrawningObjects; + +import src.Entities.EntityAirFighter; + +import java.awt.*; + +public class ExtentionDrawningAirplane { + private static String getName(Color col){ + if(col.equals(Color.RED)) + return new String("RED"); + if(col.equals(Color.GREEN)) + return new String("GREEN"); + if(col.equals(Color.BLUE)) + return new String("BLUE"); + if(col.equals(Color.YELLOW)) + return new String("YELLOW"); + if(col.equals(Color.WHITE)) + return new String("WHITE"); + if(col.equals(Color.GRAY)) + return new String("GRAY"); + if(col.equals(Color.BLACK)) + return new String("BLACK"); + if(col.equals(Color.PINK)) + return new String("PINK"); + return null; + } + + private static Color getColor(String col){ + if(col.equals("RED")) + return Color.RED; + if(col.equals("GREEN")) + return Color.GREEN; + if(col.equals("BLUE")) + return Color.BLUE; + if(col.equals("YELLOW")) + return Color.YELLOW; + if(col.equals("WHITE")) + return Color.WHITE; + if(col.equals("GRAY")) + return Color.GRAY; + if(col.equals("BLACK")) + return Color.BLACK; + if(col.equals("PINK")) + return Color.PINK; + return null; + } + public static DrawningAirplane CreateDrawningAirplane(String info, char separatorForObject, + int width, int height){ + String[] strs = info.split(Character.toString(separatorForObject)); + if(strs.length == 5){ + DrawningAirplane toRet = new DrawningAirplane(Integer.parseInt(strs[0]), + Integer.parseInt(strs[1]), getColor(strs[2]), + width, height); + toRet.ChangeEnginesNumb(Integer.parseInt(strs[3])); + toRet.ChangeIDraw(Integer.parseInt(strs[4])); + return toRet; + } + if(strs.length == 8){ + DrawningAirplane toRet = new DrawningAirFighter(Integer.parseInt(strs[0]), + Integer.parseInt(strs[1]), getColor(strs[2]), + getColor(strs[7]), + Boolean.parseBoolean(strs[5]), + Boolean.parseBoolean(strs[6]),width, height); + toRet.ChangeIDraw(Integer.parseInt(strs[3])); + toRet.ChangeEnginesNumb(Integer.parseInt(strs[4])); + return toRet; + } + return null; + } + public static String GetDataForSave(DrawningAirplane drawningAirplane, char separatorForObject){ + var airplane = drawningAirplane.EntityAirplane; + if(airplane == null) + return null; + String colName = getName(airplane.BodyColor); + var str = String.format("%d%c%d%c%s%c%d%c%d", airplane.Speed(), separatorForObject, (int)airplane.Weight(), + separatorForObject, getName(airplane.BodyColor), separatorForObject, + drawningAirplane._engineNumb, separatorForObject, drawningAirplane.GetIDrawType()); + if(!(airplane instanceof EntityAirFighter)){ + return str; + } + var nstr = String.format("%s%c%b%c%b%c%s", str, separatorForObject, + ((EntityAirFighter) airplane).Racket(), separatorForObject, + ((EntityAirFighter) airplane).DopWing(), separatorForObject, + getName(((EntityAirFighter) airplane).AdditionalColor), separatorForObject); + return nstr; + } +} \ No newline at end of file diff --git a/src/FormAirFighterCollection.java b/src/FormAirFighterCollection.java index 738bd47..838e509 100644 --- a/src/FormAirFighterCollection.java +++ b/src/FormAirFighterCollection.java @@ -1,6 +1,7 @@ package src; import src.DrawningObjects.DrawningAirplane; +import src.DrawningObjects.ExtentionDrawningAirplane; import src.Generics.AirplaneGenericCollection; import src.FormAirFighter; import src.Generics.AirplaneGenericStorage; @@ -8,12 +9,38 @@ import src.Generics.AirplaneTrashCollection; import src.MovementStrategy.DrawningObjectAirplane; import src.MovementStrategy.IMoveableObject; import javax.swing.*; +import javax.swing.filechooser.FileFilter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Map; +import java.util.Scanner; +import java.util.HashMap; +class TxtSaveFilter extends FileFilter { + @Override + public boolean accept(File f) { + if (f.isDirectory()) { + return false; + } + + String s = f.getName().toLowerCase(); + + return s.endsWith(".txt"); + } + + @Override + public String getDescription() { + return "*.txt"; + } +} public class FormAirFighterCollection { + private final AirplaneGenericStorage _storage; private JList listBoxStorages; @@ -41,6 +68,17 @@ public class FormAirFighterCollection { listBoxStorages.setSelectedIndex(index); } public FormAirFighterCollection(){ + JMenuBar menuFile = new JMenuBar(); + JMenu file = new JMenu("Файл"); + menuFile.add(file); + JMenuItem saveFile = new JMenuItem("Сохранить"); + JMenuItem loadFile = new JMenuItem("Загрузить"); + JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию"); + JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию"); + file.add(saveCollection); + file.add(loadCollection); + file.add(saveFile); + file.add(loadFile); AirplaneTrashCollection _trashCollection = new AirplaneTrashCollection<>(); JButton callTrashButton = new JButton("мусор"); _storage = new AirplaneGenericStorage(pictureBoxWidth, pictureBoxHeight); @@ -75,10 +113,91 @@ public class FormAirFighterCollection { toolBox.add(refreshButton); toolBox.add(callTrashButton); collectionFrame.add(toolBox); + collectionFrame.setJMenuBar(menuFile); collectionFrame.add(canv); collectionFrame.setVisible(true); canv._storage = _storage; canv.listBoxStorages = listBoxStorages; + saveFile.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves"); + fc.addChoosableFileFilter(new TxtSaveFilter()); + int retrieval = fc.showSaveDialog(null); + + if (retrieval == JFileChooser.APPROVE_OPTION) { + File file = new File(fc.getSelectedFile() + "." + "txt"); + + try { + _storage.SaveData(file); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + }); + + saveCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves"); + fc.addChoosableFileFilter(new TxtSaveFilter()); + int retrieval = fc.showSaveDialog(null); + + if (retrieval == JFileChooser.APPROVE_OPTION) { + File file = new File(fc.getSelectedFile() + "." + "txt"); + + try { + if(listBoxStorages.getSelectedIndex() == -1) { + return; + } + _storage._airplaneStorages.get(listBoxStorages.getSelectedValue()).SaveData(file, listBoxStorages.getSelectedValue()); + ReloadObjects(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + }); + + loadFile.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves"); + int ret = fc.showDialog(null, "Открыть файл"); + if(ret == JFileChooser.APPROVE_OPTION){ + File file = fc.getSelectedFile(); + try { + _storage.LoadData(file); + canv._storage =_storage; + ReloadObjects(); + canv.repaint(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + } + }); + + loadCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves"); + int ret = fc.showDialog(null, "Открыть файл"); + if(ret == JFileChooser.APPROVE_OPTION){ + File file = fc.getSelectedFile(); + try { + _storage.LoadCollection(file); + ReloadObjects(); + canv.repaint(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + } + }); addStorageButton.addActionListener(new ActionListener() { @Override @@ -96,7 +215,7 @@ public class FormAirFighterCollection { if(listBoxStorages.getSelectedIndex() == -1) { return; } - _storage.DelSet(listBoxStorages.getSelectedValue()); + _storage.DelSet(listBoxStorages.getSelectedValue(), _trashCollection); ReloadObjects(); } }); diff --git a/src/Generics/AirplaneGenericCollection.java b/src/Generics/AirplaneGenericCollection.java index 3b18e2a..b4eeb4a 100644 --- a/src/Generics/AirplaneGenericCollection.java +++ b/src/Generics/AirplaneGenericCollection.java @@ -2,20 +2,54 @@ package src.Generics; import src.DrawningObjects.DrawningAirplane; import src.MovementStrategy.IMoveableObject; +import src.DrawningObjects.ExtentionDrawningAirplane; +import src.MovementStrategy.DrawningObjectAirplane; import java.awt.*; import java.awt.image.BufferedImage; - +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Map; public class AirplaneGenericCollection { private final int _pictureWidth; private final int _pictureHeight; - + public static char _separatorRecords = ';'; + public static char _separatorForObject = ':'; private final int _placeSizeWidth = 166; private final int _placeSizeHeight = 160; - private final SetGeneric _collection; + private SetGeneric _collection; + public ArrayList GetAirplanes(){ + return _collection.GetAirplanes(_collection.Count); + } + + public boolean SaveData(File f, String name) throws IOException { + if(f.exists()) { + f.delete(); + } + f.createNewFile(); + StringBuilder data = new StringBuilder(); + data.append("AirplaneCollection\n"); + data.append(String.format("%s\n", name)); + StringBuilder records = new StringBuilder(); + for(DrawningAirplane elem : GetAirplanes()) + { + records.append(String.format("%s%c", ExtentionDrawningAirplane.GetDataForSave(elem, _separatorForObject), + _separatorRecords)); + } + data.append(records); + if(data.length() == 0) + return false; + FileWriter writer = new FileWriter(f); + writer.write(data.toString()); + writer.flush(); + writer.close(); + return true; + } public AirplaneGenericCollection(int picWidth, int picHeight){ int width = picWidth / _placeSizeWidth; @@ -88,4 +122,8 @@ public class AirplaneGenericCollection(_pictureWidth * _pictureHeight); + } } diff --git a/src/Generics/AirplaneGenericStorage.java b/src/Generics/AirplaneGenericStorage.java index 80f935a..149e463 100644 --- a/src/Generics/AirplaneGenericStorage.java +++ b/src/Generics/AirplaneGenericStorage.java @@ -2,15 +2,117 @@ package src.Generics; import src.DrawningObjects.DrawningAirplane; import src.MovementStrategy.DrawningObjectAirplane; +import src.DrawningObjects.ExtentionDrawningAirplane; -import java.util.Dictionary; -import java.util.HashMap; -import java.util.List; -import java.util.Queue; +import java.io.*; +import java.util.*; import java.util.stream.Collectors; public class AirplaneGenericStorage { - final HashMap> _airplaneStorages; + public final HashMap> _airplaneStorages; + private static final char _separatorForKeyValue = '|'; + private final char _separatorRecords = ';'; + private static final char _separatorForObject = ':'; + + public boolean SaveData(File f) throws IOException { + if(f.exists()) { + f.delete(); + } + f.createNewFile(); + StringBuilder data = new StringBuilder(); + data.append("AirplaneStorage\n"); + for(Map.Entry> record : _airplaneStorages.entrySet()){ + StringBuilder records = new StringBuilder(); + for(DrawningAirplane elem : record.getValue().GetAirplanes()) + { + records.append(String.format("%s%c", ExtentionDrawningAirplane.GetDataForSave(elem, _separatorForObject), + _separatorRecords)); + } + data.append(String.format("%s%c%s\n", record.getKey(), _separatorForKeyValue, records.toString())); + } + if(data.length() == 0) + return false; + FileWriter writer = new FileWriter(f); + writer.write(data.toString()); + writer.flush(); + writer.close(); + return true; + } + public boolean LoadData(File f) throws FileNotFoundException { + if(!f.exists()) + return false; + StringBuilder bufferTextFromFile =new StringBuilder(); + Scanner s = new Scanner(f); + while(s.hasNext()) + bufferTextFromFile.append(s.next() + "\n"); + s.close(); + var strs = bufferTextFromFile.toString().split("\n"); + if(strs == null || strs.length == 0) + return false; + if (!strs[0].startsWith("AirplaneStorage")) + return false; + _airplaneStorages.clear(); + for(String data : strs){ + String st = new String("\\" + Character.toString( _separatorForKeyValue)); + String[]record = data.split(st); + if (record.length != 2) + continue; + AirplaneGenericCollection collection = + new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight); + String[] set = record[1].split(Character.toString(_separatorRecords)); + + for(int i = set.length -1; i >=0; i--){ + String elem = set[i]; + DrawningAirplane airplane = ExtentionDrawningAirplane.CreateDrawningAirplane(elem, + _separatorForObject, _pictureWidth, _pictureHeight); + if (airplane != null) + { + if (!(collection.Insert(airplane))) + { + return false; + } + } + } + _airplaneStorages.put(record[0], collection); + } + return true; + } + + public boolean LoadCollection(File f) throws FileNotFoundException { + if(!f.exists()) + return false; + StringBuilder bufferTextFromFile =new StringBuilder(); + Scanner s = new Scanner(f); + while(s.hasNext()) + bufferTextFromFile.append(s.next() + "\n"); + s.close(); + var strs = bufferTextFromFile.toString().split("\n"); + if(strs == null || strs.length == 0) + return false; + if (!strs[0].startsWith("AirplaneCollection")) + return false; + String collectionName = strs[1]; + AirplaneGenericCollection collection = GetCollection(collectionName); + if(collection == null) + collection = new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight); + else + collection.Clear(); + String[] airplanesInfo = strs[2].split(Character.toString(AirplaneGenericCollection._separatorRecords)); + for(int i = airplanesInfo.length-1; i >= 0; i--){ + String data = airplanesInfo[i]; + DrawningAirplane airplane = ExtentionDrawningAirplane.CreateDrawningAirplane(data, + AirplaneGenericCollection._separatorForObject, _pictureWidth, _pictureHeight); + if (airplane != null) + { + if (!(collection.Insert(airplane))) + { + return false; + } + } + } + AddSetFromFile(collectionName, collection); + return true; + } public List Keys(){ if(_airplaneStorages == null) return null; @@ -31,9 +133,19 @@ public class AirplaneGenericStorage { _airplaneStorages.put(name, new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight)); } - public void DelSet(String name){ + public void AddSetFromFile(String name, AirplaneGenericCollection toAdd){ + if(_airplaneStorages.containsKey(name)){ + _airplaneStorages.remove(name); + } + _airplaneStorages.put(name, toAdd); + } + + public void DelSet(String name, AirplaneTrashCollection trashBox){ if(!_airplaneStorages.containsKey(name)) return; + AirplaneGenericCollection cur = _airplaneStorages.get(name); + for(int i = 0; i < cur.Size(); i++) + trashBox.Push(cur.Get(i)); _airplaneStorages.remove(name); } @@ -46,4 +158,7 @@ public class AirplaneGenericStorage { public DrawningAirplane Get(String collectionName, int position){ return _airplaneStorages.get(collectionName).Get(position); } + public AirplaneGenericCollection GetCollection(String collectionName){ + return _airplaneStorages.get(collectionName); + } } diff --git a/src/Generics/SetGeneric.java b/src/Generics/SetGeneric.java index 3dbbc18..eeac27d 100644 --- a/src/Generics/SetGeneric.java +++ b/src/Generics/SetGeneric.java @@ -40,11 +40,11 @@ public class SetGeneric { return (T)_places.get(position); } - public ArrayList GetMonorails(int maxMonorails){ + public ArrayList GetAirplanes(int maxAirplanes){ ArrayList toRet = new ArrayList<>(); for(int i = 0; i < _places.size(); i++){ toRet.add(_places.get(i)); - if(i == maxMonorails) + if(i == maxAirplanes) return toRet; } return toRet; -- 2.25.1