-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeManager.java
More file actions
221 lines (195 loc) · 5.71 KB
/
Copy pathShapeManager.java
File metadata and controls
221 lines (195 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.LinkedList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ShapeManager
{
private PaintMainWindow mMainWindow;
private Color mCurPenColor = Color.BLACK;
private Color mCurFillColor = null;
private LineThickness mCurLineThickness = LineThickness.THIN_LINE;
private ShapeID mCurShapeID = ShapeID.RECTANGLE;
private LinkedList<Shape> mShapeList = new LinkedList<Shape>();
private LinkedList<Shape> mUndoList = new LinkedList<Shape>();
private Shape mTempShape;
public ShapeManager(PaintMainWindow mainWindow)
{
mMainWindow = mainWindow;
}
public void setShapeID(ShapeID shapeID)
{
mCurShapeID = shapeID;
}
public ShapeID getCurShapeID()
{
return mCurShapeID;
}
public void setPenColor(Color penColor)
{
mCurPenColor = penColor;
}
public Color getCurPenColor()
{
return mCurPenColor;
}
public void setFillColor(Color fillColor)
{
mCurFillColor = fillColor;
}
public Color getCurFillColor()
{
return mCurFillColor;
}
public void setLineThickness(LineThickness thickness)
{
mCurLineThickness = thickness;
}
public LineThickness getCurLineThickness()
{
return mCurLineThickness;
}
public void processMousePressed(Point point)
{
mTempShape = ShapeFactory.createNewShape(this, point);
mUndoList.clear();
}
public void processMouseReleased(Point point)
{
if (mTempShape != null)
{
mTempShape.processMouseReleased(point);
if (mTempShape.checkCompleted() == true)
{
mShapeList.add(mTempShape);
mTempShape = null;
}
}
}
public void processMouseDragged(Point point)
{
if (mTempShape != null)
mTempShape.processMouseDragged(point);
}
public void drawShapes(Graphics2D g2)
{
for (int i = 0; i < mShapeList.size(); i++)
{
Shape curShape = mShapeList.get(i);
curShape.drawShape(g2);
}
if (mTempShape != null)
mTempShape.drawShape(g2);
}
public void undo()
{
if (mTempShape != null)
mTempShape = null;
else if (mShapeList.isEmpty() == false)
mUndoList.add(mShapeList.removeLast());
}
public void redo()
{
if (mTempShape != null)
mTempShape = null;
else if (mUndoList.isEmpty() == false)
mShapeList.add(mUndoList.removeLast());
}
private File chooseFileForSave()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("."));
int response = fileChooser.showSaveDialog(null);
if (response == JFileChooser.APPROVE_OPTION)
{
try
{
return fileChooser.getSelectedFile();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return null;
}
public boolean saveToFile()
{
boolean succeeded = false;
try
{
File saveFile = chooseFileForSave();
if (saveFile != null)
{
FileOutputStream fileStream = new FileOutputStream(saveFile);
ObjectOutputStream os = new ObjectOutputStream(fileStream);
os.writeObject(mShapeList);
os.close();
succeeded = true;
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Save failed.");
e.printStackTrace();
}
return succeeded;
}
private File chooseFileForOpen()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("."));
int response = fileChooser.showOpenDialog(null);
if (response == JFileChooser.APPROVE_OPTION)
{
try
{
return fileChooser.getSelectedFile();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return null;
}
public boolean loadFromFile()
{
boolean succeeded = false;
try {
File openFile = chooseFileForOpen();
if (openFile == null)
return succeeded;
FileInputStream fileStream = new FileInputStream(openFile);
ObjectInputStream os = new ObjectInputStream(fileStream);
Object object1 = os.readObject();
String className = object1.getClass().getName();
if (className.matches("java.util.LinkedList"))
{
@SuppressWarnings("unchecked")
LinkedList<Shape> shapeList = (LinkedList<Shape>) object1;
mMainWindow.repaint();
mUndoList.clear();
mShapeList = shapeList;
succeeded = true;
}
else
mShapeList = null;
os.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "File Open failed.");
e.printStackTrace();
}
return succeeded;
}
}