From 9f39107385b474f7292064840140de72616a3a33 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 19:32:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B1=D0=B0=D0=B7=D0=B0=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B16?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 7 ++++--- ExtraRoundWheelDraw.java | 4 ++-- ExtraStarWheelDraw.java | 4 ++-- FormLocomotive.java | 1 - FormMapWithSetLocomotives.java | 30 +++++++++++++++++++++++++++++- MapWithSetLocomotivesGeneric.java | 1 - MapsCollection.java | 12 ++++++------ 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 8f7e68d..4cbd4f3 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -1,4 +1,5 @@ import java.awt.*; +import java.util.Objects; import java.util.Random; public class DrawningLocomotive { @@ -177,9 +178,9 @@ public class DrawningLocomotive { IDrawningExtra drawningExtra = null; EntityLocomotive Locomotive = null; String[] strs = info.split(Character.toString(_separatorForObject)); - if (strs[5] == "Simple") drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); - if (strs[5] == "Star") drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); - if (strs[5] == "Round") drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); + if (strs[5].equals("Simple")) drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4]))); + if (strs[5].equals("Star")) drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4]))); + if (Objects.equals(strs[5], "Round")) drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4]))); if (drawningExtra == null) return null; if (strs.length == 7) { diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java index 8824c27..e578f0b 100644 --- a/ExtraRoundWheelDraw.java +++ b/ExtraRoundWheelDraw.java @@ -6,11 +6,11 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{ private Color color; public void setExtraNum(int num) { switch (num) { - case 0: { + case 3: { wheelsCount = WheelsCount.Three; break; } - case 1: { + case 4: { wheelsCount = WheelsCount.Four; break; } diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java index a1ca0d5..b8ca923 100644 --- a/ExtraStarWheelDraw.java +++ b/ExtraStarWheelDraw.java @@ -6,11 +6,11 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ private Color color; public void setExtraNum(int num) { switch (num) { - case 0: { + case 3: { wheelsCount = WheelsCount.Three; break; } - case 1: { + case 4: { wheelsCount = WheelsCount.Four; break; } diff --git a/FormLocomotive.java b/FormLocomotive.java index 57fd25a..765b90b 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -108,7 +108,6 @@ public class FormLocomotive extends JComponent{ super.repaint(); } public static void main(String[] args) { - new FormMapWithSetLocomotives(); } } diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index ce8dfab..d30af27 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -263,7 +263,35 @@ public class FormMapWithSetLocomotives extends JComponent { dialog.setVisible(true); } }); - statusPanel.add(showDeletedButton); + + JFileChooser fileChooser = new JFileChooser(); + JButton saveButton = new JButton("Save"); + saveButton.addActionListener(e -> { + fileChooser.setDialogTitle("Saving"); + int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this); + if (result == JFileChooser.APPROVE_OPTION ) { + if (_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Save success"); + } + else JOptionPane.showMessageDialog(null, "Save failed"); + } + + }); + statusPanel.add(saveButton); + JButton loadButton = new JButton("Load"); + loadButton.addActionListener(e -> { + fileChooser.setDialogTitle("Loading"); + int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this); + if (result == JFileChooser.APPROVE_OPTION ) { + if (_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Load success"); + } + else JOptionPane.showMessageDialog(null, "Load failed"); + } + ReloadMaps(); + repaint(); + }); + statusPanel.add(loadButton); formFrame.getContentPane().add(this); formFrame.setVisible(true); diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java index c0ad0b7..0df89e8 100644 --- a/MapWithSetLocomotivesGeneric.java +++ b/MapWithSetLocomotivesGeneric.java @@ -180,7 +180,6 @@ public class MapWithSetLocomotivesGeneric /// Получение данных в виде строки public String GetData(char separatorType, char separatorData) { - //string data = $"{_map.GetType().Name}{separatorType}"; String data = ""+ _map.getClass() + separatorType; for (var locomotive : _setLocomotives.GetLocomotives()) { diff --git a/MapsCollection.java b/MapsCollection.java index 2077e27..ccdcd81 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -15,7 +15,7 @@ public class MapsCollection { /// Высота окна отрисовки private final int _pictureHeight; // Сепараторы - private final char separatorDict = '|'; + private final String separatorDict = "\\|"; private final char separatorData = ';'; /// Конструктор public MapsCollection(int pictureWidth, int pictureHeight) @@ -60,7 +60,7 @@ public class MapsCollection { try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) { bw.write("MapsCollection\n"); for (var storage : _mapStorages.entrySet()) { - bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict, separatorData) + "\n"); + bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict.charAt(0), separatorData) + "\n"); } } catch (IOException ex) { @@ -86,18 +86,18 @@ public class MapsCollection { _mapStorages.clear(); while ((curLine = br.readLine()) != null) { - var elems = curLine.split(Character.toString(separatorDict)); + var elems = curLine.split(separatorDict); AbstractMap map = null; switch (elems[1]) { //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - case "SimpleMap": + case "class SimpleMap": map = new SimpleMap(); break; - case "SpikeMap": + case "class SpikeMap": map = new SpikeMap(); break; - case "RailMap": + case "class RailMap": map = new RailMap(); break; }