JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
JOGLNewtApplet1Run.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.awt.applet;
29
30import java.applet.Applet;
31import java.awt.BorderLayout;
32import java.awt.Button;
33import java.awt.Component;
34import java.awt.Container;
35import java.awt.EventQueue;
36import java.awt.event.KeyListener;
37import java.awt.event.MouseListener;
38import java.awt.event.MouseMotionListener;
39import java.util.Arrays;
40
41import com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode;
42import com.jogamp.opengl.FPSCounter;
43import com.jogamp.opengl.GLCapabilities;
44import com.jogamp.opengl.GLEventListener;
45import com.jogamp.opengl.GLProfile;
46
47import jogamp.nativewindow.jawt.JAWTUtil;
48
49import com.jogamp.common.util.awt.AWTEDTExecutor;
50import com.jogamp.newt.awt.NewtCanvasAWT;
51import com.jogamp.newt.opengl.GLWindow;
52import com.jogamp.newt.util.applet.JOGLNewtAppletBase;
53
54/**
55 * Simple GLEventListener deployment as an applet using JOGL. This demo must be
56 * referenced from a web page via an <applet> tag.
57 *
58 * <p>
59 * Example of an applet tag using GearsES2 within the applet area (normal case):
60 * <pre>
61 &lt;applet width=100 height=100&gt;
62 &lt;param name="java_arguments" value="-Dsun.java2d.noddraw=true"&gt;
63 &lt;param name="gl_event_listener_class" value="com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2"&gt;
64 &lt;param name="gl_profile" value="GL2"&gt;
65 &lt;param name="gl_swap_interval" value="1"&gt;
66 &lt;param name="gl_debug" value="false"&gt;
67 &lt;param name="gl_trace" value="false"&gt;
68 &lt;param name="jnlp_href" value="jogl-newt-applet-runner.jnlp"&gt;
69 &lt;/applet&gt;Hello Gears !
70 * </pre>
71 * </p>
72 *
73 * <p>
74 * Example of an applet tag using GearsES2 in an undecorated, translucent, closeable and always-on-top window:
75 * <pre>
76 &lt;applet width=1 height=1&gt;
77 &lt;param name="java_arguments" value="-Dsun.java2d.noddraw=true"&gt;
78 &lt;param name="gl_event_listener_class" value="com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2"&gt;
79 &lt;param name="gl_profile" value="GL2"&gt;
80 &lt;param name="gl_swap_interval" value="1"&gt;
81 &lt;param name="gl_undecorated" value="true"&gt;
82 &lt;param name="gl_alwaysontop" value="true"&gt;
83 &lt;param name="gl_closeable" value="true"&gt;
84 &lt;param name="gl_alpha" value="1"&gt;
85 &lt;param name="gl_multisamplebuffer" value="0"&gt;
86 &lt;param name="gl_opaque" value="false"&gt;
87 &lt;param name="gl_dx" value="10"&gt;
88 &lt;param name="gl_dy" value="0"&gt;
89 &lt;param name="gl_width" value="100"&gt;
90 &lt;param name="gl_height" value="100"&gt;
91 &lt;param name="gl_nodefaultkeyListener" value="true"&gt;
92 &lt;param name="gl_debug" value="false"&gt;
93 &lt;param name="gl_trace" value="false"&gt;
94 &lt;param name="jnlp_href" value="jogl-newt-applet-runner.jnlp"&gt;
95 &lt;/applet&gt;Hello Gears !
96 * </pre>
97 * </p>
98 */
99@SuppressWarnings("serial")
100public class JOGLNewtApplet1Run extends Applet {
101 public static final boolean DEBUG = JOGLNewtAppletBase.DEBUG;
102
103 GLWindow glWindow = null;
104 NewtCanvasAWT newtCanvasAWT = null;
105 JOGLNewtAppletBase base = null;
106 /** if valid glStandalone:=true (own window) ! */
107 int glXd=Integer.MAX_VALUE, glYd=Integer.MAX_VALUE, glWidth=Integer.MAX_VALUE, glHeight=Integer.MAX_VALUE;
108
109 @Override
110 public void init() {
111 if(DEBUG) {
112 System.err.println("JOGLNewtApplet1Run.init() START - "+currentThreadName());
113 }
114 final Container container = this;
115
116 String glEventListenerClazzName=null;
117 String glProfileName=null;
118 int glSwapInterval=1;
119 boolean glDebug=false;
120 boolean glTrace=false;
121 boolean glUndecorated=false;
122 boolean glAlwaysOnTop=false;
123 boolean glCloseable=false;
124 boolean glOpaque=true;
125 int glAlphaBits=0;
126 int glNumMultisampleBuffer=0;
127 boolean glNoDefaultKeyListener = false;
128 boolean appletDebugTestBorder = false;
129 try {
130 glEventListenerClazzName = getParameter("gl_event_listener_class");
131 glProfileName = getParameter("gl_profile");
132 glSwapInterval = JOGLNewtAppletBase.str2Int(getParameter("gl_swap_interval"), glSwapInterval);
133 glDebug = JOGLNewtAppletBase.str2Bool(getParameter("gl_debug"), glDebug);
134 glTrace = JOGLNewtAppletBase.str2Bool(getParameter("gl_trace"), glTrace);
135 glUndecorated = JOGLNewtAppletBase.str2Bool(getParameter("gl_undecorated"), glUndecorated);
136 glAlwaysOnTop = JOGLNewtAppletBase.str2Bool(getParameter("gl_alwaysontop"), glAlwaysOnTop);
137 glCloseable = JOGLNewtAppletBase.str2Bool(getParameter("gl_closeable"), glCloseable);
138 glOpaque = JOGLNewtAppletBase.str2Bool(getParameter("gl_opaque"), glOpaque);
139 glAlphaBits = JOGLNewtAppletBase.str2Int(getParameter("gl_alpha"), glAlphaBits);
140 glNumMultisampleBuffer = JOGLNewtAppletBase.str2Int(getParameter("gl_multisamplebuffer"), glNumMultisampleBuffer);
141 glXd = JOGLNewtAppletBase.str2Int(getParameter("gl_dx"), glXd);
142 glYd = JOGLNewtAppletBase.str2Int(getParameter("gl_dy"), glYd);
143 glWidth = JOGLNewtAppletBase.str2Int(getParameter("gl_width"), glWidth);
144 glHeight = JOGLNewtAppletBase.str2Int(getParameter("gl_height"), glHeight);
145 glNoDefaultKeyListener = JOGLNewtAppletBase.str2Bool(getParameter("gl_nodefaultkeyListener"), glNoDefaultKeyListener);
146 appletDebugTestBorder = JOGLNewtAppletBase.str2Bool(getParameter("appletDebugTestBorder"), appletDebugTestBorder);
147 } catch (final Exception e) {
148 e.printStackTrace();
149 }
150 if(null==glEventListenerClazzName) {
151 throw new RuntimeException("No applet parameter 'gl_event_listener_class'");
152 }
153 final boolean glStandalone = Integer.MAX_VALUE>glXd && Integer.MAX_VALUE>glYd && Integer.MAX_VALUE>glWidth && Integer.MAX_VALUE>glHeight;
154 if(DEBUG) {
155 System.err.println("JOGLNewtApplet1Run Configuration:");
156 System.err.println("glStandalone: "+glStandalone);
157 if(glStandalone) {
158 System.err.println("pos-size: "+glXd+"/"+glYd+" "+glWidth+"x"+glHeight);
159 }
160 System.err.println("glEventListenerClazzName: "+glEventListenerClazzName);
161 System.err.println("glProfileName: "+glProfileName);
162 System.err.println("glSwapInterval: "+glSwapInterval);
163 System.err.println("glDebug: "+glDebug);
164 System.err.println("glTrace: "+glTrace);
165 System.err.println("glUndecorated: "+glUndecorated);
166 System.err.println("glAlwaysOnTop: "+glAlwaysOnTop);
167 System.err.println("glCloseable: "+glCloseable);
168 System.err.println("glOpaque: "+glOpaque);
169 System.err.println("glAlphaBits: "+glAlphaBits);
170 System.err.println("glNumMultisampleBuffer: "+glNumMultisampleBuffer);
171 System.err.println("glNoDefaultKeyListener: "+glNoDefaultKeyListener);
172 }
173
174 base = new JOGLNewtAppletBase(glEventListenerClazzName,
175 glSwapInterval,
176 glNoDefaultKeyListener,
177 glCloseable,
178 glDebug,
179 glTrace);
180
181 try {
182 final GLCapabilities caps = new GLCapabilities(GLProfile.get(glProfileName));
183 caps.setAlphaBits(glAlphaBits);
184 if(0<glNumMultisampleBuffer) {
185 caps.setSampleBuffers(true);
186 caps.setNumSamples(glNumMultisampleBuffer);
187 }
188 caps.setBackgroundOpaque(glOpaque);
189 glWindow = GLWindow.create(caps);
191 glWindow.setUndecorated(glUndecorated);
192 glWindow.setAlwaysOnTop(glAlwaysOnTop);
194 container.setLayout(new BorderLayout());
195 if(appletDebugTestBorder) {
196 AWTEDTExecutor.singleton.invoke(true, new Runnable() {
197 public void run() {
198 container.add(new Button("North"), BorderLayout.NORTH);
199 container.add(new Button("South"), BorderLayout.SOUTH);
200 container.add(new Button("East"), BorderLayout.EAST);
201 container.add(new Button("West"), BorderLayout.WEST);
202 } } );
203 }
204 base.init(glWindow);
205 if(base.isValid()) {
206 final GLEventListener glEventListener = base.getGLEventListener();
207
208 if(glEventListener instanceof MouseListener) {
209 addMouseListener((MouseListener)glEventListener);
210 }
211 if(glEventListener instanceof MouseMotionListener) {
212 addMouseMotionListener((MouseMotionListener)glEventListener);
213 }
214 if(glEventListener instanceof KeyListener) {
215 addKeyListener((KeyListener)glEventListener);
216 }
217 }
218 if( !glStandalone ) {
219 AWTEDTExecutor.singleton.invoke(true, new Runnable() {
220 public void run() {
221 newtCanvasAWT = new NewtCanvasAWT(glWindow);
222 newtCanvasAWT.setSkipJAWTDestroy(true); // Bug 910
223 container.add(newtCanvasAWT, BorderLayout.CENTER);
224 container.validate();
225 } } );
226 }
227 } catch (final Throwable t) {
228 throw new RuntimeException(t);
229 }
230 if(DEBUG) {
231 System.err.println("JOGLNewtApplet1Run.init() END - "+currentThreadName());
232 }
233 }
234
235 private static String currentThreadName() { return "["+Thread.currentThread().getName()+", isAWT-EDT "+EventQueue.isDispatchThread()+"]"; }
236
237 @Override
238 public void start() {
239 if(DEBUG) {
240 System.err.println("JOGLNewtApplet1Run.start() START (isVisible "+isVisible()+", isDisplayable "+isDisplayable()+") - "+currentThreadName());
241 }
242 final java.awt.Point[] p0 = { null };
243 AWTEDTExecutor.singleton.invoke(true, new Runnable() {
244 public void run() {
245 setVisible(true);
246 p0[0] = getLocationOnScreen();
247 if( null != newtCanvasAWT ) {
248 newtCanvasAWT.setFocusable(true);
249 newtCanvasAWT.requestFocus();
250 }
251 }
252 });
253 if( null == newtCanvasAWT ) {
254 glWindow.requestFocus();
255 glWindow.setSize(glWidth, glHeight);
256 glWindow.setPosition(p0[0].x+glXd, p0[0].y+glYd);
257 }
258 if(DEBUG) {
259 Component topC = this;
260 while (null != topC.getParent()) {
261 topC = topC.getParent();
262 }
263 System.err.println("JOGLNewtApplet1Run start:");
264 System.err.println("TopComponent: "+topC.getLocation()+" rel, "+topC.getLocationOnScreen()+" screen, visible "+topC.isVisible()+", "+topC);
265 System.err.println("Applet Pos: "+this.getLocation()+" rel, "+Arrays.toString(p0)+" screen, visible "+this.isVisible()+", "+this);
266 if(null != newtCanvasAWT) {
267 System.err.println("NewtCanvasAWT Pos: "+newtCanvasAWT.getLocation()+" rel, "+newtCanvasAWT.getLocationOnScreen()+" screen, visible "+newtCanvasAWT.isVisible()+", "+newtCanvasAWT);
268 }
269 System.err.println("GLWindow Pos: "+glWindow.getX()+"/"+glWindow.getY()+" rel, "+glWindow.getLocationOnScreen(null)+" screen");
270 System.err.println("GLWindow: "+glWindow);
271 }
272 base.start();
273 if( null != newtCanvasAWT &&
274 newtCanvasAWT.isOffscreenLayerSurfaceEnabled() &&
275 0 != ( JAWTUtil.JAWT_OSX_CALAYER_QUIRK_POSITION & JAWTUtil.getOSXCALayerQuirks() ) ) {
276 // force relayout
277 AWTEDTExecutor.singleton.invoke(true, new Runnable() {
278 public void run() {
279 final int cW = newtCanvasAWT.getWidth();
280 final int cH = newtCanvasAWT.getHeight();
281 newtCanvasAWT.setSize(cW+1, cH+1);
282 newtCanvasAWT.setSize(cW, cH);
283 } } );
284 }
285 if(DEBUG) {
286 System.err.println("JOGLNewtApplet1Run.start() END - "+currentThreadName());
287 }
288 }
289
290 @Override
291 public void stop() {
292 if(DEBUG) {
293 System.err.println("JOGLNewtApplet1Run.stop() START - "+currentThreadName());
294 }
295 base.stop();
296 if(DEBUG) {
297 System.err.println("JOGLNewtApplet1Run.stop() END - "+currentThreadName());
298 }
299 }
300
301 @Override
302 public void destroy() {
303 if(DEBUG) {
304 System.err.println("JOGLNewtApplet1Run.destroy() START - "+currentThreadName());
305 }
306 AWTEDTExecutor.singleton.invoke(true, new Runnable() {
307 public void run() {
308 glWindow.setVisible(false); // hide 1st
309 if( null != newtCanvasAWT ) {
310 newtCanvasAWT.setSkipJAWTDestroy(false); // Bug 910
311 remove(newtCanvasAWT); // remove newtCanvasAWT incl. glWindow.reparentWindow(null) if not done yet!
312 newtCanvasAWT.destroy();
313 }
314 } } );
315 base.destroy(); // destroy glWindow unrecoverable
316 base=null;
317 glWindow=null;
318 newtCanvasAWT=null;
319 if(DEBUG) {
320 System.err.println("JOGLNewtApplet1Run.destroy() END - "+currentThreadName());
321 }
322 }
323}
324
void setBackgroundOpaque(final boolean opaque)
Sets whether the surface shall be opaque or translucent.
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
AWT Canvas containing a NEWT Window using native parenting.
final boolean isOffscreenLayerSurfaceEnabled()
Returns true if this instance uses an offscreen layer, otherwise false.
final void destroy()
Destroys this resource:
final void setSkipJAWTDestroy(final boolean v)
Mitigates Bug 910 (IcedTea-Web), i.e.
Simple GLEventListener deployment as an applet using JOGL.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
Point getLocationOnScreen(final Point storage)
Returns the window's top-left client-area position in the screen.
Definition: GLWindow.java:643
WindowClosingMode setDefaultCloseOperation(final WindowClosingMode op)
Definition: GLWindow.java:222
final void setPosition(final int x, final int y)
Sets the location of the window's client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:525
final void setAlwaysOnTop(final boolean value)
Definition: GLWindow.java:351
final int getX()
Returns the current x position of this window, relative to it's parent.
Definition: GLWindow.java:436
final int getY()
Returns the current y position of the top-left corner of the client area relative to it's parent in w...
Definition: GLWindow.java:441
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void setUndecorated(final boolean value)
Definition: GLWindow.java:337
final void requestFocus()
Request focus for this native window.
Definition: GLWindow.java:416
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Shows how to deploy an applet using JOGL.
static int str2Int(final String str, final int def)
static boolean str2Bool(final String str, final boolean def)
Specifies a set of OpenGL capabilities.
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
void setSampleBuffers(final boolean enable)
Defaults to false.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
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.
FPSCounter feature.
Definition: FPSCounter.java:37
void setUpdateFPSFrames(int frames, PrintStream out)
static final int DEFAULT_FRAMES_PER_INTERVAL
Definition: FPSCounter.java:38
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.