-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPShapeJava2D.java
More file actions
377 lines (311 loc) · 12.2 KB
/
PShapeJava2D.java
File metadata and controls
377 lines (311 loc) · 12.2 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2015 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.awt;
import java.awt.Paint;
import java.awt.PaintContext;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PShapeSVG;
import processing.data.*;
/**
* Implements features for PShape that are specific to AWT and Java2D.
* At the moment, this is gradients and java.awt.Paint handling.
*/
public class PShapeJava2D extends PShapeSVG {
Paint strokeGradientPaint;
Paint fillGradientPaint;
public PShapeJava2D(XML svg) {
super(svg);
}
public PShapeJava2D(PShapeSVG parent, XML properties, boolean parseKids) {
super(parent, properties, parseKids);
}
@Override
protected void setParent(PShapeSVG parent) {
super.setParent(parent);
if (parent instanceof PShapeJava2D) {
PShapeJava2D pj = (PShapeJava2D) parent;
fillGradientPaint = pj.fillGradientPaint;
strokeGradientPaint = pj.strokeGradientPaint;
} else { // parent is null or not Java2D
fillGradientPaint = null;
strokeGradientPaint = null;
}
}
/** Factory method for subclasses. */
@Override
protected PShapeSVG createShape(PShapeSVG parent, XML properties, boolean parseKids) {
return new PShapeJava2D(parent, properties, parseKids);
}
/*
@Override
public void setColor(String colorText, boolean isFill) {
super.setColor(colorText, isFill);
if (fillGradient != null) {
fillGradientPaint = calcGradientPaint(fillGradient);
}
if (strokeGradient != null) {
strokeGradientPaint = calcGradientPaint(strokeGradient);
}
}
*/
static class LinearGradientPaint implements Paint {
float x1, y1, x2, y2;
float[] offset;
int[] color;
int count;
float opacity;
public LinearGradientPaint(float x1, float y1, float x2, float y2,
float[] offset, int[] color, int count,
float opacity) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.offset = offset;
this.color = color;
this.count = count;
this.opacity = opacity;
}
public PaintContext createContext(ColorModel cm,
Rectangle deviceBounds, Rectangle2D userBounds,
AffineTransform xform, RenderingHints hints) {
Point2D t1 = xform.transform(new Point2D.Float(x1, y1), null);
Point2D t2 = xform.transform(new Point2D.Float(x2, y2), null);
return new LinearGradientContext((float) t1.getX(), (float) t1.getY(),
(float) t2.getX(), (float) t2.getY());
}
public int getTransparency() {
return TRANSLUCENT; // why not.. rather than checking each color
}
public class LinearGradientContext implements PaintContext {
int ACCURACY = 2;
float tx1, ty1, tx2, ty2;
public LinearGradientContext(float tx1, float ty1, float tx2, float ty2) {
this.tx1 = tx1;
this.ty1 = ty1;
this.tx2 = tx2;
this.ty2 = ty2;
}
public void dispose() { }
public ColorModel getColorModel() { return ColorModel.getRGBdefault(); }
public Raster getRaster(int x, int y, int w, int h) {
WritableRaster raster =
getColorModel().createCompatibleWritableRaster(w, h);
int[] data = new int[w * h * 4];
// make normalized version of base vector
float nx = tx2 - tx1;
float ny = ty2 - ty1;
float len = (float) Math.sqrt(nx*nx + ny*ny);
if (len != 0) {
nx /= len;
ny /= len;
}
int span = (int) PApplet.dist(tx1, ty1, tx2, ty2) * ACCURACY;
if (span <= 0) {
//System.err.println("span is too small");
// annoying edge case where the gradient isn't legit
int index = 0;
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
data[index++] = 0;
data[index++] = 0;
data[index++] = 0;
data[index++] = 255;
}
}
} else {
int[][] interp = new int[span][4];
int prev = 0;
for (int i = 1; i < count; i++) {
int c0 = color[i-1];
int c1 = color[i];
int last = (int) (offset[i] * (span-1));
//System.out.println("last is " + last);
for (int j = prev; j <= last; j++) {
float btwn = PApplet.norm(j, prev, last);
interp[j][0] = (int) PApplet.lerp((c0 >> 16) & 0xff, (c1 >> 16) & 0xff, btwn);
interp[j][1] = (int) PApplet.lerp((c0 >> 8) & 0xff, (c1 >> 8) & 0xff, btwn);
interp[j][2] = (int) PApplet.lerp(c0 & 0xff, c1 & 0xff, btwn);
interp[j][3] = (int) (PApplet.lerp((c0 >> 24) & 0xff, (c1 >> 24) & 0xff, btwn) * opacity);
//System.out.println(j + " " + interp[j][0] + " " + interp[j][1] + " " + interp[j][2]);
}
prev = last;
}
int index = 0;
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
//float distance = 0; //PApplet.dist(cx, cy, x + i, y + j);
//int which = PApplet.min((int) (distance * ACCURACY), interp.length-1);
float px = (x + i) - tx1;
float py = (y + j) - ty1;
// distance up the line is the dot product of the normalized
// vector of the gradient start/stop by the point being tested
int which = (int) ((px*nx + py*ny) * ACCURACY);
if (which < 0) which = 0;
if (which > interp.length-1) which = interp.length-1;
//if (which > 138) System.out.println("grabbing " + which);
data[index++] = interp[which][0];
data[index++] = interp[which][1];
data[index++] = interp[which][2];
data[index++] = interp[which][3];
}
}
}
raster.setPixels(0, 0, w, h, data);
return raster;
}
}
}
static class RadialGradientPaint implements Paint {
float cx, cy, radius;
float[] offset;
int[] color;
int count;
float opacity;
public RadialGradientPaint(float cx, float cy, float radius,
float[] offset, int[] color, int count,
float opacity) {
this.cx = cx;
this.cy = cy;
this.radius = radius;
this.offset = offset;
this.color = color;
this.count = count;
this.opacity = opacity;
}
public PaintContext createContext(ColorModel cm,
Rectangle deviceBounds, Rectangle2D userBounds,
AffineTransform xform, RenderingHints hints) {
return new RadialGradientContext();
}
public int getTransparency() {
return TRANSLUCENT;
}
public class RadialGradientContext implements PaintContext {
int ACCURACY = 5;
public void dispose() {}
public ColorModel getColorModel() { return ColorModel.getRGBdefault(); }
public Raster getRaster(int x, int y, int w, int h) {
WritableRaster raster =
getColorModel().createCompatibleWritableRaster(w, h);
int span = (int) radius * ACCURACY;
int[][] interp = new int[span][4];
int prev = 0;
for (int i = 1; i < count; i++) {
int c0 = color[i-1];
int c1 = color[i];
int last = (int) (offset[i] * (span - 1));
for (int j = prev; j <= last; j++) {
float btwn = PApplet.norm(j, prev, last);
interp[j][0] = (int) PApplet.lerp((c0 >> 16) & 0xff, (c1 >> 16) & 0xff, btwn);
interp[j][1] = (int) PApplet.lerp((c0 >> 8) & 0xff, (c1 >> 8) & 0xff, btwn);
interp[j][2] = (int) PApplet.lerp(c0 & 0xff, c1 & 0xff, btwn);
interp[j][3] = (int) (PApplet.lerp((c0 >> 24) & 0xff, (c1 >> 24) & 0xff, btwn) * opacity);
}
prev = last;
}
int[] data = new int[w * h * 4];
int index = 0;
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
float distance = PApplet.dist(cx, cy, x + i, y + j);
int which = PApplet.min((int) (distance * ACCURACY), interp.length-1);
data[index++] = interp[which][0];
data[index++] = interp[which][1];
data[index++] = interp[which][2];
data[index++] = interp[which][3];
}
}
raster.setPixels(0, 0, w, h, data);
return raster;
}
}
}
protected Paint calcGradientPaint(Gradient gradient) {
if (gradient instanceof LinearGradient) {
// System.out.println("creating linear gradient");
LinearGradient grad = (LinearGradient) gradient;
return new LinearGradientPaint(grad.x1, grad.y1, grad.x2, grad.y2,
grad.offset, grad.color, grad.count,
opacity);
} else if (gradient instanceof RadialGradient) {
// System.out.println("creating radial gradient");
RadialGradient grad = (RadialGradient) gradient;
return new RadialGradientPaint(grad.cx, grad.cy, grad.r,
grad.offset, grad.color, grad.count,
opacity);
}
return null;
}
// protected Paint calcGradientPaint(Gradient gradient,
// float x1, float y1, float x2, float y2) {
// if (gradient instanceof LinearGradient) {
// LinearGradient grad = (LinearGradient) gradient;
// return new LinearGradientPaint(x1, y1, x2, y2,
// grad.offset, grad.color, grad.count,
// opacity);
// }
// throw new RuntimeException("Not a linear gradient.");
// }
// protected Paint calcGradientPaint(Gradient gradient,
// float cx, float cy, float r) {
// if (gradient instanceof RadialGradient) {
// RadialGradient grad = (RadialGradient) gradient;
// return new RadialGradientPaint(cx, cy, r,
// grad.offset, grad.color, grad.count,
// opacity);
// }
// throw new RuntimeException("Not a radial gradient.");
// }
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@Override
protected void styles(PGraphics g) {
super.styles(g);
//if (g instanceof PGraphicsJava2D) {
PGraphicsJava2D p2d = (PGraphicsJava2D) g;
if (strokeGradient != null) {
p2d.strokeGradient = true;
if (strokeGradientPaint == null) {
strokeGradientPaint = calcGradientPaint(strokeGradient);
}
p2d.strokeGradientObject = strokeGradientPaint;
} else {
// need to shut off, in case parent object has a gradient applied
//p2d.strokeGradient = false;
}
if (fillGradient != null) {
p2d.fillGradient = true;
if (fillGradientPaint == null) {
fillGradientPaint = calcGradientPaint(fillGradient);
}
p2d.fillGradientObject = fillGradientPaint;
} else {
// need to shut off, in case parent object has a gradient applied
//p2d.fillGradient = false;
}
//}
}
}