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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
|
/**
* @(#) GLCanvas.java
*/
package gl4java.awt;
import gl4java.*;
import gl4java.drawable.*;
import gl4java.drawable.utils.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
/**
* This is meant as an base class writing
* easy render functions. A clean usage of multi-threading compatible
* with JAVA2 is implemented in GLAnimCanvas !
*
* <p>
*
* If you are interessting in further Documentation and/or
* the history of GL4Java follow the following link.
*
* <pre>
<a href="../../GL4Java.html">The GL4Java Documentation</a>
* </pre>
* <p>
*
* There are two ways of using a GLCanvas: the {@link
* gl4java.drawable.GLEventListener} model or the subclassing model. Earlier
* versions of OpenGL for Java only supported the subclassing model.
* The default implementations of {@link #init}, {@link #display},
* {@link #reshape} and {@link #doCleanup}
* now send events to GLEventListeners; they can
* still be overridden as before to support the subclassing model.
*
* <p>
* If using the subclassing model, you should override the following
* methods for your needs:
* <pre>
<a href="GLCanvas.html#init()">preInit - initialisation before creating GLContext</a>
<a href="GLCanvas.html#init()">init - 1st initialisation after creating GLContext</a>
<a href="GLCanvas.html#doCleanup()">doCleanup - OGL cleanup prior to context deletion</a>
<a href="GLCanvas.html#display()">display - render your frame</a>
<a href="GLCanvas.html#reshape(int, int)">reshape - to reshape (window resize), gljResize() is allready invoked !</a>
* </pre>
*
* To check if you can use the GLContext and GL and GLU methods,
* use the function
* <pre>
<a href="GLCanvas.html#cvsIsInit()">cvsIsInit</a>
* </pre>
* <p>
* IF you remove/release a GLCanvas,
* e.g. you want to close/dispose it�s Window (which contains this GLCanvas),
* you HAVE TO call:
*
* <pre>
<a href="GLCanvas.html#cvsDispose()">cvsDispose</a>
* </pre>
* You should call this before releasing/dispose this Window !
* Also you can overwrite this class,
* to dispose your own elements, e.g. a Frame etc. -
* but be shure that you call
* cvsDispose implementation call this one !
*
* <p>
* We do override the following Canvas methods.
*
* <pre>
<a href="GLCanvas.html#update(java.awt.Graphics)">update</a>
<a href="GLCanvas.html#paint(java.awt.Graphics)">paint</a>
* </pre>
* <p>
*
* @see gl4java.awt.GLAnimCanvas
* @version 2.0, 21. April 1999
* @author Sven Goethel
* */
public class GLCanvas extends Canvas
implements GLEnum, GLUEnum,
ComponentListener, WindowListener, MouseListener,
GLDrawable
{
protected GLContext glj = null;
public GLFunc gl = null;
public GLUFunc glu = null;
protected Dimension size = null;
protected boolean mustResize = false;
protected boolean needCvsDispose = false;
/**
* Visual pre-set for doubleBuffer, default: true
* This value is updated after a GLContext is created with the
* original updated value of GLContext !
*
* @see gl4java.awt.GLCanvas#preInit
* @see gl4java.awt.GLCanvas#paint
*/
protected boolean doubleBuffer = true;
/**
* Visual pre-set for stencil-bit number, default: 0
* This value is updated after a GLContext is created with the
* original updated value of GLContext !
*
* @see gl4java.awt.GLCanvas#preInit
* @see gl4java.awt.GLCanvas#paint
*/
protected int stencilBits = 0;
/**
* Visual pre-set for accumulator buffer size, default: 0
* This value is updated after a GLContext is created with the
* original updated value of GLContext !
*
* This value has a special behavior.
* For input - within the contructor,
* it is the value for each component !
*
* The output value, after the constructor returns,
* it is the summary of all accumulation bits of all components !
*
* @see gl4java.awt.GLCanvas#preInit
* @see gl4java.awt.GLCanvas#paint
*/
protected int accumSize = 0;
/**
* Visual pre-set for stereoView, default: false
* This value is updated after a GLContext is created with the
* original updated value of GLContext !
*
* @see gl4java.awt.GLCanvas#preInit
* @see gl4java.awt.GLCanvas#paint
*/
protected boolean stereoView = false;
/**
* Visual pre-set for RGBA usage, default: true - of course ;-)
* This value is updated after a GLContext is created with the
* original updated value of GLContext !
*
* @see gl4java.awt.GLCanvas#preInit
* @see gl4java.awt.GLCanvas#paint
*/
protected boolean rgba = true;
protected GLCapabilities capabilities = null;
/**
* Visual pre-set for RGBA usage, default: true - of course ;-)
* This value is updated after a GLContext is created with the
* original updated value of GLContext !
*
* @see gl4java.awt.GLCanvas#preInit
* @see gl4java.awt.GLCanvas#paint
*/
protected boolean createOwnWindow = false;
/**
* The context with wich display lists and textures will be shared.
*
* @see gl4java.awt.GLCanvas#preInit
* @see gl4java.awt.GLCanvas#paint
*/
protected GLContext sharedGLContext;
// The list of GLEventListeners
private GLEventListenerList listeners = new GLEventListenerList();
// Indicates whether init() is _almost_ called ...
private volatile boolean initCalledAlmost = false;
// Indicates whether init() has been called yet.
private volatile boolean initCalled = false;
// Indicates whether the canvas will permit any calls to init() or
// display() from within the paint() method.
private boolean enableAWTThreadRendering;
void recomputeAWTThreadRendering() {
// Switches to false under the following circumstances:
// - this is (precisely a) GLAnimCanvas; subclassing may change
// the code flow significantly enough that this optimization
// breaks
// - useRepaint is false
enableAWTThreadRendering = !((getClass() == GLAnimCanvas.class) && !getUseRepaint());
}
static {
if(GLContext.doLoadNativeLibraries(null, null, null)==false)
System.out.println("GLCanvas could not load def. native libs.");
}
/**
*
* Constructor
*
* @param width the canvas initial-prefered width
* @param height the canvas initial-prefered height
*
* @param gl_Name The name of the GLFunc implementation
* If gl_Name==null, the default class will be used !
*
* @param glu_Name The name of the GLUFunc implementation
* If gl_LibName==null, the default class will be used !
*
*/
public GLCanvas( GLCapabilities capabilities,
int width, int height,
String gl_Name,
String glu_Name
)
{
super( );
this.capabilities=capabilities;
if( (gl=GLContext.createGLFunc(gl_Name)) ==null)
{
System.out.println("GLFunc implementation "+gl_Name+" not created");
}
if( (glu=GLContext.createGLUFunc(glu_Name)) ==null)
{
System.out.println("GLUFunc implementation "+glu_Name+" not created");
}
size = new Dimension(width, height);
setSize(size);
recomputeAWTThreadRendering();
}
/**
*
* Constructor
*
* @param width the canvas initial-prefered width
* @param height the canvas initial-prefered height
*
* @param gl_Name The name of the GLFunc implementation
* If gl_Name==null, the default class will be used !
*
* @param glu_Name The name of the GLUFunc implementation
* If gl_LibName==null, the default class will be used !
*
*/
public GLCanvas( GLCapabilities capabilities,
int width, int height
)
{
this(capabilities, width, height, null, null);
}
/**
*
* Constructor
*
* @param width the canvas initial-prefered width
* @param height the canvas initial-prefered height
*
* @param gl_Name The name of the GLFunc implementation
* If gl_Name==null, the default class will be used !
*
* @param glu_Name The name of the GLUFunc implementation
* If gl_LibName==null, the default class will be used !
*
*/
public GLCanvas( int width, int height,
String gl_Name,
String glu_Name
)
{
this(null, width, height, gl_Name, glu_Name);
}
/**
*
* Constructor
*
* Uses the default GLFunc and GLUFunc implementation !
*
* @param width the canvas initial-prefered width
* @param height the canvas initial-prefered height
*
*/
public GLCanvas( int width, int height )
{
this(width, height, null, null);
}
/**
*
* Constructor (JDK 1.2 or later)
*
* @param config the GraphicsConfiguration for this canvas (>= JDK 1.2)
*
* @param width the canvas initial-prefered width
* @param height the canvas initial-prefered height
*
* @param gl_Name The name of the GLFunc implementation
* If gl_Name==null, the default class will be used !
*
* @param glu_Name The name of the GLUFunc implementation
* If gl_LibName==null, the default class will be used !
*
*/
public GLCanvas( GraphicsConfiguration config,
GLCapabilities capabilities,
int width, int height,
String gl_Name,
String glu_Name
)
{
super( config );
this.capabilities=capabilities;
if( (gl=GLContext.createGLFunc(gl_Name)) ==null)
{
System.out.println("GLFunc implementation "+gl_Name+" not created");
}
if( (glu=GLContext.createGLUFunc(glu_Name)) ==null)
{
System.out.println("GLUFunc implementation "+glu_Name+" not created");
}
size = new Dimension(width, height);
setSize(size);
recomputeAWTThreadRendering();
}
/**
*
* Constructor (JDK 1.2 or later)
*
* Uses the default GLFunc and GLUFunc implementation !
*
* @param config the GraphicsConfiguration for this canvas (>= JDK 1.2)
*
* @param width the canvas initial-prefered width
* @param height the canvas initial-prefered height
*
*/
public GLCanvas( GraphicsConfiguration config,
GLCapabilities capabilities,
int width, int height )
{
this(config, capabilities, width, height, null, null);
}
/* GLCanvas AWT classes */
public Dimension getPreferredSize() {
return getMinimumSize();
}
public Dimension getMinimumSize() {
return size;
}
/**
* Used to return the created GLContext
*/
public final GLContext getGLContext() { return glj; }
/**
*
* Overridden update
* This one only call's the paint method, without clearing
* the background - thats hopefully done by OpenGL ;-)
*
* @param g the Graphics Context
* @return void
*
* @see gl4java.awt.GLCanvas#paint
*/
public void update(Graphics g)
{
/* let's let OpenGL clear the background ... */
paint(g);
}
/**
* Safe the toplevel window
*/
protected Window topLevelWindow = null;
/**
*
* This function returns the found TopLevelWindow,
* which contains this Canvas ..
*
* @return void
*
* @see gl4java.awt.GLCanvas#paint
*/
public final Window getTopLevelWindow()
{ return topLevelWindow; }
/**
* this function overrides the Canvas paint method !
*
* For the first paint,
* the user function preInit is called, a GLContext is created
* and the user function init is called !
*
* Also, if a GL Context exist, GLCanvas's sDisplay-method will be called
* to do OpenGL-rendering.
*
* The sDisplay method itself calls the display-method !
* sDisplay is needed to be thread-safe, to manage
* the resize functionality and to safe the time per frame.
*
* To define your rendering, you should overwrite the display-method
* in your derivation.
*
* @see gl4java.GLContext#GLContext
* @see gl4java.awt.GLCanvas#cvsIsInit
* @see gl4java.awt.GLCanvas#sDisplay
* @see gl4java.awt.GLCanvas#display
* @see gl4java.awt.GLCanvas#preInit
* @see gl4java.awt.GLCanvas#init
*/
public final void paint( Graphics g )
{
if(glj == null || ( !glj.gljIsInit() && isGLEnabled() ) )
{
if(GLContext.gljClassDebug)
System.out.println("GLCanvas create GLContext (recreate="+
(glj != null) +")");
preInit();
if(glj!=null) glj=null;
if (capabilities != null )
{
glj = new GLContext ( this, gl, glu,
capabilities,
sharedGLContext );
} else {
glj = new GLContext ( this, gl, glu,
createOwnWindow,
doubleBuffer, stereoView,
rgba, stencilBits, accumSize,
sharedGLContext );
}
if(glj==null) return;
createOwnWindow = glj.isOwnWindowCreated();
doubleBuffer = glj.isDoubleBuffer();
stencilBits = glj.getStencilBitNumber();
accumSize = glj.getAccumSize();
stereoView = glj.isStereoView();
rgba = glj.isRGBA();
Color col = getBackground();
gl.glClearColor((float)col.getRed()/255.0f,
(float)col.getGreen()/255.0f,
(float)col.getBlue()/255.0f, 0.0f);
if (enableAWTThreadRendering) {
if(GLContext.gljClassDebug)
System.out.println("GLCanvas init() will be called now (by AWTThreadRendering) !");
init();
initCalled = true;
/* force a reshape, to be sure .. */
mustResize = true;
}
initCalledAlmost = true;
// fetch the top-level window ,
// to add us as the windowListener
//
Container _c = getParent();
Container c = null;
while(_c!=null)
{
c = _c;
_c = _c.getParent();
}
if(c instanceof Window) {
topLevelWindow = (Window)c;
topLevelWindow.addComponentListener(this);
} else {
topLevelWindow = null;
System.out.println("toplevel is not a Window: "+c);
}
if(topLevelWindow!=null)
{
topLevelWindow.addWindowListener(this);
} else {
System.out.println("no parent found for "+getName());
System.out.flush();
}
/* to be able for RESIZE event's */
addComponentListener(this);
addMouseListener(this);
/* if we are not allowed to render from the AWT
thread, release the OpenGL context for the
animation thread to use */
if (!enableAWTThreadRendering) {
// makeCurrent() necessary to make GLContext
// realize that JAWT lock has to be released...
glj.gljMakeCurrent();
glj.gljFree();
}
}
if (enableAWTThreadRendering) {
sDisplay();
}
}
// Package-private hack to make this work similarly to old
// releases. This avoids ever calling sDisplay() from the AWT
// thread if the component is a GLAnimCanvas and if
// setUseRepaint(false) has been called.
boolean getUseRepaint() { return true; }
/**
*
* This is your pre-init method.
* preInit is called just BEFORE the GL-Context is created.
* You should override preInit, to initialize your visual-stuff,
* like the protected vars: doubleBuffer and stereoView
*
* @return void
*
* @see gl4java.awt.GLCanvas#paint
* @see gl4java.awt.GLCanvas#doubleBuffer
* @see gl4java.awt.GLCanvas#stereoView
* @see gl4java.awt.GLCanvas#rgba
* @see gl4java.awt.GLCanvas#stencilBits
* @see gl4java.awt.GLCanvas#accumSize
*/
public void preInit()
{
}
/**
*
* init is called right after the GL-Context is initialized.
* The default implementation calls init() on all of this
* component's GLEventListeners.
*
* <p>
* If using the subclassing model, you can override this to
* perform one-time OpenGL initializations such as setting up
* lights and display lists.
*
* @return void
*
* @see gl4java.awt.GLCanvas#paint
* @see gl4java.drawable.GLEventListener#init
*/
public void init()
{
listeners.sendInitEvent(this);
}
/**
* This method is used to clean up any OpenGL stuff (delete textures
* or whatever) prior to actually deleting the OpenGL context.
* You should override this with your own version, if you need to do
* any cleanup work at this phase.
* This functions is called within cvsDispose
*
* @return void
*
* @see gl4java.awt.GLCanvas#cvsDispose
* @see gl4java.drawable.GLEventListener#cleanup
*/
public void doCleanup()
{
listeners.sendCleanupEvent(this);
}
/**
* This function returns, if everything is init: the GLContext,
* the and the users init function
* This value is set in the paint method!
*
* @return boolean
*
* @see gl4java.awt.GLCanvas#paint
* @see gl4java.awt.GLCanvas#init
*/
public boolean cvsIsInit()
{
return initCalledAlmost && glj!=null && glj.gljIsInit();
}
/**
* This function enables, disables the GL-Context !
* If false is given, the openGL renderer/context is
* disabled and disconected (gljFree is called, if initialized) !
*
* If disabled, all GL Functions are disabled but the
* Destroy & Free are not !
*
* @return boolean
*
* @see gl4java.awt.GLCanvas#cvsDispose
* @see gl4java.GLContext#setEnabled
* @see gl4java.GLContext#gljMakeCurrent
* @see gl4java.GLContext#gljDestroy
* @see gl4java.GLContext#gljFree
*/
public void setGLEnabled(boolean b)
{
if(glj!=null)
glj.setEnabled(b);
}
/**
* This function enables, disables the GL-Context !
* If false is given, the openGL renderer/context is
* disabled and disconected (gljFree is called, if initialized) !
*
* If disabled, all GL Functions are disabled but the
* Destroy & Free are not !
*
* The Visible-Flag of this AWT Component is also set to the given value !
* The setVisible(boolean) method of Component is called !
*
* @return boolean
*
* @see gl4java.awt.GLCanvas#cvsDispose
* @see gl4java.GLContext#setEnabled
* @see gl4java.GLContext#gljMakeCurrent
* @see gl4java.GLContext#gljDestroy
* @see gl4java.GLContext#gljFree
*/
public void setVisible(boolean b)
{
if(glj!=null)
glj.setEnabled(b);
super.setVisible(b);
}
/**
* This function queries, if the GL-Context is enabled !
*
* @return boolean
*
* @see gl4java.GLContext#isEnabled
* @see gl4java.GLContext#gljMakeCurrent
*/
public boolean isGLEnabled()
{
if(glj!=null)
return glj.isEnabled();
return false;
}
protected long _f_dur = 0;
/**
* Return the uses milli secounds of the last frame
*/
public long getLastFrameMillis()
{ return _f_dur; }
/**
*
* This is the thread save rendering-method called by paint.
*
* BE SURE, if you want to call 'display' by yourself
* (e.g. in the run method for animation)
* YOU HAVE TO CALL sDisplay -- OR YOU MUST KNOW WHAT YOU ARE DOING THEN !
*
* @return void
*
* @see gl4java.awt.GLCanvas#paint
* @see gl4java.awt.GLCanvas#display
*/
public final void sDisplay()
{
boolean ok = true;
if ( cvsIsInit() )
{
if ( !initCalled && !enableAWTThreadRendering &&
glj.gljMakeCurrent() )
{
if(GLContext.gljClassDebug)
System.out.println("GLCanvas init() will be called now (by rendering thread) !");
init();
initCalled = true;
/* force a reshape, to be sure .. */
mustResize = true;
/* force freeing the context here .. */
glj.gljFree(true);
}
if( mustResize )
{
if( glj.gljMakeCurrent() == true )
{
size = super.getSize();
glj.gljResize( size.width, size.height ) ;
reshape(size.width, size.height);
mustResize = false;
if (enableAWTThreadRendering) {
invalidate();
repaint(100);
}
/* force freeing the context here .. */
glj.gljFree(true);
}
}
long _s = System.currentTimeMillis();
if(ok) display();
_f_dur = System.currentTimeMillis()-_s;
}
}
/**
*
* This is the rendering-method called by sDisplay
* (and sDisplay is called by paint !).
*
* <p>
* The default implementation of display() sends
* preDisplay, display and postDisplay events to
* all {@link gl4java.drawable.GLEventListener}s associated with this
* GLCanvas in the above order.
*
* <p>
* <pre>
reset timer for frame duration (done by sDisplay)
for_all(gl4java.GLEventListener)
SEND preDisplay
if( gljMakeCurrent() )
{
for_all(gl4java.GLEventListener)
SEND display
gljSwap()
gljFree()
for_all(gl4java.GLEventListener)
SEND postDisplay
}
stop timer for frame duration (done by sDisplay)
* </pre>
*
* <p>
* If you use the subclassing model (as opposed to the
* GLEventListener model), your subclass will redefine this to
* perform its OpenGL drawing. In this case you MUST encapsulate
* your OpenGL calls within:
* <pre>
- glj.gljMakeCurrent()
YOUR OpenGL commands here !
- glj.gljFree()
* </pre>
*
* BE SURE, if you want to call 'display' by yourself
* (e.g. in the run method for animation)
* YOU HAVE TO CALL sDisplay !
*
* 'sDisplay' manages a semaphore to avoid reentrance of
* the display function !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*
* @return void
*
* @see gl4java.awt.GLCanvas#sDisplay
* @see gl4java.awt.GLCanvas#paint
* @see gl4java.GLContext#gljMakeCurrent
* @see gl4java.GLContext#gljSwap
* @see gl4java.drawable.GLEventListener#preDisplay
* @see gl4java.drawable.GLEventListener#display
* @see gl4java.drawable.GLEventListener#postDisplay
*/
public void display()
{
if( cvsIsInit() )
{
listeners.sendPreDisplayEvent(this);
if ( glj.gljMakeCurrent() )
{
listeners.sendDisplayEvent(this);
glj.gljSwap();
glj.gljCheckGL();
glj.gljFree();
listeners.sendPostDisplayEvent(this);
}
}
}
/**
*
* This �reshape� method will be invoked after the first paint command
* after GLCanvas.componentResize is called AND only if �gljMakeCurrent� was
* successful (so a call of gljMakeCurrent is redundant).
* �reshape� is not an overloading of java.awt.Component.reshape,
* �reshape� is more like �glut�-reshape.
*
* <p>
* GLCanvas.reshape already has a simple default implementation,
* which calls �gljResize� and �glViewport�. It also sends the
* reshape() event to all GLEventListeners. If using the
* GLEventListener model, it may not be necessary to do anything
* in your event listener's reshape() method; if using the
* subclassing model, it may not be necessary to override this.
*
* <p>
* The needed call to �gljResize� is done by the invoker paint !
*
* @param width the new width
* @param height the new height
* @return void
*
* @see gl4java.awt.GLCanvas#paint
* @see gl4java.awt.GLCanvas#sDisplay
* @see gl4java.drawable.GLEventListener#reshape
*/
public void reshape( int width, int height )
{
gl.glViewport(0,0, width, height);
listeners.sendReshapeEvent(this, width, height);
}
/**
*
* �componentResized� is the componentListeners event handler.
*
* This method sets the variable �mustResize� to true,
* so the upcoming �paint� method-call will invoke �reshape� !
*
* This little look-alike complicating thing is done,
* to avoid an Exception by using the glContext from more than
* one concurrent thread�s !
*
* You cannot override this implementation, it is final
* - override �reshape' instead !
*
* @param e the element, which is resized
* @return void
*
* @see gl4java.awt.GLCanvas#paint
* @see gl4java.awt.GLCanvas#reshape
*/
public void componentResized(ComponentEvent e)
{
if(glj!=null && glj.gljIsInit() && e.getComponent()==this )
{
mustResize = true;
repaint();
}
}
public void componentMoved(ComponentEvent e)
{
if(glj!=null && glj.gljIsInit())
{
repaint(100);
}
}
public void componentShown(ComponentEvent e)
{
}
public void componentHidden(ComponentEvent e)
{ }
public void mouseClicked(MouseEvent e)
{
if(glj!=null && glj.gljIsInit())
{
repaint();
}
}
public void mouseEntered(MouseEvent e)
{ }
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void windowOpened(WindowEvent e)
{
}
/**
*
* �windowClosing� is the windowListeners event handler
* for the topLevelWindow of this Canvas !
*
* This methods free�s AND destroy�s
* the GL Context with �glj.gljDestroy� !
*
* @return void
*
*/
public void windowClosing(WindowEvent e)
{
if(e.getComponent().equals(topLevelWindow))
{
cvsDispose();
}
}
/**
*
* �windowClosed� is the windowListeners event handler.
*
* @return void
*
*/
public void windowClosed(WindowEvent e)
{
if (needCvsDispose) cvsDispose();
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
if(glj!=null && glj.gljIsInit())
{
repaint(100);
}
}
public void windowDeactivated(WindowEvent e)
{
}
/**
* You should call this before releasing/dispose this Window !
* Also you can overwrite this class,
* to dispose your own elements, e.g. a Frame etc. -
* but be shure that you call
* cvsDispose implementation call this one !
*
* This function calls gljDestroy of GLContext !
* The Visibility is set to false !
* The Enabled is set to false either !
*
* To bring this component back to live,
* you need call setVisible(true) !
*
* @see gl4java.GLContext#gljDestroy
* @see gl4java.awt.GLCanvas#doCleanup
*/
public void cvsDispose()
{
if(GLContext.gljClassDebug)
System.out.println("GLCanvas cvsDispose (doit="+
( (glj != null) && glj.gljIsInit() ) +")");
initCalled = false;
initCalledAlmost = false;
removeComponentListener(this);
removeMouseListener(this);
if (glj != null)
{
if (glj.gljIsInit())
{
/* Sometimes the Microsoft VM calls the
Applet.stop() method but doesn't have
permissions to do J/Direct calls, so
this whole block of code will throw a
security exception. If this happens,
however, windowClosing() will still
call us again later and we will have
another opportunity to shut down the
context, so it all works out fine. */
try
{
setVisible(false);
doCleanup();
glj.gljDestroy();
needCvsDispose = false;
}
catch (Exception ex)
{
needCvsDispose = true;
}
}
}
// Setting glj to null will simply cause paint() to re-initialize.
// We don't want that to happen, so we will leave glj non-null.
}
/**
* does nothing than:
*
* @see gl4java.awt.GLCanvas#cvsDispose
*/
protected void finalize()
throws Throwable
{
if(GLContext.gljClassDebug)
System.out.println("GLCanvas finalize ..");
cvsDispose();
super.finalize();
}
/**
* does nothing than:
*
* @see gl4java.awt.GLCanvas#cvsDispose
*
* @deprecated Use cvsDispose instead, well finalize is also implemented
*/
public void destroy()
{
if(GLContext.gljClassDebug)
System.out.println("GLCanvas destroy ..");
cvsDispose();
}
/**
* get methods
*/
public final int cvsGetWidth() {
return getSize().width;
}
public final int cvsGetHeight() {
return getSize().height;
}
//----------------------------------------------------------------------
// Implementation of GLDrawable
//
public void addGLEventListener(GLEventListener listener) {
listeners.add(listener);
}
public void removeGLEventListener(GLEventListener listener) {
listeners.remove(listener);
}
public EventListener[] getListeners(Class listenerType)
throws ClassCastException
{
EventListener[] evtlst=null;
Class _GLEventListener = null;
try {
_GLEventListener = Class.forName("gl4java.drawable.GLEventListener");
} catch (Exception ex) {
System.out.println(ex);
}
if (_GLEventListener!=null &&
listenerType.isAssignableFrom(_GLEventListener) )
evtlst = listeners.getListeners();
EventListener[] t_evtlst = super.getListeners(listenerType);
if(t_evtlst==null || t_evtlst.length==0)
return evtlst;
if(evtlst==null || evtlst.length==0)
return t_evtlst;
EventListener[] n_evtlst =
new EventListener[t_evtlst.length+evtlst.length];
try {
System.arraycopy(evtlst, 0, n_evtlst, 0, evtlst.length);
System.arraycopy(t_evtlst, 0, n_evtlst, evtlst.length, t_evtlst.length);
} catch (Exception ex)
{ System.out.println(ex); }
evtlst = null;
t_evtlst = null;
return n_evtlst;
}
public GLFunc getGL() {
return gl;
}
public GLUFunc getGLU() {
return glu;
}
}
|