Graphic Rendering System 1.0
A Java-based graphic rendering system implementing various design patterns
Loading...
Searching...
No Matches
FileManager.java
Go to the documentation of this file.
1package com.example.graphics.util;
2
3import com.example.graphics.Drawing;
4import com.example.graphics.factory.ShapeFactory;
5import com.example.graphics.model.Shape;
6import org.json.JSONArray;
7import org.json.JSONObject;
8
9import java.io.*;
10import java.nio.file.Files;
11import java.nio.file.Paths;
12import java.util.ArrayList;
13import java.util.List;
14
18public class FileManager {
20
24 public FileManager() {
25 this.shapeFactory = new ShapeFactory();
26 }
27
34 public void saveDrawingBinary(Drawing drawing, String filePath) throws IOException {
35 try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
36 // 创建一个新的列表,只包含形状
37 List<Shape> shapes = new ArrayList<>(drawing.getShapes());
38 oos.writeObject(shapes);
39 }
40 }
41
49 @SuppressWarnings("unchecked")
50 public List<Shape> loadDrawingBinary(String filePath) throws IOException, ClassNotFoundException {
51 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
52 return (List<Shape>) ois.readObject();
53 }
54 }
55
62 public void saveDrawingJson(String jsonData, String filePath) throws IOException {
63 Files.write(Paths.get(filePath), jsonData.getBytes());
64 }
65
72 public List<Shape> loadDrawingJson(String filePath) throws IOException {
73 String content = new String(Files.readAllBytes(Paths.get(filePath)));
74 JSONObject jsonObject = new JSONObject(content);
75 JSONArray shapesArray = jsonObject.getJSONArray("shapes");
76
77 List<Shape> shapes = new ArrayList<>();
78
79 for (int i = 0; i < shapesArray.length(); i++) {
80 JSONObject shapeJson = shapesArray.getJSONObject(i);
81 String type = shapeJson.getString("type");
82
83 Shape shape = null;
84 switch (type) {
85 case "circle":
86 shape = shapeFactory.createCircle(
87 shapeJson.getInt("x"),
88 shapeJson.getInt("y"),
89 shapeJson.getInt("radius")
90 );
91 break;
92 case "rectangle":
93 shape = shapeFactory.createRectangle(
94 shapeJson.getInt("x"),
95 shapeJson.getInt("y"),
96 shapeJson.getInt("width"),
97 shapeJson.getInt("height")
98 );
99 break;
100 case "line":
101 shape = shapeFactory.createLine(
102 shapeJson.getInt("x1"),
103 shapeJson.getInt("y1"),
104 shapeJson.getInt("x2"),
105 shapeJson.getInt("y2")
106 );
107 break;
108 case "triangle":
109 shape = shapeFactory.createTriangle(
110 shapeJson.getInt("x1"),
111 shapeJson.getInt("y1"),
112 shapeJson.getInt("x2"),
113 shapeJson.getInt("y2"),
114 shapeJson.getInt("x3"),
115 shapeJson.getInt("y3")
116 );
117 break;
118 }
119
120 if (shape != null) {
121 shapes.add(shape);
122 }
123 }
124
125 return shapes;
126 }
127
134 public void saveDrawingXml(String xmlData, String filePath) throws IOException {
135 Files.write(Paths.get(filePath), xmlData.getBytes());
136 }
137}
void saveDrawingBinary(Drawing drawing, String filePath)
void saveDrawingXml(String xmlData, String filePath)
void saveDrawingJson(String jsonData, String filePath)
List< Shape > loadDrawingBinary(String filePath)
List< Shape > loadDrawingJson(String filePath)