-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathGraphicObject.java
More file actions
332 lines (266 loc) · 7.37 KB
/
GraphicObject.java
File metadata and controls
332 lines (266 loc) · 7.37 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright 2012 Samuel Taylor
*
* This file is part of darkFunction Editor
*
* darkFunction Editor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* darkFunction Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with darkFunction Editor. If not, see <http://www.gnu.org/licenses/>.
*/
package dfEditor;
import java.awt.*;
/**
*
* @author s4m20
*/
public abstract class GraphicObject extends java.util.Observable
{
public enum Anchor { TOP_LEFT, CENTRE }
protected Rectangle _rect;
protected String _id;
protected boolean _bResizable;
protected boolean _bSelected;
protected Rectangle _bounds;
protected Rectangle _savedRect;
protected String _description = null;
protected String _savedId;
protected float _angle;
protected float _scale;
protected float _opacity;
protected float _savedAngle;
protected float _savedScale;
protected float _savedOpacity;
protected Anchor _anchor;
public GraphicObject(Rectangle aRect)
{
_id = "";
_angle = 0;
_scale = 100;
_opacity = 255;
_bResizable = false;
_bSelected = false;
setRect(aRect);
saveRect();
saveAngle();
saveScale();
saveOpacity();
saveId();
_anchor = Anchor.TOP_LEFT;
_description = super.toString();
}
public void setAnchor(Anchor aAnchor)
{
_anchor = aAnchor;
}
public Anchor getAnchor()
{
return _anchor;
}
public void draw(Graphics g, Point aOffset, float aScale, float aAlpha, boolean bDrawSelectedMode)
{
Graphics2D g2d = (Graphics2D)g;
Composite backupComposite = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, aAlpha));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
this.draw(g, aOffset, aScale, bDrawSelectedMode);
g2d.setComposite(backupComposite);
}
public abstract void draw(Graphics g, Point aOffset, float aScale, boolean bDrawSelectedMode);
public abstract GraphicObject copy();
private void changed()
{
setChanged();
notifyObservers();
}
public boolean hasMoved()
{
return ! (_savedRect.equals(_rect));
}
public void setScale(float scale)
{
_scale = scale;
}
public float getScale()
{
return _scale;
}
public void setOpacity(float opacity)
{
_opacity = opacity;
}
public float getOpacity()
{
return _opacity;
}
public void setId(String id)
{
_id = id;
}
public String getId()
{
return _id;
}
public void setAngle(float aAngle)
{
_angle = aAngle;
}
public float getAngle()
{
return _angle;
}
public void setRect(Rectangle aRect)
{
_rect = new Rectangle(aRect.x, aRect.y, aRect.width, aRect.height);
constrainToBounds(_bounds);
changed();
}
public void setBounds(Rectangle aBounds)
{
_bounds = aBounds;
}
public void saveRect()
{
Rectangle r = getRect();
_savedRect = new Rectangle(r.x, r.y, r.width, r.height);
}
public void saveAngle()
{
_savedAngle = getAngle();
}
public void saveScale() {
_savedScale = getScale();
}
public void saveOpacity() {
_savedOpacity = getOpacity();
}
public void saveId() {
_savedId = getId();
}
public Rectangle getRect()
{
return _rect;
}
public Rectangle getSavedRect()
{
return _savedRect;
}
public float getSavedAngle()
{
return _savedAngle;
}
public float getSavedScale()
{
return _savedScale;
}
public float getSavedOpacity()
{
return _savedOpacity;
}
public String getSavedId()
{
return _savedId;
}
public void moveFromSavedPoint(Point aDist)
{
Rectangle r = getRect();
r.x = _savedRect.x + aDist.x;
r.y = _savedRect.y + aDist.y;
constrainToBounds(_bounds);
changed();
}
public void resizeFromSavedPoint(Point aVec, int aDir)
{
if ((aDir & Compass.NORTH) != 0)
{
int startY = _rect.y;
_rect.y = _savedRect.y + aVec.y;
if (_rect.y > _savedRect.y + _savedRect.height - 1)
_rect.y = _savedRect.y + _savedRect.height - 1;
else if (_rect.y < _bounds.y)
_rect.y = _bounds.y;
_rect.height += (startY - _rect.y);
}
else if ((aDir & Compass.SOUTH) != 0)
{
int y = _savedRect.height + aVec.y;
if (y > _bounds.height - _rect.y)
y = _bounds.height - _rect.y;
_rect.height = y;
}
if ((aDir & Compass.WEST) != 0)
{
int startX = _rect.x;
_rect.x = _savedRect.x + aVec.x;
if (_rect.x > _savedRect.x + _savedRect.width - 1)
_rect.x = _savedRect.x + _savedRect.width - 1;
else if (_rect.x < _bounds.x)
_rect.x = _bounds.x;
_rect.width += (startX - _rect.x);
}
else if ((aDir & Compass.EAST) != 0)
{
int x = _savedRect.width + aVec.x;
if (x > _bounds.width - _rect.x)
x = _bounds.width - _rect.x;
_rect.width = x;
}
constrainToBounds(_bounds);
changed();
}
public boolean isResizable()
{
return _bResizable;
}
public void setResizable(boolean aResizable)
{
_bResizable = aResizable;
}
public boolean isSelected()
{
return _bSelected;
}
public void setSelected(boolean aSelected)
{
_bSelected = aSelected;
changed();
}
public void constrainToBounds(Rectangle aBounds)
{
if (aBounds == null)
return;
Rectangle r = this.getRect();
if (r.width < 1)
r.width = 1;
else if (r.width > aBounds.width)
r.width = aBounds.width;
if (r.height < 1)
r.height = 1;
else if (r.height > aBounds.height)
r.height = aBounds.height;
if (r.x < 0)
r.x = 0;
else if (r.x + r.width > aBounds.width)
r.x = aBounds.width - r.width;
if (r.y < 0)
r.y = 0;
else if (r.y + r.height > aBounds.height)
r.y = aBounds.height - r.height;
}
public void setDescription(final String aDesc)
{
_description = aDesc;
}
@Override
public String toString()
{
return _description;
}
}