Polevoy S.V Lab_work6 #7

Closed
ChipsEater wants to merge 9 commits from LabWork06 into LabWork05
3 changed files with 54 additions and 3 deletions
Showing only changes of commit 9b23e2f119 - Show all commits

View File

@ -13,6 +13,7 @@ public class ArtillerySerde { // Artillery Serialization/Deserialization
case "DrawingRollers" -> new DrawingRollers(Integer.parseInt(strings[4]), bodyColor);
case "DrawingCrossRollers" -> new DrawingCrossRollers(Integer.parseInt(strings[4]), bodyColor);
case "DrawingSquaredRollers" -> new DrawingSquaredRollers(Integer.parseInt(strings[4]), bodyColor);
default -> null;
};
if (strings.length == 5) {
@ -38,7 +39,7 @@ public class ArtillerySerde { // Artillery Serialization/Deserialization
EntityArtillery artillery = drawingArtillery.getArtillery();
String result = String.format(
"%d%c%f%c%d%c%s%c%d",
"%d%c%s%c%d%c%s%c%d",
artillery.getSpeed(),
_separatorForObject,
artillery.getWeight(),

View File

@ -1,13 +1,16 @@
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Optional;
import java.util.Stack;
public class FormMapWithSetArtilleries extends JFrame {
private JMenuBar menuBar;
private JPanel pictureBox;
private JPanel toolsGroup;
private JLabel toolsLabel;
@ -52,6 +55,50 @@ public class FormMapWithSetArtilleries extends JFrame {
_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);
setJMenuBar(menuBar);
comboBoxMapSelector.removeAllItems();
for (var key : _mapsDict.keySet()) {
comboBoxMapSelector.addItem(key);

View File

@ -59,6 +59,8 @@ public class MapsCollection {
for (var storage : _mapsStorage.entrySet()) {
writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData)));
}
writer.flush();
}
return true;
@ -80,15 +82,16 @@ public class MapsCollection {
_mapsStorage.clear();
while ((currentLine = reader.readLine()) != null) {
var elements = currentLine.split(Character.toString(separatorDict));
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?)"));
_mapsStorage.get(elements[0]).loadData(elements[2].split(separatorData + "\n?"));
}
}