From 6663b98a920c29616b000a4267556118d8288d80 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Fri, 2 Dec 2022 10:17:40 +0300 Subject: [PATCH] lab6 full --- AircraftFactory.java | 71 ++++++++++++++++ DrawingEngines.java | 12 +++ DrawingObjectAircraft.java | 9 +++ DrawingTruncatedEngines.java | 12 +++ DrawingWavyEngines.java | 12 +++ FormMapWithSetAircrafts.java | 80 ++++++++++++++++++ IDrawingEngines.java | 2 + IDrawingObject.java | 1 + MapWithSetAircraftsGeneric.java | 59 ++++++++++---- MapsCollection.java | 139 ++++++++++++++++++++++++++++++++ SetAircraftsGeneric.java | 9 +++ 11 files changed, 392 insertions(+), 14 deletions(-) create mode 100644 AircraftFactory.java diff --git a/AircraftFactory.java b/AircraftFactory.java new file mode 100644 index 0000000..56e63fe --- /dev/null +++ b/AircraftFactory.java @@ -0,0 +1,71 @@ +import java.awt.*; +import java.util.Objects; + +public class AircraftFactory { + private static char _separatorForObject = ':'; + private static Color convertStrToColor(String str) { + String color[] = str.split(","); + int r = Integer.parseInt(color[0]); + int g = Integer.parseInt(color[1]); + int b = Integer.parseInt(color[2]); + + return new Color(r, g, b); + } + + public static DrawingAircraft createAircraftFromData(String data) { + String strsArr[] = data.split(_separatorForObject + ""); + + DrawingAircraft result; + IDrawingEngines engines; + + switch(strsArr[3]) { + case "DrawingTruncatedEngines": + engines = new DrawingTruncatedEngines(Integer.parseInt(strsArr[4]), convertStrToColor(strsArr[5])); + break; + case "DrawingWavyEngines": + engines = new DrawingWavyEngines(Integer.parseInt(strsArr[4]), convertStrToColor(strsArr[5])); + break; + default: + engines = new DrawingEngines(Integer.parseInt(strsArr[4]), convertStrToColor(strsArr[5])); + } + + if(strsArr.length == 9) { + result = new DrawingModernAircraft( + Integer.parseInt(strsArr[0]), Float.parseFloat(strsArr[1]), convertStrToColor(strsArr[2]), + convertStrToColor(strsArr[6]), Objects.equals(strsArr[8], "true"), Objects.equals(strsArr[7], "true") + ); + } else { + result = new DrawingAircraft( + Integer.parseInt(strsArr[0]), Float.parseFloat(strsArr[1]), convertStrToColor(strsArr[2]) + ); + } + result.setEngines(engines); + + return result; + } + + public static String getDataForSave(DrawingAircraft drawingAircraft) { + EntityAircraft aircraft = drawingAircraft.AirFighter; + + String res = aircraft.Speed + "" + _separatorForObject; + res += aircraft.Weight + "" + _separatorForObject; + res += aircraft.BodyColor.getRed() + "," + aircraft.BodyColor.getGreen() + "," + aircraft.BodyColor.getBlue(); + res += _separatorForObject; + + IDrawingEngines engines = drawingAircraft.getEngines(); + + res += engines.getClass().getName() + _separatorForObject; + res += engines.getCount() + "" + _separatorForObject; + res += engines.getColor().getRed() + "," + engines.getColor().getGreen() + "," + engines.getColor().getBlue(); + + if(aircraft instanceof EntityModernAircraft a) { + res += _separatorForObject; + res += a.DopColor.getRed() + "," + a.DopColor.getGreen() + "," + a.DopColor.getBlue(); + res += _separatorForObject; + res += a.Rockets + "" + _separatorForObject; + res += a.DopWings + ""; + } + + return res; + } +} diff --git a/DrawingEngines.java b/DrawingEngines.java index c52e809..669ab76 100644 --- a/DrawingEngines.java +++ b/DrawingEngines.java @@ -21,6 +21,18 @@ public class DrawingEngines implements IDrawingEngines { this.color = color; } + @Override + public int getCount() { + if(enginesCount == EnginesCount.Two) return 2; + if(enginesCount == EnginesCount.Four) return 4; + return 6; + } + + @Override + public Color getColor() { + return color; + } + public void draw(Graphics2D g, int startPosX, int startPosY) { g.setPaint(color); g.fillOval(startPosX + 80, startPosY + 10, 30, 15); diff --git a/DrawingObjectAircraft.java b/DrawingObjectAircraft.java index 296299c..cf31c3c 100644 --- a/DrawingObjectAircraft.java +++ b/DrawingObjectAircraft.java @@ -43,4 +43,13 @@ public class DrawingObjectAircraft implements IDrawingObject if(_aircraft == null) return new Point(0,0); return _aircraft.getRightBottom(); } + + @Override + public String GetInfo() { + return AircraftFactory.getDataForSave(_aircraft); + } + + public static DrawingObjectAircraft Create(String data) { + return new DrawingObjectAircraft(AircraftFactory.createAircraftFromData(data)); + } } diff --git a/DrawingTruncatedEngines.java b/DrawingTruncatedEngines.java index a0ee8cb..b7d1692 100644 --- a/DrawingTruncatedEngines.java +++ b/DrawingTruncatedEngines.java @@ -21,6 +21,18 @@ public class DrawingTruncatedEngines implements IDrawingEngines { this.color = color; } + @Override + public int getCount() { + if(enginesCount == EnginesCount.Two) return 2; + if(enginesCount == EnginesCount.Four) return 4; + return 6; + } + + @Override + public Color getColor() { + return color; + } + public void draw(Graphics2D g, int startPosX, int startPosY) { g.setPaint(color); g.fillArc(startPosX + 90, startPosY + 10, 30, 15, 90, 180); diff --git a/DrawingWavyEngines.java b/DrawingWavyEngines.java index ff84219..81ac4dc 100644 --- a/DrawingWavyEngines.java +++ b/DrawingWavyEngines.java @@ -21,6 +21,18 @@ public class DrawingWavyEngines implements IDrawingEngines { this.color = color; } + @Override + public int getCount() { + if(enginesCount == EnginesCount.Two) return 2; + if(enginesCount == EnginesCount.Four) return 4; + return 6; + } + + @Override + public Color getColor() { + return color; + } + private void drawEngine(Graphics2D g, int x, int y) { g.setColor(color); g.fillRect(x, y, 21, 10); diff --git a/FormMapWithSetAircrafts.java b/FormMapWithSetAircrafts.java index 48d25f3..30c2f1f 100644 --- a/FormMapWithSetAircrafts.java +++ b/FormMapWithSetAircrafts.java @@ -1,4 +1,5 @@ import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.util.*; import java.util.List; @@ -22,6 +23,8 @@ public class FormMapWithSetAircrafts implements Form { private JButton buttonDeleteMap; private JButton buttonShowDeleted; + private JMenuBar menu; + private MapsCollection _mapsCollection; private HashMap _mapsDict = new HashMap<>(){{ put( "Простая карта", new SimpleMap() ); @@ -62,6 +65,83 @@ public class FormMapWithSetAircrafts implements Form { jFrame.revalidate(); + menu = new JMenuBar(); + JMenu fileActions = new JMenu("Файл"); + + menu.add(fileActions); + jFrame.setJMenuBar(menu); + + JMenuItem saveItem = new JMenuItem("Сохранить"); + JMenuItem loadItem = new JMenuItem("Загрузить"); + JMenuItem saveMapItem = new JMenuItem("Сохранить карту"); + JMenuItem loadMapItem = new JMenuItem("Загрузить карту"); + + fileActions.add(saveItem); + fileActions.add(loadItem); + fileActions.add(saveMapItem); + fileActions.add(loadMapItem); + + saveItem.addActionListener(e -> { + JFileChooser chooser = new JFileChooser(); + chooser.setFileFilter(new FileNameExtensionFilter("TXT format", "txt")); + chooser.showSaveDialog(jFrame); + + if(chooser.getSelectedFile() == null) return; + + String path = chooser.getSelectedFile().getAbsolutePath(); + + if(_mapsCollection.SaveData(path)) { + JOptionPane.showMessageDialog(jFrame, "Сохранение прошло успешно"); + } else JOptionPane.showMessageDialog(jFrame, "Не сохранилось"); + + }); + + loadItem.addActionListener(e -> { + JFileChooser chooser = new JFileChooser(); + chooser.setFileFilter(new FileNameExtensionFilter("TXT format", "txt")); + chooser.showOpenDialog(jFrame); + + if(chooser.getSelectedFile() == null) return; + + String path = chooser.getSelectedFile().getAbsolutePath(); + if(_mapsCollection.LoadData(path)) { + JOptionPane.showMessageDialog(jFrame, "Загрузка прошла успешно"); + ReloadMaps(); + } else JOptionPane.showMessageDialog(jFrame, "Не загрузилось"); + }); + + saveMapItem.addActionListener(e -> { + if(_mapsCollection == null) return; + if(listBoxMaps.getSelectedValue() == null) return; + + JFileChooser chooser = new JFileChooser(); + chooser.setFileFilter(new FileNameExtensionFilter("TXT format", "txt")); + chooser.showSaveDialog(jFrame); + + if(chooser.getSelectedFile() == null) return; + + String path = chooser.getSelectedFile().getAbsolutePath(); + + if(_mapsCollection.SaveMap(path, listBoxMaps.getSelectedValue().toString())) { + JOptionPane.showMessageDialog(jFrame, "Сохранение прошло успешно"); + } else JOptionPane.showMessageDialog(jFrame, "Не сохранилось"); + + }); + + loadMapItem.addActionListener(e -> { + JFileChooser chooser = new JFileChooser(); + chooser.setFileFilter(new FileNameExtensionFilter("TXT format", "txt")); + chooser.showOpenDialog(jFrame); + + if(chooser.getSelectedFile() == null) return; + + String path = chooser.getSelectedFile().getAbsolutePath(); + if(_mapsCollection.LoadMap(path)) { + JOptionPane.showMessageDialog(jFrame, "Загрузка прошла успешно"); + ReloadMaps(); + } else JOptionPane.showMessageDialog(jFrame, "Не загрузилось"); + }); + _mapsCollection = new MapsCollection(canv.getSize().width, canv.getSize().height); comboBoxSelectorMap.removeAllItems(); diff --git a/IDrawingEngines.java b/IDrawingEngines.java index 27685c6..2e5a117 100644 --- a/IDrawingEngines.java +++ b/IDrawingEngines.java @@ -3,5 +3,7 @@ import java.awt.*; public interface IDrawingEngines { void setCount(int count); void setColor(Color color); + int getCount(); + Color getColor(); void draw(Graphics2D g, int x, int y); } diff --git a/IDrawingObject.java b/IDrawingObject.java index 9665c81..941a6cf 100644 --- a/IDrawingObject.java +++ b/IDrawingObject.java @@ -8,4 +8,5 @@ public interface IDrawingObject void DrawningObject(Graphics2D g); Point GetLeftTop(); Point GetRightBottom(); + String GetInfo(); } diff --git a/MapWithSetAircraftsGeneric.java b/MapWithSetAircraftsGeneric.java index 64f4533..6a437cf 100644 --- a/MapWithSetAircraftsGeneric.java +++ b/MapWithSetAircraftsGeneric.java @@ -1,5 +1,6 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.util.ListIterator; public class MapWithSetAircraftsGeneric { @@ -7,27 +8,53 @@ public class MapWithSetAircraftsGeneric _setCars; + private SetAircraftsGeneric _setAircrafts; private U _map; public MapWithSetAircraftsGeneric(int picWidth, int picHeight, U map) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; - _setCars = new SetAircraftsGeneric(width * height); + _setAircrafts = new SetAircraftsGeneric(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; } + public U getMap() { + return _map; + } + + public String GetData(char separatorType, char separatorData) + { + String data = _map.getClass().getName() + "" + separatorType; + ListIterator iter = _setAircrafts.GetElems(); + while (iter.hasNext()) + { + data += iter.next().GetInfo() + separatorData; + } + return data; + } + + public void ClearMap() { + _setAircrafts.Clear(); + } + + public void LoadData(String[] records) { + for (String rec : records) + { + _setAircrafts.Insert((T)DrawingObjectAircraft.Create(rec)); + } + } + public int addAircraft(T aircraft) { - return _setCars.Insert(aircraft); + return _setAircrafts.Insert(aircraft); } public T removeAircraft(int position) { - return _setCars.Remove(position); + return _setAircrafts.Remove(position); } public Image ShowSet() @@ -47,9 +74,9 @@ public class MapWithSetAircraftsGeneric getAircraftsIter() { + return _setAircrafts.GetElems(); + } + private void Shaking() { - int j = _setCars.getCount() - 1; - for (int i = 0; i < _setCars.getCount(); i++) + int j = _setAircrafts.getCount() - 1; + for (int i = 0; i < _setAircrafts.getCount(); i++) { - if (_setCars.Get(i) == null) + if (_setAircrafts.Get(i) == null) { for (; j > i; j--) { - var car = _setCars.Get(j); + var car = _setAircrafts.Get(j); if (car != null) { - _setCars.Insert(car, i); - _setCars.Remove(j); + _setAircrafts.Insert(car, i); + _setAircrafts.Remove(j); break; } } @@ -137,12 +168,12 @@ public class MapWithSetAircraftsGeneric getKeys() { return _mapStorages.keySet().stream().toList(); } public MapsCollection(int pictureWidth, int pictureHeight) @@ -16,6 +24,137 @@ public class MapsCollection _pictureHeight = pictureHeight; } + public boolean SaveData(String filename) + { + File file = new File(filename); + if (file.exists()) file.delete(); + + try { + file.createNewFile(); + + FileWriter writer = new FileWriter(file); + writer.append("MapsCollection\n"); + + for(String key : _mapStorages.keySet()) { + String record = key + separatorDict + _mapStorages.get(key).GetData(separatorDict, separatorData) + "\n"; + writer.append(record); + } + + writer.flush(); + writer.close(); + } catch(Exception err) { + return false; + } + + return true; + } + + public boolean LoadData(String filename) + { + File file = new File(filename); + if (!file.exists()) return false; + + try { + BufferedReader reader = new BufferedReader(new FileReader(file)); + String current = reader.readLine(); + + if (!current.equals("MapsCollection")) return false; + _mapStorages.clear(); + + while ((current = reader.readLine()) != null) + { + String elem[] = current.split("\\" + separatorDict); + AbstractMap map = null; + switch (elem[1]) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MyMap": + map = new MyMap(); + break; + } + _mapStorages.put(elem[0], new MapWithSetAircraftsGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.get(elem[0]).LoadData(elem[2].split(separatorData + "")); + } + + reader.close(); + } catch(Exception err) { + return false; + } + + return true; + } + + + public boolean SaveMap(String filename, String key) { + File file = new File(filename); + if(file.exists()) file.delete(); + + MapWithSetAircraftsGeneric item = _mapStorages.getOrDefault(key, null); + if(item == null) return false; + + try { + file.createNewFile(); + FileWriter writer = new FileWriter(file); + + writer.append("Map\n"); + writer.append(key + "\n"); + writer.append(item.getMap().getClass().getName() + "\n"); + ListIterator iter = item.getAircraftsIter(); + while(iter.hasNext()) { + writer.append(AircraftFactory.getDataForSave(iter.next().getAircraft()) + "\n"); + } + + writer.flush(); + writer.close(); + } catch(Exception err) { + return false; + } + + return true; + } + + public boolean LoadMap(String filename) { + File file = new File(filename); + if (!file.exists()) return false; + + try { + BufferedReader reader = new BufferedReader(new FileReader(file)); + String format = reader.readLine(); + if (!format.equals("Map")) return false; + + String key = reader.readLine(); + String mapStr = reader.readLine(); + + AbstractMap map = null; + + switch (mapStr) + { + case "SimpleMap": + map = new SimpleMap(); + break; + case "MyMap": + map = new MyMap(); + break; + } + + if(_mapStorages.containsKey(key)) _mapStorages.get(key).ClearMap(); + else _mapStorages.put(key, new MapWithSetAircraftsGeneric<>(_pictureWidth, _pictureHeight, map)); + + String current = null; + while ((current = reader.readLine()) != null) + { + _mapStorages.get(key).addAircraft(DrawingObjectAircraft.Create(current)); + } + reader.close(); + } catch(Exception err) { + return false; + } + + return true; + } + public void AddMap(String name, AbstractMap map) { // TODO Прописать логику для добавления diff --git a/SetAircraftsGeneric.java b/SetAircraftsGeneric.java index fbbc5fd..86d854f 100644 --- a/SetAircraftsGeneric.java +++ b/SetAircraftsGeneric.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.ListIterator; public class SetAircraftsGeneric { @@ -34,6 +35,14 @@ public class SetAircraftsGeneric return res; } + public ListIterator GetElems() { + return _places.listIterator(); + } + + public void Clear() { + _places.clear(); + } + public T Get(int position) { return _places.get(position);