Compare commits

..

No commits in common. "d5533765089eb9754adcf8313078684bb225d070" and "ade75a5dc3283ab638a8d4cf5299dd14c5b3173e" have entirely different histories.

7 changed files with 4 additions and 255 deletions

View File

@ -1,10 +1,10 @@
import java.awt.*; import java.awt.*;
public class ArtillerySerde { // Artillery Serialization/Deserialization public class ArtillerySerde { // Artillery Serialization/Deserialization
private static final char _separatorForObject = ':'; private static final String _separatorForObject = ":";
public static DrawingArtillery deserialize(String info) { public static DrawingArtillery deserialize(String info) {
String[] strings = info.split(Character.toString(_separatorForObject)); String[] strings = info.split(_separatorForObject);
int speed = Integer.parseInt(strings[0]); int speed = Integer.parseInt(strings[0]);
float weight = Float.parseFloat(strings[1]); float weight = Float.parseFloat(strings[1]);
@ -13,7 +13,6 @@ public class ArtillerySerde { // Artillery Serialization/Deserialization
case "DrawingRollers" -> new DrawingRollers(Integer.parseInt(strings[4]), bodyColor); case "DrawingRollers" -> new DrawingRollers(Integer.parseInt(strings[4]), bodyColor);
case "DrawingCrossRollers" -> new DrawingCrossRollers(Integer.parseInt(strings[4]), bodyColor); case "DrawingCrossRollers" -> new DrawingCrossRollers(Integer.parseInt(strings[4]), bodyColor);
case "DrawingSquaredRollers" -> new DrawingSquaredRollers(Integer.parseInt(strings[4]), bodyColor); case "DrawingSquaredRollers" -> new DrawingSquaredRollers(Integer.parseInt(strings[4]), bodyColor);
default -> null;
}; };
if (strings.length == 5) { if (strings.length == 5) {
@ -39,7 +38,7 @@ public class ArtillerySerde { // Artillery Serialization/Deserialization
EntityArtillery artillery = drawingArtillery.getArtillery(); EntityArtillery artillery = drawingArtillery.getArtillery();
String result = String.format( String result = String.format(
"%d%c%s%c%d%c%s%c%d", "%d%s%f%s%d%s%s%s%d",
artillery.getSpeed(), artillery.getSpeed(),
_separatorForObject, _separatorForObject,
artillery.getWeight(), artillery.getWeight(),
@ -56,7 +55,7 @@ public class ArtillerySerde { // Artillery Serialization/Deserialization
} }
return String.format( return String.format(
"%s%c%d%c%b%c%b", "%s%s%d%s%b%s%b",
result, result,
_separatorForObject, _separatorForObject,
advanced.getDopColor().getRGB(), advanced.getDopColor().getRGB(),

View File

@ -38,16 +38,4 @@ public class DrawingObjectArtillery implements IDrawingObject {
public void drawingObject(Graphics2D g) { public void drawingObject(Graphics2D g) {
_artillery.drawTransport(g); _artillery.drawTransport(g);
} }
public String getInfo() {
if (_artillery == null) {
return null;
}
return ArtillerySerde.serialize(_artillery);
}
public static IDrawingObject create(String data) {
return new DrawingObjectArtillery(ArtillerySerde.deserialize(data));
}
} }

View File

@ -1,16 +1,13 @@
import javax.swing.*; import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter; import javax.swing.text.MaskFormatter;
import java.awt.*; import java.awt.*;
import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Optional; import java.util.Optional;
import java.util.Stack; import java.util.Stack;
public class FormMapWithSetArtilleries extends JFrame { public class FormMapWithSetArtilleries extends JFrame {
private JMenuBar menuBar;
private JPanel pictureBox; private JPanel pictureBox;
private JPanel toolsGroup; private JPanel toolsGroup;
private JLabel toolsLabel; private JLabel toolsLabel;
@ -55,87 +52,6 @@ public class FormMapWithSetArtilleries extends JFrame {
_mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight()); _mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight());
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Файл");
menuBar.add(fileMenu);
JMenuItem saveMenuItem = new JMenuItem("Сохранить");
saveMenuItem.addActionListener(e -> {
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt"));
dialog.showSaveDialog(this);
try {
if (_mapsCollection.saveData(dialog.getSelectedFile().getAbsolutePath())) {
JOptionPane.showMessageDialog(this, "Сохранение прошло успешно", "Успех", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Не сохранилось", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
fileMenu.add(saveMenuItem);
JMenuItem loadMenuItem = new JMenuItem("Загрузить");
loadMenuItem.addActionListener(e -> {
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt"));
dialog.showOpenDialog(this);
try {
if (_mapsCollection.loadData(dialog.getSelectedFile().getAbsolutePath())) {
reloadMaps();
JOptionPane.showMessageDialog(this, "Загрузка прошла успешно", "Успех", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Не загрузилось", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
fileMenu.add(loadMenuItem);
JMenuItem saveMapMenuItem = new JMenuItem("Сохранить карту");
saveMapMenuItem.addActionListener(e -> {
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt"));
dialog.showSaveDialog(this);
try {
if (_mapsCollection.saveMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse(""), dialog.getSelectedFile().getAbsolutePath())) {
JOptionPane.showMessageDialog(this, "Сохранение прошло успешно", "Успех", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Не сохранилось", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
fileMenu.add(saveMapMenuItem);
JMenuItem loadMapMenuItem = new JMenuItem("Загрузить карту");
loadMapMenuItem.addActionListener(e -> {
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt"));
dialog.showOpenDialog(this);
try {
if (_mapsCollection.loadMap(dialog.getSelectedFile().getAbsolutePath())) {
reloadMaps();
JOptionPane.showMessageDialog(this, "Загрузка прошла успешно", "Успех", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Не загрузилось", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
fileMenu.add(loadMapMenuItem);
setJMenuBar(menuBar);
comboBoxMapSelector.removeAllItems(); comboBoxMapSelector.removeAllItems();
for (var key : _mapsDict.keySet()) { for (var key : _mapsDict.keySet()) {
comboBoxMapSelector.addItem(key); comboBoxMapSelector.addItem(key);

View File

@ -6,5 +6,4 @@ public interface IDrawingObject {
void moveObject(Direction direction); void moveObject(Direction direction);
void drawingObject(Graphics2D g); void drawingObject(Graphics2D g);
float[] getCurrentPosition(); float[] getCurrentPosition();
String getInfo();
} }

View File

@ -19,10 +19,6 @@ public class MapWithSetArtilleriesGeneric<T extends IDrawingObject, U extends Ab
_map = map; _map = map;
} }
public U getMap() {
return _map;
}
public int addArtillery(T artillery) { public int addArtillery(T artillery) {
return _setArtilleries.insert(artillery); return _setArtilleries.insert(artillery);
} }
@ -128,21 +124,4 @@ public class MapWithSetArtilleriesGeneric<T extends IDrawingObject, U extends Ab
index++; index++;
} }
} }
public String getData(char separatorType, char separatorData) {
StringBuilder data = new StringBuilder(String.format("%s%c", _map.getClass().getSimpleName(), separatorType));
for (var artillery : _setArtilleries.getArtilleries()) {
data.append(String.format("%s%c", artillery.getInfo(), separatorData));
}
return data.toString();
}
@SuppressWarnings("unchecked")
public void loadData(String[] records) {
for (int i = records.length - 1; i >= 0; i--) {
_setArtilleries.insert((T) DrawingObjectArtillery.create(records[i]));
}
}
} }

View File

@ -1,4 +1,3 @@
import java.io.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set; import java.util.Set;
@ -8,9 +7,6 @@ public class MapsCollection {
private final int _pictureWidth; private final int _pictureWidth;
private final int _pictureHeight; private final int _pictureHeight;
private final char separatorDict = '|';
private final char separatorData = ';';
public Set<String> getKeys() { public Set<String> getKeys() {
return _mapsStorage.keySet(); return _mapsStorage.keySet();
} }
@ -42,128 +38,4 @@ public class MapsCollection {
} }
return null; return null;
} }
@SuppressWarnings("ResultOfMethodCallIgnored")
public boolean saveData(String filename) throws IOException {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
file.createNewFile();
try (PrintWriter writer = new PrintWriter(file)) {
writer.println("MapsCollection");
for (var storage : _mapsStorage.entrySet()) {
writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData)));
}
}
return true;
}
public boolean loadData(String filename) throws IOException {
File file = new File(filename);
if (!file.exists()) {
return false;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String currentLine = reader.readLine();
if (currentLine == null || !currentLine.contains("MapsCollection")) {
return false;
}
_mapsStorage.clear();
while ((currentLine = reader.readLine()) != null) {
var elements = currentLine.split(String.format("\\%c", separatorDict));
AbstractMap map = switch (elements[1]) {
case "SimpleMap" -> new SimpleMap();
case "ForestMap" -> new ForestMap();
default -> null;
};
_mapsStorage.put(elements[0], new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map));
_mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "\n?"));
}
}
return true;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public boolean saveMap(String mapName, String filename) throws IOException {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
file.createNewFile();
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map = _mapsStorage.getOrDefault(mapName, null);
if (map == null) {
return false;
}
try (PrintWriter writer = new PrintWriter(file)) {
writer.println("Map");
writer.println(mapName);
writer.println(map.getMap().getClass().getSimpleName());
for (var artillery : map._setArtilleries.getArtilleries()) {
writer.println(artillery.getInfo());
}
}
return true;
}
public boolean loadMap(String filename) throws IOException {
File file = new File(filename);
if (!file.exists()) {
return false;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String currentLine = reader.readLine();
if (currentLine == null || !currentLine.contains("Map")) {
return false;
}
String mapName = reader.readLine();
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map;
if (_mapsStorage.containsKey(mapName)) {
map = _mapsStorage.get(mapName);
if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) {
return false;
}
} else {
map = switch (reader.readLine()) {
case "SimpleMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new SimpleMap());
case "ForestMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new ForestMap());
default -> null;
};
}
map._setArtilleries.clear();
while ((currentLine = reader.readLine()) != null) {
map._setArtilleries.insert(DrawingObjectArtillery.create(currentLine));
}
_mapsStorage.put(mapName, map);
}
return true;
}
} }

View File

@ -53,8 +53,4 @@ public class SetArtilleriesGeneric<T> {
public Iterable<T> getArtilleries() { public Iterable<T> getArtilleries() {
return () -> _places.stream().filter(Objects::nonNull).iterator(); return () -> _places.stream().filter(Objects::nonNull).iterator();
} }
public void clear() {
_places.clear();
}
} }