-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathCanvasContext.java
More file actions
283 lines (235 loc) · 7.85 KB
/
CanvasContext.java
File metadata and controls
283 lines (235 loc) · 7.85 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
/*
* This file is part of NodeBox.
*
* Copyright (C) 2008 Frederik De Bleser (frederik@pandora.be)
*
* NodeBox 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.
*
* NodeBox 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 NodeBox. If not, see <http://www.gnu.org/licenses/>.
*/
package nodebox.graphics;
import java.awt.image.BufferedImage;
public class CanvasContext extends AbstractGraphicsContext {
public static final float DEFAULT_WIDTH = 1000;
public static final float DEFAULT_HEIGHT = 1000;
public enum ImageMode {
CORNERS, CORNER, RADIUS, CENTER
}
protected ImageMode imageMode = ImageMode.CORNER;
private Canvas canvas;
//// Initialization ////
public CanvasContext() {
canvas = new Canvas(DEFAULT_WIDTH, DEFAULT_HEIGHT);
resetContext(true);
}
public CanvasContext(Canvas canvas) {
this.canvas = canvas;
resetContext(false);
}
@Override
public void resetContext() {
resetContext(true);
}
public void resetContext(boolean resetBackground) {
super.resetContext();
if (resetBackground)
canvas.setBackground(new Color(1, 1, 1));
}
public ImageMode imagemode() {
return imageMode;
}
public ImageMode imagemode(ImageMode m) {
return imageMode = m;
}
//// Setup methods ////
public void size(float width, float height) {
canvas.setWidth(width);
canvas.setHeight(height);
}
public float getWidth() {
return canvas.getWidth();
}
public float getHeight() {
return canvas.getHeight();
}
public float getWIDTH() {
return canvas.getWidth();
}
public float getHEIGHT() {
return canvas.getHeight();
}
/**
* Get the current background color.
*
* @return the current background color.
*/
public Color background() {
return canvas.getBackground();
}
/**
* Set the current background color to given grayscale value.
*
* @param x the gray component.
* @return the current background color.
*/
public Color background(float x) {
float nx = normalize(x);
return canvas.setBackground(new Color(nx, nx, nx));
}
/**
* Set the current background color to given grayscale and alpha value.
*
* @param x the grayscale value.
* @param y the alpha value.
* @return the current background color.
*/
public Color background(float x, float y) {
float nx = normalize(x);
return canvas.setBackground(new Color(nx, nx, nx, normalize(y)));
}
/**
* Set the current background color to the given R/G/B value.
*
* @param x the red component.
* @param y the green component.
* @param z the blue component.
* @return the current background color.
*/
public Color background(float x, float y, float z) {
return canvas.setBackground(new Color(normalize(x), normalize(y), normalize(z), colormode()));
}
/**
* Set the current background color to the given R/G/B/A value.
*
* @param x the red component.
* @param y the green component.
* @param z the blue component.
* @param a the alpha component.
* @return the current background color.
*/
public Color background(float x, float y, float z, float a) {
return canvas.setBackground(new Color(normalize(x), normalize(y), normalize(z), normalize(a), colormode()));
}
/**
* Set the current background color to the given color.
* <p/>
* The color object is cloned; you can change the original afterwards.
* If the color object is null, the current background color is turned off (same as nobackground).
*
* @param c the color object.
* @return the current background color.
*/
public Color background(Color c) {
return canvas.setBackground(c == null ? null : c.clone());
}
public void nobackground() {
canvas.setBackground(null);
}
//// Attribute access ////
public Canvas getCanvas() {
return canvas;
}
//// Image methods ////
@Override
public Image image(String path, float x, float y) {
return image(path, x, y, null, null, 1.0f, true);
}
@Override
public Image image(String path, float x, float y, Float width) {
return image(path, x, y, width, null, 1.0f, true);
}
@Override
public Image image(String path, float x, float y, Float width, Float height) {
return image(path, x, y, width, height, 1.0f, true);
}
@Override
public Image image(String path, float x, float y, Float width, Float height, float alpha) {
return image(path, x, y, width, height, alpha, true);
}
@Override
public Image image(String path, float x, float y, Float width, Float height, boolean draw) {
return image(path, x, y, width, height, 1.0f, draw);
}
@Override
public Image image(String path, float x, float y, Float width, Float height, float alpha, boolean draw) {
return loadImage(new Image(path), x, y, width, height, alpha, draw);
}
@Override
public Image image(Image img, float x, float y, Float width, Float height, float alpha, boolean draw) {
return loadImage(img.clone(), x, y, width, height, alpha, draw);
}
@Override
public Image image(BufferedImage img, float x, float y, Float width, Float height, float alpha, boolean draw) {
return loadImage(new Image(img), x, y, width, height, alpha, draw);
}
private Image loadImage(Image img, float x, float y, Float width, Float height, float alpha, boolean draw) {
if (width != null) img.setWidth(width);
if (height != null) img.setHeight(height);
switch (imageMode) {
case CORNER:
float w = img.getWidth();
float h = img.getHeight();
img.setX(x + w / 2);
img.setY(y + h / 2);
break;
case CENTER:
img.setX(x);
img.setY(y);
}
// todo: differentiate between newly constructed objects and copies.
img.setTransformDelegate(new ContextTransformDelegate(this));
inheritFromContext(img);
if (alpha != 1.0)
img.setAlpha(alpha);
if (draw)
canvas.add(img);
return img;
}
@Override
public Size imagesize(String path) {
Image img = new Image(path);
return img.getSize();
}
@Override
public Size imagesize(Image img) {
return img.getSize();
}
@Override
public Size imagesize(BufferedImage img) {
return new Size(img.getWidth(), img.getHeight());
}
/// Drawing methods ////
/**
* The draw method doesn't actually draw anything, but rather appends grobs to the canvas.
* When the canvas gets drawn, this grob will be drawn also.
*
* @param grob the grob to append to the canvas
*/
public void draw(Grob grob) {
canvas.add(grob);
}
@Override
protected void addPath(Path p) {
canvas.add(p);
}
@Override
protected void addText(Text t) {
canvas.add(t);
}
protected void addImage(Image i) {
canvas.add(i);
}
//// Context inheritance ////
protected void inheritFromContext(Image i) {
TransformDelegate d = i.getTransformDelegate();
d.transform(i, transform, true); }
}