-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPGraphicsJava2D.java
More file actions
3038 lines (2267 loc) · 85.2 KB
/
PGraphicsJava2D.java
File metadata and controls
3038 lines (2267 loc) · 85.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2013-15 The Processing Foundation
Copyright (c) 2005-13 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
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.*;
import java.awt.font.TextAttribute;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import processing.core.*;
/**
* Subclass for PGraphics that implements the graphics API using Java2D.
* <p>
* To get access to the Java 2D "Graphics2D" object for the default
* renderer, use:
* <PRE>
* Graphics2D g2 = (Graphics2D) g.getNative();
* </PRE>
* This will let you do Graphics2D calls directly, but is not supported
* in any way shape or form. Which just means "have fun, but don't complain
* if it breaks."
* <p>
* Advanced <a href="http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-Desktop/html/java2d.html">debugging notes</a> for Java2D.
*/
public class PGraphicsJava2D extends PGraphics {
//// BufferStrategy strategy;
//// BufferedImage bimage;
//// VolatileImage vimage;
// Canvas canvas;
//// boolean useCanvas = true;
// boolean useCanvas = false;
//// boolean useRetina = true;
//// boolean useOffscreen = true; // ~40fps
// boolean useOffscreen = false;
public Graphics2D g2;
// protected BufferedImage offscreen;
Composite defaultComposite;
GeneralPath gpath;
// path for contours so gpath can be closed
GeneralPath auxPath;
boolean openContour;
/// break the shape at the next vertex (next vertex() call is a moveto())
boolean breakShape;
/// coordinates for internal curve calculation
float[] curveCoordX;
float[] curveCoordY;
float[] curveDrawX;
float[] curveDrawY;
int transformCount;
AffineTransform[] transformStack =
new AffineTransform[MATRIX_STACK_DEPTH];
double[] transform = new double[6];
Line2D.Float line = new Line2D.Float();
Ellipse2D.Float ellipse = new Ellipse2D.Float();
Rectangle2D.Float rect = new Rectangle2D.Float();
Arc2D.Float arc = new Arc2D.Float();
protected Color tintColorObject;
protected Color fillColorObject;
public boolean fillGradient;
public Paint fillGradientObject;
protected Stroke strokeObject;
protected Color strokeColorObject;
public boolean strokeGradient;
public Paint strokeGradientObject;
Font fontObject;
//////////////////////////////////////////////////////////////
// INTERNAL
public PGraphicsJava2D() { }
//public void setParent(PApplet parent)
//public void setPrimary(boolean primary)
//public void setPath(String path)
// /**
// * Called in response to a resize event, handles setting the
// * new width and height internally, as well as re-allocating
// * the pixel buffer for the new size.
// *
// * Note that this will nuke any cameraMode() settings.
// */
// @Override
// public void setSize(int iwidth, int iheight) { // ignore
// width = iwidth;
// height = iheight;
//
// allocate();
// reapplySettings();
// }
// @Override
// protected void allocate() {
// //surface.initImage(this, width, height);
// surface.initImage(this);
// }
/*
@Override
protected void allocate() {
// Tried this with RGB instead of ARGB for the primarySurface version,
// but didn't see any performance difference (OS X 10.6, Java 6u24).
// For 0196, also attempted RGB instead of ARGB, but that causes
// strange things to happen with blending.
// image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
if (primarySurface) {
if (useCanvas) {
if (canvas != null) {
parent.removeListeners(canvas);
parent.remove(canvas);
}
canvas = new Canvas();
canvas.setIgnoreRepaint(true);
// parent.setLayout(new BorderLayout());
// parent.add(canvas, BorderLayout.CENTER);
parent.add(canvas);
// canvas.validate();
// parent.doLayout();
if (canvas.getWidth() != width || canvas.getHeight() != height) {
PApplet.debug("PGraphicsJava2D comp size being set to " + width + "x" + height);
canvas.setSize(width, height);
} else {
PApplet.debug("PGraphicsJava2D comp size already " + width + "x" + height);
}
parent.addListeners(canvas);
// canvas.createBufferStrategy(1);
// g2 = (Graphics2D) canvas.getGraphics();
} else {
parent.updateListeners(parent); // in case they're already there
// using a compatible image here doesn't seem to provide any performance boost
if (useOffscreen) {
// Needs to be RGB otherwise there's a major performance hit [0204]
// http://code.google.com/p/processing/issues/detail?id=729
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// GraphicsConfiguration gc = parent.getGraphicsConfiguration();
// image = gc.createCompatibleImage(width, height);
offscreen = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// offscreen = gc.createCompatibleImage(width, height);
g2 = (Graphics2D) offscreen.getGraphics();
} else {
// System.out.println("hopefully faster " + width + " " + height);
// new Exception().printStackTrace(System.out);
GraphicsConfiguration gc = canvas.getGraphicsConfiguration();
// If not realized (off-screen, i.e the Color Selector Tool),
// gc will be null.
if (gc == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
}
image = gc.createCompatibleImage(width, height);
g2 = (Graphics2D) image.getGraphics();
}
}
} else { // not the primary surface
// Since this buffer's offscreen anyway, no need for the extra offscreen
// buffer. However, unlike the primary surface, this feller needs to be
// ARGB so that blending ("alpha" compositing) will work properly.
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
g2 = (Graphics2D) image.getGraphics();
}
*/
/*
if (primarySurface) {
Canvas canvas = ((PSurfaceAWT) surface).canvas;
GraphicsConfiguration gc = canvas.getGraphicsConfiguration();
// If not realized (off-screen, i.e the Color Selector Tool),
// gc will be null.
if (gc == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
}
image = gc.createCompatibleImage(width, height);
g2 = (Graphics2D) image.getGraphics();
} else {
}
g2 = (Graphics2D) image.getGraphics();
}
*/
//public void dispose()
@Override
public PSurface createSurface() {
return surface = new PSurfaceAWT(this);
}
/**
* Still need a means to get the java.awt.Image object, since getNative()
* is going to return the {@link Graphics2D} object.
*/
@Override
public Image getImage() {
return image;
}
/** Returns the java.awt.Graphics2D object used by this renderer. */
@Override
public Object getNative() {
return g2;
}
//////////////////////////////////////////////////////////////
// FRAME
// @Override
// public boolean canDraw() {
// return true;
// }
// @Override
// public void requestDraw() {
//// EventQueue.invokeLater(new Runnable() {
//// public void run() {
// parent.handleDraw();
//// }
//// });
// }
// Graphics2D g2old;
public Graphics2D checkImage() {
if (image == null ||
((BufferedImage) image).getWidth() != width*pixelDensity ||
((BufferedImage) image).getHeight() != height*pixelDensity) {
// ((VolatileImage) image).getWidth() != width ||
// ((VolatileImage) image).getHeight() != height) {
// image = new BufferedImage(width * pixelFactor, height * pixelFactor
// format == RGB ? BufferedImage.TYPE_INT_ARGB);
// Commenting this out, because we are not drawing directly to the screen [jv 2018-06-01]
//
// GraphicsConfiguration gc = null;
// if (surface != null) {
// Component comp = null; //surface.getComponent();
// if (comp == null) {
//// System.out.println("component null, but parent.frame is " + parent.frame);
// comp = parent.frame;
// }
// if (comp != null) {
// gc = comp.getGraphicsConfiguration();
// }
// }
// // If not realized (off-screen, i.e the Color Selector Tool), gc will be null.
// if (gc == null) {
// //System.err.println("GraphicsConfiguration null in initImage()");
// GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
// gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
// }
// Formerly this was broken into separate versions based on offscreen or
// not, but we may as well create a compatible image; it won't hurt, right?
// P.S.: Three years later, I'm happy to report it did in fact hurt [jv 2018-06-01]
int wide = width * pixelDensity;
int high = height * pixelDensity;
// System.out.println("re-creating image");
// For now we expect non-premultiplied INT ARGB and the compatible image
// might not be it... create the image directly. It's important that the
// image has all four bands, otherwise we get garbage alpha during blending
// (see https://github.com/processing/processing/pull/2645,
// https://github.com/processing/processing/pull/3523)
//
// image = gc.createCompatibleImage(wide, high, Transparency.TRANSLUCENT);
image = new BufferedImage(wide, high, BufferedImage.TYPE_INT_ARGB);
}
return (Graphics2D) image.getGraphics();
}
@Override
public void beginDraw() {
g2 = checkImage();
// Calling getGraphics() seems to nuke several settings.
// It seems to be re-creating a new Graphics2D object each time.
// https://github.com/processing/processing/issues/3331
if (strokeObject != null) {
g2.setStroke(strokeObject);
}
// https://github.com/processing/processing/issues/2617
if (fontObject != null) {
g2.setFont(fontObject);
}
// https://github.com/processing/processing/issues/4019
if (blendMode != 0) {
blendMode(blendMode);
}
handleSmooth();
/*
// NOTE: Calling image.getGraphics() will create a new Graphics context,
// even if it's for the same image that's already had a context created.
// Seems like a speed/memory issue, and also requires that all smoothing,
// stroke, font and other props be reset. Can't find a good answer about
// whether getGraphics() and dispose() on each frame is 1) better practice
// and 2) minimal overhead, however. Instinct suggests #1 may be true,
// but #2 seems a problem.
if (primarySurface && !useOffscreen) {
GraphicsConfiguration gc = canvas.getGraphicsConfiguration();
if (false) {
if (image == null || ((VolatileImage) image).validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) {
image = gc.createCompatibleVolatileImage(width, height);
g2 = (Graphics2D) image.getGraphics();
reapplySettings = true;
}
} else {
if (image == null) {
image = gc.createCompatibleImage(width, height);
PApplet.debug("created new image, type is " + image);
g2 = (Graphics2D) image.getGraphics();
reapplySettings = true;
}
}
}
if (useCanvas && primarySurface) {
if (parent.frameCount == 0) {
canvas.createBufferStrategy(2);
strategy = canvas.getBufferStrategy();
PApplet.debug("PGraphicsJava2D.beginDraw() strategy is " + strategy);
BufferCapabilities caps = strategy.getCapabilities();
caps = strategy.getCapabilities();
PApplet.debug("PGraphicsJava2D.beginDraw() caps are " +
" flipping: " + caps.isPageFlipping() +
" front/back accel: " + caps.getFrontBufferCapabilities().isAccelerated() + " " +
"/" + caps.getBackBufferCapabilities().isAccelerated());
}
GraphicsConfiguration gc = canvas.getGraphicsConfiguration();
if (bimage == null ||
bimage.getWidth() != width ||
bimage.getHeight() != height) {
PApplet.debug("PGraphicsJava2D creating new image");
bimage = gc.createCompatibleImage(width, height);
// image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
g2 = bimage.createGraphics();
defaultComposite = g2.getComposite();
reapplySettings = true;
}
}
*/
checkSettings();
resetMatrix(); // reset model matrix
vertexCount = 0;
}
/**
* Smoothing for Java2D is 2 for bilinear, and 3 for bicubic (the default).
* Internally, smooth(1) is the default, smooth(0) is noSmooth().
*/
protected void handleSmooth() {
if (smooth == 0) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
} else {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if (smooth == 1 || smooth == 3) { // default is bicubic
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
} else if (smooth == 2) {
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
}
// http://docs.oracle.com/javase/tutorial/2d/text/renderinghints.html
// Oracle Java text anti-aliasing on OS X looks like s*t compared to the
// text rendering with Apple's old Java 6. Below, several attempts to fix:
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// Turns out this is the one that actually makes things work.
// Kerning is still screwed up, however.
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
// g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
// RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
// g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
// RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
// g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
// RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
}
}
@Override
public void endDraw() {
// hm, mark pixels as changed, because this will instantly do a full
// copy of all the pixels to the surface.. so that's kind of a mess.
//updatePixels();
if (primaryGraphics) {
/*
//if (canvas != null) {
if (useCanvas) {
//System.out.println(canvas);
// alternate version
//canvas.repaint(); // ?? what to do for swapping buffers
// System.out.println("endDraw() frameCount is " + parent.frameCount);
// if (parent.frameCount != 0) {
redraw();
// }
} else if (useOffscreen) {
// don't copy the pixels/data elements of the buffered image directly,
// since it'll disable the nice speedy pipeline stuff, sending all drawing
// into a world of suck that's rough 6 trillion times slower.
synchronized (image) {
//System.out.println("inside j2d sync");
image.getGraphics().drawImage(offscreen, 0, 0, null);
}
} else {
// changed to not dispose and get on each frame,
// otherwise a new Graphics context is used on each frame
// g2.dispose();
// System.out.println("not doing anything special in endDraw()");
}
*/
} else {
// TODO this is probably overkill for most tasks...
loadPixels();
}
// // Marking as modified, and then calling updatePixels() in
// // the super class, which just sets the mx1, my1, mx2, my2
// // coordinates of the modified area. This avoids doing the
// // full copy of the pixels to the surface in this.updatePixels().
// setModified();
// super.updatePixels();
// Marks pixels as modified so that the pixels will be updated.
// Also sets mx1/y1/x2/y2 so that OpenGL will pick it up.
setModified();
g2.dispose();
}
/*
private void redraw() {
// only need this check if the validate() call will use redraw()
// if (strategy == null) return;
do {
PApplet.debug("PGraphicsJava2D.redraw() top of outer do { } block");
do {
PApplet.debug("PGraphicsJava2D.redraw() top of inner do { } block");
PApplet.debug("strategy is " + strategy);
Graphics bsg = strategy.getDrawGraphics();
// if (vimage != null) {
// bsg.drawImage(vimage, 0, 0, null);
// } else {
bsg.drawImage(bimage, 0, 0, null);
// if (parent.frameCount == 0) {
// try {
// ImageIO.write(image, "jpg", new java.io.File("/Users/fry/Desktop/buff.jpg"));
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
bsg.dispose();
// the strategy version
// g2.dispose();
// if (!strategy.contentsLost()) {
// if (parent.frameCount != 0) {
// Toolkit.getDefaultToolkit().sync();
// }
// } else {
// System.out.println("XXXXX strategy contents lost");
// }
// }
// }
} while (strategy.contentsRestored());
PApplet.debug("PGraphicsJava2D.redraw() showing strategy");
strategy.show();
} while (strategy.contentsLost());
PApplet.debug("PGraphicsJava2D.redraw() out of do { } block");
}
*/
//////////////////////////////////////////////////////////////
// SETTINGS
//protected void checkSettings()
@Override
protected void defaultSettings() {
// if (!useCanvas) {
// // Papered over another threading issue...
// // See if this comes back now that the other issue is fixed.
//// while (g2 == null) {
//// try {
//// System.out.println("sleeping until g2 is available");
//// Thread.sleep(5);
//// } catch (InterruptedException e) { }
//// }
defaultComposite = g2.getComposite();
// }
super.defaultSettings();
}
//protected void reapplySettings()
//////////////////////////////////////////////////////////////
// HINT
@Override
public void hint(int which) {
// take care of setting the hint
super.hint(which);
// Avoid badness when drawing shorter strokes.
// http://code.google.com/p/processing/issues/detail?id=1068
// Unfortunately cannot always be enabled, because it makes the
// stroke in many standard Processing examples really gross.
if (which == ENABLE_STROKE_PURE) {
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
} else if (which == DISABLE_STROKE_PURE) {
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_DEFAULT);
}
}
//////////////////////////////////////////////////////////////
// SHAPE CREATION
@Override
protected PShape createShapeFamily(int type) {
return new PShape(this, type);
}
@Override
protected PShape createShapePrimitive(int kind, float... p) {
return new PShape(this, kind, p);
}
// @Override
// public PShape createShape(PShape source) {
// return PShapeOpenGL.createShape2D(this, source);
// }
/*
protected PShape createShapeImpl(PGraphicsJava2D pg, int type) {
PShape shape = null;
if (type == PConstants.GROUP) {
shape = new PShape(pg, PConstants.GROUP);
} else if (type == PShape.PATH) {
shape = new PShape(pg, PShape.PATH);
} else if (type == PShape.GEOMETRY) {
shape = new PShape(pg, PShape.GEOMETRY);
}
// defaults to false, don't assign it and make complexity for overrides
//shape.set3D(false);
return shape;
}
*/
/*
static protected PShape createShapeImpl(PGraphicsJava2D pg,
int kind, float... p) {
PShape shape = null;
int len = p.length;
if (kind == POINT) {
if (len != 2) {
showWarning("Wrong number of parameters");
return null;
}
shape = new PShape(pg, PShape.PRIMITIVE);
shape.setKind(POINT);
} else if (kind == LINE) {
if (len != 4) {
showWarning("Wrong number of parameters");
return null;
}
shape = new PShape(pg, PShape.PRIMITIVE);
shape.setKind(LINE);
} else if (kind == TRIANGLE) {
if (len != 6) {
showWarning("Wrong number of parameters");
return null;
}
shape = new PShape(pg, PShape.PRIMITIVE);
shape.setKind(TRIANGLE);
} else if (kind == QUAD) {
if (len != 8) {
showWarning("Wrong number of parameters");
return null;
}
shape = new PShape(pg, PShape.PRIMITIVE);
shape.setKind(QUAD);
} else if (kind == RECT) {
if (len != 4 && len != 5 && len != 8 && len != 9) {
showWarning("Wrong number of parameters");
return null;
}
shape = new PShape(pg, PShape.PRIMITIVE);
shape.setKind(RECT);
} else if (kind == ELLIPSE) {
if (len != 4 && len != 5) {
showWarning("Wrong number of parameters");
return null;
}
shape = new PShape(pg, PShape.PRIMITIVE);
shape.setKind(ELLIPSE);
} else if (kind == ARC) {
if (len != 6 && len != 7) {
showWarning("Wrong number of parameters");
return null;
}
shape = new PShape(pg, PShape.PRIMITIVE);
shape.setKind(ARC);
} else if (kind == BOX) {
showWarning("Primitive not supported in 2D");
} else if (kind == SPHERE) {
showWarning("Primitive not supported in 2D");
} else {
showWarning("Unrecognized primitive type");
}
if (shape != null) {
shape.setParams(p);
}
// defaults to false, don't assign it and make complexity for overrides
//shape.set3D(false);
return shape;
}
*/
//////////////////////////////////////////////////////////////
// SHAPES
@Override
public void beginShape(int kind) {
//super.beginShape(kind);
shape = kind;
vertexCount = 0;
curveVertexCount = 0;
// set gpath to null, because when mixing curves and straight
// lines, vertexCount will be set back to zero, so vertexCount == 1
// is no longer a good indicator of whether the shape is new.
// this way, just check to see if gpath is null, and if it isn't
// then just use it to continue the shape.
gpath = null;
auxPath = null;
}
//public boolean edge(boolean e)
//public void normal(float nx, float ny, float nz) {
//public void textureMode(int mode)
@Override
public void texture(PImage image) {
showMethodWarning("texture");
}
@Override
public void vertex(float x, float y) {
curveVertexCount = 0;
//float vertex[];
if (vertexCount == vertices.length) {
float[][] temp = new float[vertexCount<<1][VERTEX_FIELD_COUNT];
System.arraycopy(vertices, 0, temp, 0, vertexCount);
vertices = temp;
//message(CHATTER, "allocating more vertices " + vertices.length);
}
// not everyone needs this, but just easier to store rather
// than adding another moving part to the code...
vertices[vertexCount][X] = x;
vertices[vertexCount][Y] = y;
vertexCount++;
switch (shape) {
case POINTS:
point(x, y);
break;
case LINES:
if ((vertexCount % 2) == 0) {
line(vertices[vertexCount-2][X],
vertices[vertexCount-2][Y], x, y);
}
break;
case TRIANGLES:
if ((vertexCount % 3) == 0) {
triangle(vertices[vertexCount - 3][X],
vertices[vertexCount - 3][Y],
vertices[vertexCount - 2][X],
vertices[vertexCount - 2][Y],
x, y);
}
break;
case TRIANGLE_STRIP:
if (vertexCount >= 3) {
triangle(vertices[vertexCount - 2][X],
vertices[vertexCount - 2][Y],
vertices[vertexCount - 1][X],
vertices[vertexCount - 1][Y],
vertices[vertexCount - 3][X],
vertices[vertexCount - 3][Y]);
}
break;
case TRIANGLE_FAN:
if (vertexCount >= 3) {
// This is an unfortunate implementation because the stroke for an
// adjacent triangle will be repeated. However, if the stroke is not
// redrawn, it will replace the adjacent line (when it lines up
// perfectly) or show a faint line (when off by a small amount).
// The alternative would be to wait, then draw the shape as a
// polygon fill, followed by a series of vertices. But that's a
// poor method when used with PDF, DXF, or other recording objects,
// since discrete triangles would likely be preferred.
triangle(vertices[0][X],
vertices[0][Y],
vertices[vertexCount - 2][X],
vertices[vertexCount - 2][Y],
x, y);
}
break;
case QUAD:
case QUADS:
if ((vertexCount % 4) == 0) {
quad(vertices[vertexCount - 4][X],
vertices[vertexCount - 4][Y],
vertices[vertexCount - 3][X],
vertices[vertexCount - 3][Y],
vertices[vertexCount - 2][X],
vertices[vertexCount - 2][Y],
x, y);
}
break;
case QUAD_STRIP:
// 0---2---4
// | | |
// 1---3---5
if ((vertexCount >= 4) && ((vertexCount % 2) == 0)) {
quad(vertices[vertexCount - 4][X],
vertices[vertexCount - 4][Y],
vertices[vertexCount - 2][X],
vertices[vertexCount - 2][Y],
x, y,
vertices[vertexCount - 3][X],
vertices[vertexCount - 3][Y]);
}
break;
case POLYGON:
if (gpath == null) {
gpath = new GeneralPath();
gpath.moveTo(x, y);
} else if (breakShape) {
gpath.moveTo(x, y);
breakShape = false;
} else {
gpath.lineTo(x, y);
}
break;
}
}
@Override
public void vertex(float x, float y, float z) {
showDepthWarningXYZ("vertex");
}
@Override
public void vertex(float[] v) {
vertex(v[X], v[Y]);
}
@Override
public void vertex(float x, float y, float u, float v) {
showVariationWarning("vertex(x, y, u, v)");
}
@Override
public void vertex(float x, float y, float z, float u, float v) {
showDepthWarningXYZ("vertex");
}
@Override
public void beginContour() {
if (openContour) {
PGraphics.showWarning("Already called beginContour()");
return;
}
// draw contours to auxiliary path so main path can be closed later
GeneralPath contourPath = auxPath;
auxPath = gpath;
gpath = contourPath;
if (contourPath != null) { // first contour does not break
breakShape = true;
}
openContour = true;
}
@Override
public void endContour() {
if (!openContour) {
PGraphics.showWarning("Need to call beginContour() first");
return;
}
// close this contour
if (gpath != null) gpath.closePath();
// switch back to main path
GeneralPath contourPath = gpath;
gpath = auxPath;
auxPath = contourPath;
openContour = false;
}
@Override
public void endShape(int mode) {
if (openContour) { // correct automagically, notify user
endContour();
PGraphics.showWarning("Missing endContour() before endShape()");
}
if (gpath != null) { // make sure something has been drawn
if (shape == POLYGON) {
if (mode == CLOSE) {
gpath.closePath();
}
if (auxPath != null) {
gpath.append(auxPath, false);
}
drawShape(gpath);
}
}
shape = 0;
}
//////////////////////////////////////////////////////////////
// CLIPPING
@Override
protected void clipImpl(float x1, float y1, float x2, float y2) {
g2.setClip(new Rectangle2D.Float(x1, y1, x2 - x1, y2 - y1));
}
@Override
public void noClip() {
g2.setClip(null);
}
//////////////////////////////////////////////////////////////
// BLEND
/**
* ( begin auto-generated from blendMode.xml )
*
* This is a new reference entry for Processing 2.0. It will be updated shortly.
*
* ( end auto-generated )