JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
JOGLNewtAppletBase.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.newt.util.applet;
29
30import java.lang.reflect.Field;
31import java.security.PrivilegedAction;
32
33import com.jogamp.nativewindow.NativeWindow;
34import com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode;
35import com.jogamp.nativewindow.util.InsetsImmutable;
36import com.jogamp.opengl.FPSCounter;
37import com.jogamp.opengl.GL;
38import com.jogamp.opengl.GLAutoDrawable;
39import com.jogamp.opengl.GLEventListener;
40import com.jogamp.opengl.GLPipelineFactory;
41
42import jogamp.newt.Debug;
43
44import com.jogamp.common.util.InterruptSource;
45import com.jogamp.common.util.SecurityUtil;
46import com.jogamp.newt.Window;
47import com.jogamp.newt.event.KeyEvent;
48import com.jogamp.newt.event.KeyListener;
49import com.jogamp.newt.event.MouseListener;
50import com.jogamp.newt.event.WindowAdapter;
51import com.jogamp.newt.event.WindowEvent;
52import com.jogamp.newt.event.WindowListener;
53import com.jogamp.newt.opengl.GLWindow;
54import com.jogamp.newt.opengl.util.NEWTDemoListener;
55import com.jogamp.opengl.util.Animator;
56import com.jogamp.opengl.util.AnimatorBase;
57
58
59/**
60 * Shows how to deploy an applet using JOGL.
61 * This demo must be referenced from a web page via an <applet> tag.
62 * <p>
63 * The demo code uses {@link NEWTDemoListener} functionality.
64 * </p>
65 */
67 public static final boolean DEBUG = Debug.debug("Applet");
68
69 String glEventListenerClazzName;
70 int glSwapInterval;
71 boolean noDefaultKeyListener;
72 boolean glClosable;
73 boolean glDebug;
74 boolean glTrace;
75
76 GLEventListener glEventListener = null;
77 GLWindow glWindow = null;
78 Animator glAnimator=null;
79 boolean isValid = false;
80 NativeWindow parentWin;
81
82 public JOGLNewtAppletBase(final String glEventListenerClazzName,
83 final int glSwapInterval,
84 final boolean noDefaultKeyListener,
85 final boolean glClosable,
86 final boolean glDebug,
87 final boolean glTrace) {
88
89 this.glEventListenerClazzName=glEventListenerClazzName;
90 this.glSwapInterval=glSwapInterval;
91 this.noDefaultKeyListener = noDefaultKeyListener;
92 this.glClosable = glClosable;
93 this.glDebug = glDebug;
94 this.glTrace = glTrace;
95 }
96
97 public GLEventListener getGLEventListener() { return glEventListener; }
98 public GLWindow getGLWindow() { return glWindow; }
99 public Animator getGLAnimator() { return glAnimator; }
100 public boolean isValid() { return isValid; }
101
102 public static boolean str2Bool(final String str, final boolean def) {
103 if(null==str) return def;
104 try {
105 return Boolean.valueOf(str).booleanValue();
106 } catch (final Exception ex) { ex.printStackTrace(); }
107 return def;
108 }
109
110 public static int str2Int(final String str, final int def) {
111 if(null==str) return def;
112 try {
113 return Integer.parseInt(str);
114 } catch (final Exception ex) { ex.printStackTrace(); }
115 return def;
116 }
117
118 public static GLEventListener createInstance(final String clazzName) {
119 Object instance = null;
120
121 try {
122 final Class<?> clazz = SecurityUtil.doPrivileged(new PrivilegedAction<Class<?>>() {
123 @Override
124 public Class<?> run() {
125 final ClassLoader cl = Thread.currentThread().getContextClassLoader();
126 Class<?> clazz = null;
127 try {
128 clazz = Class.forName(clazzName, false, cl);
129 } catch (final Throwable t) {
130 t.printStackTrace();
131 }
132 return clazz;
133 }
134 });
135 instance = clazz.newInstance();
136 } catch (final Throwable t) {
137 t.printStackTrace();
138 throw new RuntimeException("Error while instantiating demo: "+clazzName);
139 }
140 if( null == instance ) {
141 throw new RuntimeException("Null GLEventListener: "+clazzName);
142 }
143 if( !(instance instanceof GLEventListener) ) {
144 throw new RuntimeException("Not a GLEventListener: "+clazzName);
145 }
146 return (GLEventListener) instance;
147 }
148
149 public static boolean setField(final Object instance, final String fieldName, final Object value) {
150 try {
151 final Field f = instance.getClass().getField(fieldName);
152 if(f.getType().isInstance(value)) {
153 f.set(instance, value);
154 return true;
155 } else {
156 System.out.println(instance.getClass()+" '"+fieldName+"' field not assignable with "+value.getClass()+", it's a: "+f.getType());
157 }
158 } catch (final NoSuchFieldException nsfe) {
159 System.out.println(instance.getClass()+" has no '"+fieldName+"' field");
160 } catch (final Throwable t) {
161 t.printStackTrace();
162 }
163 return false;
164 }
165
166 public void init(final GLWindow glWindow) {
167 init(Thread.currentThread().getThreadGroup(), glWindow);
168 }
169
170 public void init(final ThreadGroup tg, final GLWindow glWindow) {
171 isValid = false;
172 this.glWindow = glWindow;
173 glEventListener = createInstance(glEventListenerClazzName);
174 if(null == glEventListener) {
175 return;
176 }
177
178 try {
179 if(!setField(glEventListener, "window", glWindow)) {
180 setField(glEventListener, "glWindow", glWindow);
181 }
182
183 glWindow.addGLEventListener(this);
184 glWindow.addGLEventListener(glEventListener);
185
186 if(glEventListener instanceof WindowListener) {
187 glWindow.addWindowListener((WindowListener)glEventListener);
188 }
189
190 if(glEventListener instanceof MouseListener) {
191 glWindow.addMouseListener((MouseListener)glEventListener);
192 }
193
194 if(glEventListener instanceof KeyListener) {
195 glWindow.addKeyListener((KeyListener)glEventListener);
196 }
197
198 glWindow.addWindowListener(reparentHomeListener);
199
200 if(!noDefaultKeyListener) {
201 glWindow.addKeyListener(this);
202 final NEWTDemoListener newtDemoListener = new NEWTDemoListener(glWindow);
203 glWindow.addKeyListener(newtDemoListener);
204 glWindow.addMouseListener(newtDemoListener);
205 }
206
208
209 // glAnimator = new FPSAnimator(canvas, 60);
210 glAnimator = new Animator(0 /* w/o AWT */);
211 glAnimator.setThreadGroup(tg);
212 glAnimator.add(glWindow);
214
215 } catch (final Throwable t) {
216 throw new RuntimeException(t);
217 }
218 isValid = true;
219 }
220
221 private final WindowListener reparentHomeListener = new WindowAdapter() {
222 // Closing action: back to parent!
223 @Override
224 public void windowDestroyNotify(final WindowEvent e) {
225 if( isValid() && WindowClosingMode.DO_NOTHING_ON_CLOSE == glWindow.getDefaultCloseOperation() &&
226 null == glWindow.getParent() && null != parentWin && 0 != parentWin.getWindowHandle() )
227 {
228 // we may be called directly by the native EDT
229 new InterruptSource.Thread(null, new Runnable() {
230 @Override
231 public void run() {
232 if( glWindow.isNativeValid() && null != parentWin && 0 != parentWin.getWindowHandle() ) {
233 glWindow.reparentWindow(parentWin, -1, -1, Window.REPARENT_HINT_BECOMES_VISIBLE);
234 }
235 }
236 }).start();
237 }
238 } };
239
240 public void start() {
241 if(isValid) {
242 glWindow.setVisible(true);
244 glAnimator.start();
245 parentWin = glWindow.getParent();
246 }
247 }
248
249 public void stop() {
250 if(null!=glAnimator) {
251 glAnimator.stop();
252 glWindow.setVisible(false);
253 }
254 }
255
256 public void destroy() {
257 isValid = false;
258 if(null!=glAnimator) {
259 glAnimator.stop();
260 glAnimator.remove(glWindow);
261 glAnimator=null;
262 }
263 if(null!=glWindow) {
264 glWindow.destroy();
265 glWindow=null;
266 }
267 }
268
269 // ***********************************************************************************
270 // ***********************************************************************************
271 // ***********************************************************************************
272
273 @Override
274 public void init(final GLAutoDrawable drawable) {
275 GL _gl = drawable.getGL();
276
277 if(glDebug) {
278 try {
279 _gl = _gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Debug", null, _gl, null) );
280 } catch (final Exception e) {e.printStackTrace();}
281 }
282
283 if(glTrace) {
284 try {
285 // Trace ..
286 _gl = _gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Trace", null, _gl, new Object[] { System.err } ) );
287 } catch (final Exception e) {e.printStackTrace();}
288 }
289 _gl.setSwapInterval(glSwapInterval);
290 }
291 @Override
292 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
293 }
294 @Override
295 public void display(final GLAutoDrawable drawable) {
296 }
297 @Override
298 public void dispose(final GLAutoDrawable drawable) {
299 }
300
301 // ***********************************************************************************
302 // ***********************************************************************************
303 // ***********************************************************************************
304
305 @Override
306 public void keyPressed(final KeyEvent e) {
307 if( !e.isPrintableKey() || e.isAutoRepeat() ) {
308 return;
309 }
310
311 if(e.getKeyChar()=='r' && 0==e.getModifiers() && null!=parentWin) {
312 e.setConsumed(true);
313 glWindow.invokeOnNewThread(null, false, new Runnable() {
314 @Override
315 public void run() {
316 if(null == glWindow.getParent()) {
317 glWindow.reparentWindow(parentWin, -1, -1, 0 /* hints */);
318 } else {
319 final InsetsImmutable insets = glWindow.getInsets();
320 final int x, y;
321 if ( 0 >= insets.getTopHeight() ) {
322 // fail safe ..
323 x = 32;
324 y = 32;
325 } else {
326 x = insets.getLeftWidth();
327 y = insets.getTopHeight();
328 }
329 glWindow.reparentWindow(null, x, y, 0 /* hints */);
331 }
332 } } );
333 }
334 }
335
336 @Override
337 public void keyReleased(final KeyEvent e) {
338 }
339
340}
341
final boolean isAutoRepeat()
getModifiers() contains AUTOREPEAT_MASK.
final int getModifiers()
Return the modifier bits of this event, e.g.
final char getKeyChar()
Returns the UTF-16 character reflecting the key symbol incl.
Definition: KeyEvent.java:161
static boolean isPrintableKey(final short uniChar, final boolean isKeyChar)
Returns true if given uniChar represents a printable character, i.e.
Definition: KeyEvent.java:316
final void setConsumed(final boolean consumed)
If consumed is true, this event is marked as consumed, ie.
Definition: NEWTEvent.java:126
NEWT Window events are provided for notification purposes ONLY.
static final short EVENT_WINDOW_RESIZED
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final NativeWindow getParent()
Definition: GLWindow.java:282
WindowClosingMode setDefaultCloseOperation(final WindowClosingMode op)
Definition: GLWindow.java:222
final void addMouseListener(final MouseListener l)
Appends the given MouseListener to the end of the list.
Definition: GLWindow.java:927
final ReparentOperation reparentWindow(final NativeWindow newParent, final int x, final int y, final int hints)
Change this window's parent window.
Definition: GLWindow.java:582
final void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
Definition: GLWindow.java:902
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
WindowClosingMode getDefaultCloseOperation()
Definition: GLWindow.java:217
void sendWindowEvent(final int eventType)
Send a WindowEvent to all WindowListener.
Definition: GLWindow.java:862
final InsetsImmutable getInsets()
Returns the insets defined as the width and height of the window decoration on the left,...
Definition: GLWindow.java:431
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
Shows how to deploy an applet using JOGL.
void init(final ThreadGroup tg, final GLWindow glWindow)
static int str2Int(final String str, final int def)
void keyPressed(final KeyEvent e)
A key has been pressed, excluding auto-repeat modifier keys.
JOGLNewtAppletBase(final String glEventListenerClazzName, final int glSwapInterval, final boolean noDefaultKeyListener, final boolean glClosable, final boolean glDebug, final boolean glTrace)
void keyReleased(final KeyEvent e)
A key has been released, excluding auto-repeat modifier keys.
static boolean setField(final Object instance, final String fieldName, final Object value)
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
static GLEventListener createInstance(final String clazzName)
static boolean str2Bool(final String str, final boolean def)
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
abstract GL setGL(GL gl)
Sets the GL pipeline object for this GLContext.
Factory for pipelining GL instances.
static final GL create(final String pipelineClazzBaseName, final Class<?> reqInterface, final GL downstream, final Object[] additionalArgs)
Creates a pipelined GL instance using the given downstream downstream and optional arguments addition...
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final synchronized void remove(final GLAutoDrawable drawable)
Removes a drawable from the animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized void setThreadGroup(final ThreadGroup tg)
Set a ThreadGroup for the animation thread.
Definition: Animator.java:336
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
Window closing mode if triggered by toolkit close operation.
DO_NOTHING_ON_CLOSE
Do nothing on native window close operation.
DISPOSE_ON_CLOSE
Dispose resources on native window close operation.
Extend the NativeSurface interface with windowing information such as window-handle,...
long getWindowHandle()
Returns the window handle for this NativeWindow.
Immutable insets representing rectangular window decoration insets on all four edges in window units.
Specifying NEWT's Window functionality:
Definition: Window.java:115
static final int REPARENT_HINT_BECOMES_VISIBLE
Reparenting hint (bitfield value): Claim window becomes visible after reparenting,...
Definition: Window.java:914
Listener for KeyEvents.
FPSCounter feature.
Definition: FPSCounter.java:37
void setUpdateFPSFrames(int frames, PrintStream out)
static final int DEFAULT_FRAMES_PER_INTERVAL
Definition: FPSCounter.java:38
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
void setSwapInterval(int interval)
Set the swap interval of the current context and attached onscreen GLDrawable.
GLContext getContext()
Returns the GLContext associated which this GL object.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.