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
|
/*
* Copyright (c) 2006 Erik Tollerud (erik.tollerud@gmail.com) All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of Erik Tollerud, Sun Microsystems, Inc. or the names of
* contributors may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ERIK TOLLERUD,
* SUN MICROSYSTEMS, INC. ("SUN"), AND SUN'S LICENSORS SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL ERIK
* TOLLERUD, SUN, OR SUN'S LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF BEN
* CHAPPELL OR SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use
* in the design, construction, operation or maintenance of any nuclear
* facility.
*/
package net.java.joglutils;
import javax.media.opengl.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.opengl.util.Animator;
/**
* A JFrame containing a heavyweight {@link GLCanvas} with a single attached {@link GLEventListener}.
* Note: the default close operation has been changed to exit rather than dispose.
* @author Erik J. Tollerud
*/
public class GLJFrame extends JFrame {
private GLEventListener listener;
private GLCapabilities caps;
private GLCapabilitiesChooser chooser;
private Animator animator;
private GLContext contextToShareWith;
/**
* Creates new form GLJFrame
* @param listener the GLEventListener to attach to the GLCanvas
*/
public GLJFrame(GLEventListener listener) {
this("OpenGL Window", listener);
}
/**
* Creates new form GLJFrame
* @param title the title for the window
* @param listener the GLEventListener to attach to the GLCanvas
*/
public GLJFrame(String title, GLEventListener listener) {
super(title);
this.listener = listener;
this.caps = new GLCapabilities();
this.chooser = null; //can be null because that will choose the default
initComponents();
((GLCanvas)mainCanvas).addGLEventListener(listener);
animator = null;
this.contextToShareWith = null;
}
/**
* Creates new form GLJFrame
* @param caps the GLCapabilities to request for the GLCanvas
* @param chooser the capabilities chooser to use in creating the GLCanvas on this frame
* @param title the title for the window
* @param listener the GLEventListener to attach to the GLCanvas
* @param contextToShareWith the context to share with
*/
public GLJFrame(String title, GLEventListener listener, GLCapabilities caps,GLCapabilitiesChooser chooser,GLContext contextToShareWith) {
super(title);
this.listener = listener;
this.caps = caps;
this.chooser = chooser;
this.contextToShareWith = contextToShareWith;
initComponents();
((GLCanvas)mainCanvas).addGLEventListener(listener);
animator = null;
}
/**
* Creates new form GLJFrame
* @param title title for the window
* @param listener the GLEventListener to attach to the GLCanvas
* @param capabilities the GLCapabilities to request for the GLCanvas
*/
public GLJFrame(String title, GLEventListener listener, GLCapabilities capabilities) {
this(title,listener,capabilities,null,null);
}
/**
* Creates new form GLJFrame
* @param title title for the window
* @param listener the GLEventListener to attach to the GLCanvas
* @param capabilities the GLCapabilities to request for the GLCanvas
* @param chooser the capabilities chooser to use in creating the GLCanvas on this frame
*/
public GLJFrame(String title, GLEventListener listener, GLCapabilities capabilities, GLCapabilitiesChooser chooser) {
this(title,listener,capabilities,chooser,null);
}
/**
* Creates new form GLJFrame
* @param listener the GLEventListener to attach to the GLCanvas
* @param contextToShareWith the context to share with
* @see javax.media.opengl.GLCanvas#javax.media.opengl.GLCanvas(javax.media.opengl.GLCapabilities,javax.media.opengl.GLCapabilitiesChooser,javax.media.opengl.GLContext,javax.media.opengl.GraphicsDevice)
*/
public GLJFrame(GLEventListener listener, GLContext contextToShareWith) {
this(listener);
this.contextToShareWith = contextToShareWith;
}
/**
* Creates new form GLJFrame
* @param listener the GLEventListener to attach to the GLCanvas
* @param capabilities the GLCapabilities to request for the GLCanvas
*/
public GLJFrame(GLEventListener listener, GLCapabilities capabilities) {
this("OpenGL Window", listener, capabilities);
}
/**
* Creates new form GLJFrame
* @param listener the GLEventListener to attach to the GLCanvas
* @param width the horizontal size for the GLCanvas in pixels
* @param height the vertical size for the GLCanvas in pixels
*/
public GLJFrame(GLEventListener listener, int width, int height) {
this("OpenGL Window",listener);
mainCanvas.setSize(width,height);
this.pack();
}
/**
* Creates new form GLJFrame
* @param title the title for the window
* @param listener the GLEventListener to attach to the GLCanvas
* @param width the horizontal size for the GLCanvas in pixels
* @param height the vertical size for the GLCanvas in pixels
*/
public GLJFrame(String title, GLEventListener listener, int width, int height) {
this(title,listener);
mainCanvas.setSize(width,height);
this.pack();
}
/**
* Creates new form GLJFrame
* @param listener the GLEventListener to attach to the GLCanvas
* @param fullscreen if true, this window is generated in fullscreen mode
*/
public GLJFrame(GLEventListener listener, boolean fullscreen) {
this("OpenGL Window",listener);
this.setFullscreen(fullscreen);
}
/**
* Creates new form GLJFrame
* @param title the title for the window
* @param listener the GLEventListener to attach to the GLCanvas
* @param fullscreen if true, this window is generated in fullscreen mode
*/
public GLJFrame(String title, GLEventListener listener, boolean fullscreen) {
this(title,listener);
this.setFullscreen(fullscreen);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
mainCanvas = new GLCanvas(caps,chooser,contextToShareWith,null);
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
formComponentResized(evt);
}
});
mainCanvas.setSize(500,500);
getContentPane().add(mainCanvas, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
this.mainCanvas.setSize(this.getRootPane().getSize());
}//GEN-LAST:event_formComponentResized
// Variables declaration - do not modify//GEN-BEGIN:variables
private java.awt.Canvas mainCanvas;
// End of variables declaration//GEN-END:variables
/**
* Sets the event listener attached to the GLcanvas. Note that this method does not repaint this component, so it will not immediately update.
* @param listener the GLEventListener to attach to the GLCanvas
*/
public void setGLEventListener(GLEventListener listener) {
((GLCanvas)this.mainCanvas).removeGLEventListener(this.listener);
this.listener = listener;
((GLCanvas)this.mainCanvas).addGLEventListener(listener);
}
/**
* Retrieves the event listener for the GLJFrame
* @return the GLEventListener associated with the GLCanvas
*/
public GLEventListener getGLEventListener() {
return this.listener;
}
/**
* Sets the fullscreen status of this window. If full screen exclusive mode is not supported, fullscreen is emulated by swing (see <a href="http://java.sun.com/docs/books/tutorial/extra/fullscreen/exclusivemode.html"> Full-Screen Exclusive Mode Tutorial </a>). If fullscreen is to be invoked under Windows, it is recommended that the VM be run with -Dsun.java2d.noddraw=true
* @param fs the mode to set this window to - true for full screen, false for windowed mode
* @return true if full screen exclusive mode is supported, false if not.
*/
public boolean setFullscreen(boolean fs) {
//TODO: fix to make double-buffered and return to non-fs mode properly (use initComponents()?)
GraphicsDevice dev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
boolean visible = this.isVisible();
if (fs) {
this.dispose();
this.setUndecorated(true);
this.setResizable(false);
try{
dev.setFullScreenWindow(this);
} catch (Exception e) {
dev.setFullScreenWindow(null);
e.printStackTrace();
JOptionPane.showMessageDialog(this,"Could not enter fullscreen exclusive mode.","Fullscreen error",JOptionPane.ERROR_MESSAGE);
}
this.setVisible(visible);
} else {
this.dispose();
this.setUndecorated(false);
this.setResizable(true);
dev.setFullScreenWindow(null);
this.setVisible(visible);
}
return dev.isFullScreenSupported();
}
/**
* Resizes this GLJFrame to match a GLCanvas of the specified size. This has no effect if fullscreen mode is active.
* @param width the new horizontal size for the GLCanvass in pixels
* @param height the new vertical size for the GLCanvass in pixels
*/
public void setSize(int width, int height) {
java.awt.GraphicsDevice dev = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (dev.getFullScreenWindow() != this) {
mainCanvas.setSize(width,height);
this.pack();
mainCanvas.repaint();
}
}
/**
* Resizes this GLJFrame to match a GLCanvas of the specified size. This has no effect if fullscreen mode is active.
* @param d the new size for the GLCanvass in pixels
*/
public void setSize(java.awt.Dimension d) {
java.awt.GraphicsDevice dev = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (dev.getFullScreenWindow() != this) {
mainCanvas.setSize(d);
this.pack();
}
}
/**
* Determines if the Frame is the Full-Screen Exclusive Mode window.
* @return true if this window is in fullscreen mode
*/
public boolean isFullscreen() {
java.awt.GraphicsDevice dev = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
return (dev.getFullScreenWindow() == this);
}
/**
* The {@link GLCapabilities} used with the GLCanvas.
* @return a copy of the capabilities used to generate the attached GLCanvas
*/
public GLCapabilities getGLCapabilities() {
return (GLCapabilities)this.caps.clone();
}
/**
* Rebuilds the GLCanvas with the specified capbilities. Will dispose and re-create the JFrame and GLCanvas with the new capabilities.
* @param caps the capabilities to be copied into this GLJFrame
*/
public void setGLCapabilities(GLCapabilities caps) {
//get old setup
boolean vis = this.isVisible();
this.caps = (GLCapabilities)caps.clone();
boolean fs = this.isFullscreen();
java.awt.Dimension d = this.mainCanvas.getSize();
this.dispose();
//re-form canvas with new caps
this.mainCanvas = new GLCanvas(caps);
this.getContentPane().removeAll();
this.add(mainCanvas);
mainCanvas.setSize(d);
GLCanvas glc = (GLCanvas) mainCanvas;
//re-apply GLEventListener
glc.addGLEventListener(listener);
//re-apply input listeners from the JPanel listeners
for(InputMethodListener l : this.getInputMethodListeners())
glc.addInputMethodListener(l);
for(KeyListener l : this.getKeyListeners())
glc.addKeyListener(l);
for(MouseListener l : this.getMouseListeners())
glc.addMouseListener(l);
for(MouseMotionListener l : this.getMouseMotionListeners())
glc.addMouseMotionListener(l);
for(MouseWheelListener l : this.getMouseWheelListeners())
glc.addMouseWheelListener(l);
this.setFullscreen(fs);
this.setVisible(vis);
}
/**
* Repaint the JFrame and render the GLCanvas if no animator is present. If animator is attached and running, only repaints the JFrame.
*/
public void repaint() {
super.repaint();
if (this.animator == null)
((GLCanvas)this.mainCanvas).display();
else if (!this.animator.isAnimating())
((GLCanvas)this.mainCanvas).display();
}
/**
* Removes InputMethodListeners, KeyListeners,MouseListeners, MouseMotionListeners, and MouseWheelListeners from this and the GLCanvas.
*/
public void clearInputListeners() {
InputMethodListener[] imls = super.getInputMethodListeners();
KeyListener[] kls = super.getKeyListeners();
MouseListener[] mls = super.getMouseListeners();
MouseMotionListener[] mmls = super.getMouseMotionListeners();
MouseWheelListener[] mwls = super.getMouseWheelListeners();
for(InputMethodListener l : imls) {
mainCanvas.removeInputMethodListener(l);
super.removeInputMethodListener(l);
}
for(KeyListener l : kls){
mainCanvas.removeKeyListener(l);
super.removeKeyListener(l);
}
for(MouseListener l : mls){
mainCanvas.removeMouseListener(l);
super.removeMouseListener(l);
}
for(MouseMotionListener l : mmls){
mainCanvas.removeMouseMotionListener(l);
super.removeMouseMotionListener(l);
}
for(MouseWheelListener l : mwls){
mainCanvas.removeMouseWheelListener(l);
super.removeMouseWheelListener(l);
}
}
/**
* Generates a new {@link Animator} for updating the GLCanvas, and starts it.
* @return the generated Animator
*/
public Animator generateAnimator() {
if (this.animator != null) {
this.animator.stop();
this.animator.remove((GLCanvas)mainCanvas);
}
this.animator = new Animator((GLCanvas)mainCanvas);
this.animator.start();
return this.animator;
}
/**
* Retrieves the attached Animator connected to the GLCanvas.
* @return The Animator attached to this, or null if there is no Animator
*/
public Animator getAnimator() {
return this.animator;
}
/**
* Specifies an {@link Animator} for updating the GLCanvas, and starts it.
* @param anim Animator to use to animate the GLCanvas
*/
public void setAnimator(Animator anim) {
this.setAnimator(anim,true);
}
/**
* Specifies an {@link Animator} for updating the GLCanvas, and optionally starts it.
* @param anim Animator to use to animate the GLCanvas
* @param start starts the animator if true, just sets it if not
*/
public void setAnimator(Animator anim, boolean start) {
if (this.animator != null) {
this.animator.stop();
this.animator.remove((GLCanvas)mainCanvas);
}
this.animator = anim;
this.animator.add((GLCanvas)mainCanvas);
if (start)
this.animator.start();
}
/**
* Stops and removes the {@link Animator}.
*/
public void removeAnimator() {
this.animator.stop();
this.animator.remove((GLCanvas)mainCanvas);
this.animator = null;
}
/**
* Determines if this GLJFrame is running on an {@link Animator}.
* @return true if the GLJFrame has an attached animator.
*/
public boolean isAnimated() {
return (animator!=null);
}
/**
* Removes the specified key listener so that it no longer receives key events from this component and the GLCanvas. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. if the listener is null, no exception is thrown and no action is performed.
* @param l the key listener.
*/
public void removeKeyListener(KeyListener l) {
super.removeKeyListener(l);
mainCanvas.removeKeyListener(l);
}
/**
* Adds the specified key listener to receive key events from this component and the GLCanvas. If l is null, no exception is thrown and no action is performed.
* @param l the key listener.
*/
public void addKeyListener(KeyListener l) {
super.addKeyListener(l);
mainCanvas.addKeyListener(l);
}
/**
* Removes the specified mouse listener so that it no longer receives mouse events from this component and the GLCanvas. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added. if the listener is null, no exception is thrown and no action is performed.
* @param l the mouse listener
*/
public void removeMouseListener(MouseListener l) {
super.removeMouseListener(l);
mainCanvas.removeMouseListener(l);
}
/**
* Adds the specified mouse listener to receive mouse events from this component and the GLCanvas. if the listener is null, no exception is thrown and no action is performed.
* @param l the mouse listener
*/
public void addMouseListener(MouseListener l) {
super.addMouseListener(l);
mainCanvas.addMouseListener(l);
}
/**
* Removes the specified mouse wheel listener so that it no longer receives mouse wheel events from this component and the GLCanvas. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added. If l is null, no exception is thrown and no action is performed.
* @param l the mouse wheel listener
*/
public void removeMouseWheelListener(MouseWheelListener l) {
super.removeMouseWheelListener(l);
mainCanvas.removeMouseWheelListener(l);
}
/**
* Removes the specified mouse motion listener so that it no longer receives mouse motion events from this component and the GLCanvas. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added. if the listener is null, no exception is thrown and no action is performed.
* @param l the mouse motion listener.
*/
public void removeMouseMotionListener(MouseMotionListener l) {
super.removeMouseMotionListener(l);
mainCanvas.removeMouseMotionListener(l);
}
/**
*
* Adds the specified mouse wheel listener to receive mouse wheel events from this component and the GLCanvas.
*
* For information on how mouse wheel events are dispatched, see the class description for {@link MouseWheelEvent}.
*
* If l is null, no exception is thrown and no action is performed.
* @param l the mouse wheel listener
*/
public void addMouseWheelListener(MouseWheelListener l) {
super.addMouseWheelListener(l);
mainCanvas.addMouseWheelListener(l);
}
/**
* Adds the specified mouse motion listener to receive mouse motion events from this component and the GLCanvas. if the listener is null, no exception is thrown and no action is performed.
* @param l the mouse motion listener.
*/
public void addMouseMotionListener(MouseMotionListener l) {
super.addMouseMotionListener(l);
mainCanvas.addMouseMotionListener(l);
}
/**
* Removes the specified input method listener so that it no longer receives input method events from this component and the GLCanvas. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added. if the listener is null, no exception is thrown and no action is performed.
* @param l the input method listener
*/
public void removeInputMethodListener(java.awt.event.InputMethodListener l) {
super.removeInputMethodListener(l);
mainCanvas.removeInputMethodListener(l);
}
/**
* Adds the specified input method listener to receive input method events from this component and the GLCanvas. A component will only receive input method events from input methods if it also overrides getInputMethodRequests to return an InputMethodRequests instance. if the listener is null, no exception is thrown and no action is performed.
* @param l the input method listener
*/
public void addInputMethodListener(java.awt.event.InputMethodListener l) {
super.addInputMethodListener(l);
mainCanvas.addInputMethodListener(l);
}
/**
* Gets the GL pipeline for the canvas in this GLJPane.
* @return the GL pipeline associated with this GLCanvas
* @see javax.media.opengl.GLCanvas#getGL()
*/
public GL getGL() {
return ((GLCanvas)mainCanvas).getGL();
}
/**
* Sets the GL pipeline for the canvas in this GLJPane.
* @see javax.media.opengl.GLCanvas#setGL(javax.media.opengl.GL)
* @param gl The pipeline to attach to this GLCanvas
*/
public void setGL(GL gl) {
((GLCanvas)mainCanvas).setGL(gl);
}
/**
* Retrieves the GLContext associated with the GLCanvas on this GLJFrame
* @see javax.media.opengl.GLCanvas#getContext()
* @return the associated GLContext
*/
public GLContext getContext() {
return ((GLCanvas)mainCanvas).getContext();
}
/**
* Retrieves a GLAutoDrawable view of the associated GLCanvas
* @return a GLAutoDrawable corresponding to the GLCanvas on this GLJFrame
*/
public GLAutoDrawable getAutoDrawable() {
return (GLAutoDrawable)mainCanvas;
}
}
|