FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/core/src/processing/core/PGraphicsJava2D.java at master · h2oop/processing · GitHub
h2oop
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
core
/
src
/
processing
/
core
/
PGraphicsJava2D.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
2799 lines (2048 loc) · 76.9 KB
Breadcrumbs
processing
/
core
/
src
/
processing
/
core
/
PGraphicsJava2D.java
Copy path
File metadata and controls
2799 lines (2048 loc) · 76.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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-14 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
.
core
;
import
java
.
awt
.*;
import
java
.
awt
.
font
.
TextAttribute
;
import
java
.
awt
.
geom
.*;
import
java
.
awt
.
image
.*;
import
java
.
io
.
InputStream
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
Hashtable
;
import
java
.
util
.
Map
;
import
java
.
util
.
zip
.
GZIPInputStream
;
import
processing
.
data
.
XML
;
/**
* 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 = ((PGraphicsJava2D)g).g2;</PRE>
* This will let you do Java 2D stuff 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
;
/// 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
Color
strokeColorObject
;
public
boolean
strokeGradient
;
public
Paint
strokeGradientObject
;
//////////////////////////////////////////////////////////////
// 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
;
// width1 = width - 1;
// height1 = height - 1;
allocate
();
reapplySettings
();
}
// broken out because of subclassing for opengl
@
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
=
parent
.
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
{
// 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 (!useCanvas) {
// defaultComposite = g2.getComposite();
// }
// can't un-set this because this may be only a resize
// http://dev.processing.org/bugs/show_bug.cgi?id=463
//defaultsInited = false;
//checkSettings();
//reapplySettings = true;
}
//public void dispose()
//////////////////////////////////////////////////////////////
// FRAME
@
Override
public
boolean
canDraw
() {
return
true
;
}
@
Override
public
void
requestDraw
() {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
parent
.
handleDraw
();
// }
// });
}
// Graphics2D g2old;
@
Override
public
void
beginDraw
() {
// 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
) {
if
(
false
) {
GraphicsConfiguration
gc
=
parent
.
getGraphicsConfiguration
();
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
) {
GraphicsConfiguration
gc
=
parent
.
getGraphicsConfiguration
();
image
=
gc
.
createCompatibleImage
(
width
,
height
);
PApplet
.
debug
(
"created new image, type is "
+
image
);
g2
= (
Graphics2D
)
image
.
getGraphics
();
reapplySettings
=
true
;
}
}
//g2 = (Graphics2D) image.getGraphics();
// if (g2 != g2old) {
// System.out.println("new g2: " + g2);
// g2old = g2;
// }
}
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 (vimage == null || vimage.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) {
// vimage = gc.createCompatibleVolatileImage(width, height);
// }
// g2 = (Graphics2D) vimage.getGraphics();
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
;
}
@
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
(
primarySurface
) {
//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
();
}
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
);
}
}
//////////////////////////////////////////////////////////////
// SHAPES
//////////////////////////////////////////////////////////////
// SHAPE CREATION
// @Override
// public PShape createShape(PShape source) {
// return PShapeOpenGL.createShape2D(this, source);
// }
@
Override
public
PShape
createShape
() {
return
createShape
(
PShape
.
GEOMETRY
);
}
@
Override
public
PShape
createShape
(
int
type
) {
return
createShapeImpl
(
this
,
type
);
}
@
Override
public
PShape
createShape
(
int
kind
,
float
...
p
) {
return
createShapeImpl
(
this
,
kind
,
p
);
}
static
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
);
}
shape
.
is3D
(
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
);
}
shape
.
is3D
(
false
);
return
shape
;
}
@
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
;
}
//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
() {
breakShape
=
true
;
}
@
Override
public
void
endContour
() {
// does nothing, just need the break in beginContour()
}
@
Override
public
void
endShape
(
int
mode
) {
if
(
gpath
!=
null
) {
// make sure something has been drawn
if
(
shape
==
POLYGON
) {
if
(
mode
==
CLOSE
) {
gpath
.
closePath
();
}
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 )
*
* @webref Rendering
* @param mode the blending mode to use
*/
@
Override
protected
void
blendModeImpl
() {
if
(
blendMode
==
BLEND
) {
g2
.
setComposite
(
defaultComposite
);
}
else
{
g2
.
setComposite
(
new
Composite
() {
@
Override
public
CompositeContext
createContext
(
ColorModel
srcColorModel
,
ColorModel
dstColorModel
,
RenderingHints
hints
) {
return
new
BlendingContext
(
blendMode
);
}
});
}
}
// Blending implementation cribbed from portions of Romain Guy's
// demo and terrific writeup on blending modes in Java 2D.
// http://www.curious-creature.org/2006/09/20/new-blendings-modes-for-java2d/
private
static
final
class
BlendingContext
implements
CompositeContext
{
private
int
mode
;
private
BlendingContext
(
int
mode
) {
this
.
mode
=
mode
;
}
public
void
dispose
() { }
public
void
compose
(
Raster
src
,
Raster
dstIn
,
WritableRaster
dstOut
) {
// not sure if this is really necessary, since we control our buffers
if
(
src
.
getSampleModel
().
getDataType
() !=
DataBuffer
.
TYPE_INT
||
dstIn
.
getSampleModel
().
getDataType
() !=
DataBuffer
.
TYPE_INT
||
dstOut
.
getSampleModel
().
getDataType
() !=
DataBuffer
.
TYPE_INT
) {
throw
new
IllegalStateException
(
"Source and destination must store pixels as INT."
);
}
int
width
=
Math
.
min
(
src
.
getWidth
(),
dstIn
.
getWidth
());
int
height
=
Math
.
min
(
src
.
getHeight
(),
dstIn
.
getHeight
());
int
[]
srcPixels
=
new
int
[
width
];
int
[]
dstPixels
=
new
int
[
width
];
// Java won't set the high bits when RGB, returns 0 for alpha
int
alphaFiller
= (
dstIn
.
getNumBands
() ==
3
) ? (
0xFF
<<
24
) :
0x00
;
for
(
int
y
=
0
;
y
<
height
;
y
++) {
src
.
getDataElements
(
0
,
y
,
width
,
1
,
srcPixels
);
dstIn
.
getDataElements
(
0
,
y
,
width
,
1
,
dstPixels
);
for
(
int
x
=
0
;
x
<
width
;
x
++) {
dstPixels
[
x
] =
blendColor
(
srcPixels
[
x
],
alphaFiller
|
dstPixels
[
x
],
mode
);
}
dstOut
.
setDataElements
(
0
,
y
,
width
,
1
,
dstPixels
);
}
}
}
//////////////////////////////////////////////////////////////
// BEZIER VERTICES
@
Override
public
void
bezierVertex
(
float
x1
,
float
y1
,
float
x2
,
float
y2
,
float
x3
,
float
y3
) {
bezierVertexCheck
();
gpath
.
curveTo
(
x1
,
y1
,
x2
,
y2
,
x3
,
y3
);
}
@
Override
public
void
bezierVertex
(
float
x2
,
float
y2
,
float
z2
,
float
x3
,
float
y3
,
float
z3
,
float
x4
,
float
y4
,
float
z4
) {
showDepthWarningXYZ
(
"bezierVertex"
);
}
//////////////////////////////////////////////////////////////
// QUADRATIC BEZIER VERTICES
@
Override
public
void
quadraticVertex
(
float
ctrlX
,
float
ctrlY
,
float
endX
,
float
endY
) {
bezierVertexCheck
();
Point2D
cur
=
gpath
.
getCurrentPoint
();
float
x1
= (
float
)
cur
.
getX
();
float
y1
= (
float
)
cur
.
getY
();
bezierVertex
(
x1
+ ((
ctrlX
-
x1
)*
2
/
3.0f
),
y1
+ ((
ctrlY
-
y1
)*
2
/
3.0f
),
endX
+ ((
ctrlX
-
endX
)*
2
/
3.0f
),
endY
+ ((
ctrlY
-
endY
)*
2
/
3.0f
),
endX
,
endY
);
}
@
Override
public
void
quadraticVertex
(
float
x2
,
float
y2
,
float
z2
,
float
x4
,
float
y4
,
float
z4
) {
showDepthWarningXYZ
(
"quadVertex"
);
}
//////////////////////////////////////////////////////////////
// CURVE VERTICES
@
Override
protected
void
curveVertexCheck
() {
super
.
curveVertexCheck
();
if
(
curveCoordX
==
null
) {
curveCoordX
=
new
float
[
4
];
curveCoordY
=
new
float
[
4
];
curveDrawX
=
new
float
[
4
];
curveDrawY
=
new
float
[
4
];
}
}
@
Override
protected
void
curveVertexSegment
(
float
x1
,
float
y1
,
float
x2
,
float
y2
,
float
x3
,
float
y3
,
float
x4
,
float
y4
) {
curveCoordX
[
0
] =
x1
;
curveCoordY
[
0
] =
y1
;
curveCoordX
[
1
] =
x2
;
curveCoordY
[
1
] =
y2
;
curveCoordX
[
2
] =
x3
;
curveCoordY
[
2
] =
y3
;
curveCoordX
[
3
] =
x4
;
curveCoordY
[
3
] =
y4
;
curveToBezierMatrix
.
mult
(
curveCoordX
,
curveDrawX
);
curveToBezierMatrix
.
mult
(
curveCoordY
,
curveDrawY
);
// since the paths are continuous,
// only the first point needs the actual moveto
if
(
gpath
==
null
) {
gpath
=
new
GeneralPath
();
gpath
.
moveTo
(
curveDrawX
[
0
],
curveDrawY
[
0
]);
}
gpath
.
curveTo
(
curveDrawX
[
1
],
curveDrawY
[
1
],
curveDrawX
[
2
],
curveDrawY
[
2
],
curveDrawX
[
3
],
curveDrawY
[
3
]);
}
@
Override
public
void
curveVertex
(
float
x
,
float
y
,
float
z
) {
showDepthWarningXYZ
(
"curveVertex"
);
}
//////////////////////////////////////////////////////////////
// RENDERER
//public void flush()
//////////////////////////////////////////////////////////////
// POINT, LINE, TRIANGLE, QUAD
@
Override
public
void
point
(
float
x
,
float
y
) {
if
(
stroke
) {
// if (strokeWeight > 1) {
line
(
x
,
y
,
x
+
EPSILON
,
y
+
EPSILON
);
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL