Lab6 finished
This commit is contained in:
parent
67bf8a6833
commit
7491ceaca0
@ -1,16 +1,127 @@
|
||||
package Generics;
|
||||
|
||||
import drawing_objects.DrawingPlane;
|
||||
import drawing_objects.ExtentionDrawingPlane;
|
||||
import movement_strategy.DrawingObjectPlane;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PlaneGenericStorage {
|
||||
final HashMap<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> planeStorages;
|
||||
public HashMap<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> planeStorages;
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
|
||||
private static final char _separatorForKeyValue = '|';
|
||||
private final char _separatorRecords = ';';
|
||||
private static final char _separatorForObject = ':';
|
||||
|
||||
public void SaveData(File f) throws IOException {
|
||||
if(f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append("PlaneStorage\n");
|
||||
for(Map.Entry<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> record : planeStorages.entrySet()){
|
||||
StringBuilder records = new StringBuilder();
|
||||
for(DrawingPlane elem : record.getValue().GetPlanes())
|
||||
{
|
||||
records.append(String.format("%s%c", ExtentionDrawingPlane.GetDataForSave(elem, _separatorForObject),
|
||||
_separatorRecords));
|
||||
}
|
||||
data.append(String.format("%s%c%s\n", record.getKey(), _separatorForKeyValue, records.toString()));
|
||||
}
|
||||
if(data.length() == 0)
|
||||
return;
|
||||
FileWriter writer = new FileWriter(f);
|
||||
writer.write(data.toString());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
public boolean LoadData(File f) throws FileNotFoundException {
|
||||
if(!f.exists())
|
||||
return false;
|
||||
System.out.println("Gay");
|
||||
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("PlaneStorage"))
|
||||
return false;
|
||||
planeStorages.clear();
|
||||
for(String data : strs){
|
||||
String st = new String("\\" + Character.toString( _separatorForKeyValue));
|
||||
String[]record = data.split(st);
|
||||
if (record.length != 2)
|
||||
continue;
|
||||
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> collection =
|
||||
new PlanesGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
String[] set = record[1].split(Character.toString(_separatorRecords));
|
||||
|
||||
for(int i = set.length -1; i >=0; i--){
|
||||
String elem = set[i];
|
||||
DrawingPlane plane = ExtentionDrawingPlane.CreateDrawingPlane(elem,
|
||||
_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (plane != null)
|
||||
{
|
||||
if (!(collection.Insert(plane)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
planeStorages.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("PlaneCollection"))
|
||||
return false;
|
||||
String collectionName = strs[1];
|
||||
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> collection = getCollection(collectionName);
|
||||
if(collection == null)
|
||||
collection = new PlanesGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
else
|
||||
collection.Clear();
|
||||
String[] planesInfo = strs[2].split(Character.toString(PlanesGenericCollection._separatorRecords));
|
||||
for(int i = planesInfo.length-1; i >= 0; i--){
|
||||
String data = planesInfo[i];
|
||||
DrawingPlane plane = ExtentionDrawingPlane.CreateDrawingPlane(data,
|
||||
PlanesGenericCollection._separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (plane != null)
|
||||
{
|
||||
if (!(collection.Insert(plane)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
AddSetFromFile(collectionName, collection);
|
||||
return true;
|
||||
}
|
||||
|
||||
public PlaneGenericStorage(int pictureWidth, int pictureHeight){
|
||||
planeStorages = new HashMap<>();
|
||||
_pictureWidth = pictureWidth;
|
||||
@ -25,12 +136,26 @@ public class PlaneGenericStorage {
|
||||
planeStorages.put(name, new PlanesGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
|
||||
public void DelSet(String name){
|
||||
public void AddSetFromFile(String name, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> toAdd){
|
||||
if(planeStorages.containsKey(name)){
|
||||
planeStorages.remove(name);
|
||||
}
|
||||
planeStorages.put(name, toAdd);
|
||||
}
|
||||
|
||||
public void DelSet(String name, PlaneTrashCollection<DrawingPlane> trashBox){
|
||||
if(!planeStorages.containsKey(name))
|
||||
return;
|
||||
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> cur = planeStorages.get(name);
|
||||
for(int i = 0; i < cur.size(); i++)
|
||||
trashBox.Push(cur.Get(i));
|
||||
planeStorages.remove(name);
|
||||
}
|
||||
|
||||
public PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> GetCollection(String collectionName){
|
||||
return planeStorages.get(collectionName);
|
||||
}
|
||||
|
||||
public PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> getCollection(String name){
|
||||
if(!planeStorages.containsKey(name))
|
||||
return null;
|
||||
|
@ -1,11 +1,18 @@
|
||||
package Generics;
|
||||
|
||||
import drawing_objects.DrawingPlane;
|
||||
import drawing_objects.ExtentionDrawingPlane;
|
||||
import movement_strategy.IMoveableObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveableObject> {
|
||||
public static char _separatorRecords = ';';
|
||||
public static char _separatorForObject = ':';
|
||||
/// <summary>
|
||||
/// Ширина окна прорисовки
|
||||
/// </summary>
|
||||
@ -13,7 +20,7 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
|
||||
/// <summary>
|
||||
/// Высота окна прорисовки
|
||||
/// </summary>
|
||||
private final int _pictureHeight;
|
||||
private final int pictureHeight;
|
||||
/// <summary>
|
||||
/// Размер занимаемого объектом места (ширина)
|
||||
/// </summary>
|
||||
@ -25,7 +32,38 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
|
||||
/// <summary>
|
||||
/// Набор объектов
|
||||
/// </summary>
|
||||
private final SetGeneric<T> collection;
|
||||
private SetGeneric<T> collection;
|
||||
public ArrayList<T> GetPlanes(){
|
||||
return collection.getPlanes(collection.getCount());
|
||||
}
|
||||
|
||||
public boolean SaveData(File f, String name) throws IOException {
|
||||
if(f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append("PlaneCollection\n");
|
||||
data.append(String.format("%s\n", name));
|
||||
StringBuilder records = new StringBuilder();
|
||||
for(DrawingPlane elem : GetPlanes())
|
||||
{
|
||||
records.append(String.format("%s%c", ExtentionDrawingPlane.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 void Clear(){
|
||||
collection = new SetGeneric<>(pictureWidth * pictureHeight);
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -36,7 +74,7 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
pictureHeight = picHeight;
|
||||
collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
|
||||
@ -89,13 +127,13 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
|
||||
g.setStroke(pen);
|
||||
for (int i = 0; i < pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||
for (int j = 0; j < pictureHeight / _placeSizeHeight + 1; ++j)
|
||||
{//линия разметки места
|
||||
g.drawLine(i * _placeSizeWidth + 10, j * _placeSizeHeight +5,
|
||||
i * _placeSizeWidth + _placeSizeWidth / 2 + 50, j * _placeSizeHeight +5);
|
||||
}
|
||||
g.drawLine(i * _placeSizeWidth + 10, 5, i * _placeSizeWidth + 10,
|
||||
_pictureHeight / _placeSizeHeight * _placeSizeHeight +5);
|
||||
pictureHeight / _placeSizeHeight * _placeSizeHeight +5);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
|
@ -13,7 +13,7 @@ public class DrawingPlane {
|
||||
public EntityPlane getEntityPlane() {
|
||||
return entityPlane;
|
||||
}
|
||||
private IDrawEngines drawingEngines;
|
||||
public IDrawEngines drawingEngines;
|
||||
public int pictureWidth;
|
||||
public int pictureHeight;
|
||||
protected int startPosX;
|
||||
|
79
src/drawing_objects/ExtentionDrawingPlane.java
Normal file
79
src/drawing_objects/ExtentionDrawingPlane.java
Normal file
@ -0,0 +1,79 @@
|
||||
package drawing_objects;
|
||||
|
||||
import entities.EntityAirBomber;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class ExtentionDrawingPlane {
|
||||
private static String getName(Color col){
|
||||
if(col.equals(Color.RED))
|
||||
return "RED";
|
||||
if(col.equals(Color.GREEN))
|
||||
return "GREEN";
|
||||
if(col.equals(Color.BLUE))
|
||||
return "BLUE";
|
||||
if(col.equals(Color.YELLOW))
|
||||
return "YELLOW";
|
||||
if(col.equals(Color.WHITE))
|
||||
return "WHITE";
|
||||
if(col.equals(Color.GRAY))
|
||||
return "GRAY";
|
||||
if(col.equals(Color.BLACK))
|
||||
return "BLACK";
|
||||
if(col.equals(Color.MAGENTA))
|
||||
return "MAGENTA";
|
||||
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("MAGENTA"))
|
||||
return Color.MAGENTA;
|
||||
return null;
|
||||
}
|
||||
public static DrawingPlane CreateDrawingPlane(String info, char separatorForObject,
|
||||
int width, int height){
|
||||
String[] strs = info.split(Character.toString(separatorForObject));
|
||||
if(strs.length == 5){
|
||||
return new DrawingPlane(Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]), getColor(strs[2]), width, height, Integer.parseInt(strs[3]), Integer.parseInt(strs[4]));
|
||||
}
|
||||
if(strs.length == 8){
|
||||
return new DrawingAirBomber(Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]), getColor(strs[2]),
|
||||
getColor(strs[5]), Boolean.parseBoolean(strs[6]),
|
||||
Boolean.parseBoolean(strs[7]), width, height,
|
||||
Integer.parseInt(strs[3]), Integer.parseInt(strs[4]));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String GetDataForSave(DrawingPlane drawingPlane, char separatorForObject){
|
||||
var plane = drawingPlane;
|
||||
var entity = plane.entityPlane;
|
||||
if(entity == null)
|
||||
return null;
|
||||
var str = String.format("%d%c%d%c%s%c%d%c%d", entity.getSpeed(), separatorForObject, (int)entity.getWeight(),
|
||||
separatorForObject, getName(entity.bodyColor), separatorForObject, plane.drawingEngines.getType(), separatorForObject, plane.drawingEngines.getNumber());
|
||||
if(!(entity instanceof EntityAirBomber)){
|
||||
return str;
|
||||
}
|
||||
var nstr = String.format("%s%c%s%c%b%c%b", str, separatorForObject,
|
||||
getName(((EntityAirBomber) entity).additionalColor), separatorForObject,
|
||||
((EntityAirBomber) entity).getBombs(), separatorForObject,
|
||||
((EntityAirBomber) entity).getFuel());
|
||||
return nstr;
|
||||
}
|
||||
}
|
@ -9,8 +9,10 @@ import Generics.PlanesGenericCollection;
|
||||
import PlaneHard.FormPlaneConfig;
|
||||
import drawing_objects.DrawingPlane;
|
||||
import movement_strategy.DrawingObjectPlane;
|
||||
import form.TxtSaveFilter;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
@ -46,6 +48,22 @@ public class FramePlaneCollection extends JFrame {
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
JMenuBar menuFile = new JMenuBar();
|
||||
JMenu file = new JMenu("Файл");
|
||||
menuFile.add(file);
|
||||
JMenuItem saveFile = new JMenuItem("Сохранить");
|
||||
saveFile.addActionListener(e -> saveFile_Click());
|
||||
JMenuItem loadFile = new JMenuItem("Загрузить");
|
||||
loadFile.addActionListener(e -> loadFile_Click());
|
||||
JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию");
|
||||
saveCollection.addActionListener(e -> saveCollection_Click());
|
||||
JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию");
|
||||
loadCollection.addActionListener(e -> loadCollection_Click());
|
||||
file.add(saveCollection);
|
||||
file.add(loadCollection);
|
||||
file.add(saveFile);
|
||||
file.add(loadFile);
|
||||
super.setJMenuBar(menuFile);
|
||||
pictureBoxCollection.setPreferredSize(new Dimension(700, 600));
|
||||
JButton buttonAddPlane = new JButton("Добавить самолет");
|
||||
textFieldNumber = new TextField();
|
||||
@ -114,6 +132,73 @@ public class FramePlaneCollection extends JFrame {
|
||||
add(panelTools, BorderLayout.EAST);
|
||||
add(pictureBoxCollection, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private void saveFile_Click(){
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\123\\Desktop");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void loadFile_Click() {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\123\\Desktop");
|
||||
int ret = fc.showDialog(null, "Открыть файл");
|
||||
if(ret == JFileChooser.APPROVE_OPTION){
|
||||
File file = fc.getSelectedFile();
|
||||
try {
|
||||
storage.LoadData(file);
|
||||
reloadObjects();
|
||||
super.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void saveCollection_Click() {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\123\\Desktop");
|
||||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||||
int retrieval = fc.showSaveDialog(null);
|
||||
|
||||
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||||
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||||
|
||||
try {
|
||||
if(listStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
storage.planeStorages.get(listStorages.getSelectedValue()).SaveData(file, listStorages.getSelectedValue());
|
||||
reloadObjects();
|
||||
super.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadCollection_Click() {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\123\\Desktop");
|
||||
int ret = fc.showDialog(null, "Открыть файл");
|
||||
if(ret == JFileChooser.APPROVE_OPTION){
|
||||
File file = fc.getSelectedFile();
|
||||
try {
|
||||
storage.LoadCollection(file);
|
||||
reloadObjects();
|
||||
super.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void reloadObjects(){
|
||||
int index = listStorages.getSelectedIndex();
|
||||
listModel.clear();
|
||||
@ -142,7 +227,7 @@ public class FramePlaneCollection extends JFrame {
|
||||
public void buttonDeleteSet_Click() {
|
||||
if (listStorages.getSelectedIndex() == -1)
|
||||
return;
|
||||
storage.DelSet(listStorages.getSelectedValue());
|
||||
storage.DelSet(listStorages.getSelectedValue(), trashCollection);
|
||||
reloadObjects();
|
||||
}
|
||||
public void buttonAddPlaneClick() {
|
||||
|
20
src/form/TxtSaveFilter.java
Normal file
20
src/form/TxtSaveFilter.java
Normal file
@ -0,0 +1,20 @@
|
||||
package form;
|
||||
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import java.io.File;
|
||||
|
||||
public 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";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user