JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
JOGLNewtApplet3Run.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.applet3;
29
30import java.util.Locale;
31
32import com.jogamp.plugin.applet.Applet3;
33import com.jogamp.plugin.applet.Applet3Context;
34import com.jogamp.plugin.ui.NativeWindowDownstream;
35import com.jogamp.plugin.ui.NativeWindowUpstream;
36
37import com.jogamp.nativewindow.AbstractGraphicsDevice;
38import com.jogamp.nativewindow.AbstractGraphicsScreen;
39import com.jogamp.nativewindow.NativeWindow;
40import com.jogamp.nativewindow.NativeWindowFactory;
41import com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode;
42import com.jogamp.nativewindow.util.PointImmutable;
43import com.jogamp.opengl.FPSCounter;
44import com.jogamp.opengl.GLCapabilities;
45import com.jogamp.opengl.GLProfile;
46
47import com.jogamp.nativewindow.UpstreamWindowHookMutableSizePos;
48import com.jogamp.newt.NewtFactory;
49import com.jogamp.newt.Window;
50import com.jogamp.newt.opengl.GLWindow;
51import com.jogamp.newt.util.applet.JOGLNewtAppletBase;
52
53/**
54 * Simple GLEventListener deployment as an applet using JOGL. This demo must be
55 * referenced from a web page via an <applet> tag.
56 *
57 * <p>
58 * Example of an applet tag using GearsES2 within the applet area (normal case):
59 * <pre>
60 &lt;applet width=100 height=100&gt;
61 &lt;param name="java_arguments" value="-Dsun.java2d.noddraw=true"&gt;
62 &lt;param name="gl_event_listener_class" value="com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2"&gt;
63 &lt;param name="gl_profile" value="GL2"&gt;
64 &lt;param name="gl_swap_interval" value="1"&gt;
65 &lt;param name="gl_debug" value="false"&gt;
66 &lt;param name="gl_trace" value="false"&gt;
67 &lt;param name="jnlp_href" value="jogl-newt-applet-runner.jnlp"&gt;
68 &lt;/applet&gt;Hello Gears !
69 * </pre>
70 * </p>
71 *
72 * <p>
73 * Example of an applet tag using GearsES2 in an undecorated, translucent, closeable and always-on-top window:
74 * <pre>
75 &lt;applet width=1 height=1&gt;
76 &lt;param name="java_arguments" value="-Dsun.java2d.noddraw=true"&gt;
77 &lt;param name="gl_event_listener_class" value="com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2"&gt;
78 &lt;param name="gl_profile" value="GL2"&gt;
79 &lt;param name="gl_swap_interval" value="1"&gt;
80 &lt;param name="gl_undecorated" value="true"&gt;
81 &lt;param name="gl_alwaysontop" value="true"&gt;
82 &lt;param name="gl_closeable" value="true"&gt;
83 &lt;param name="gl_alpha" value="1"&gt;
84 &lt;param name="gl_multisamplebuffer" value="0"&gt;
85 &lt;param name="gl_opaque" value="false"&gt;
86 &lt;param name="gl_dx" value="10"&gt;
87 &lt;param name="gl_dy" value="0"&gt;
88 &lt;param name="gl_width" value="100"&gt;
89 &lt;param name="gl_height" value="100"&gt;
90 &lt;param name="gl_nodefaultkeyListener" value="true"&gt;
91 &lt;param name="gl_debug" value="false"&gt;
92 &lt;param name="gl_trace" value="false"&gt;
93 &lt;param name="jnlp_href" value="jogl-newt-applet-runner.jnlp"&gt;
94 &lt;/applet&gt;Hello Gears !
95 * </pre>
96 * </p>
97 */
98public class JOGLNewtApplet3Run implements Applet3 {
99 public static final boolean DEBUG = JOGLNewtAppletBase.DEBUG;
100
101 GLWindow glWindow = null;
102 JOGLNewtAppletBase base = null;
103 /** if valid glStandalone:=true (own window) ! */
104 int glXd=Integer.MAX_VALUE, glYd=Integer.MAX_VALUE, glWidth=Integer.MAX_VALUE, glHeight=Integer.MAX_VALUE;
105 Applet3Context ctx;
106 boolean glStandalone = false;
107 UpstreamWindowHookMutableSizePos upstreamSizePosHook;
108 PointImmutable upstreamLocOnScreen;
109 NativeWindow browserWin;
110
111 final String getParameter(final String name) {
112 return ctx.getParameter(name);
113 }
114
115 @Override
116 public NativeWindowDownstream createNativeWindow(final Applet3Context ctx, final NativeWindowUpstream upstreamWin) {
117 this.ctx = ctx;
118
119 String glProfileName=null;
120 boolean glOpaque=true;
121 int glAlphaBits=0;
122 int glNumMultisampleBuffer=0;
123 boolean glUndecorated=false;
124 boolean glAlwaysOnTop=false;
125 try {
126 glProfileName = getParameter("gl_profile");
127 glOpaque = JOGLNewtAppletBase.str2Bool(getParameter("gl_opaque"), glOpaque);
128 glAlphaBits = JOGLNewtAppletBase.str2Int(getParameter("gl_alpha"), glAlphaBits);
129 glNumMultisampleBuffer = JOGLNewtAppletBase.str2Int(getParameter("gl_multisamplebuffer"), glNumMultisampleBuffer);
130 glXd = JOGLNewtAppletBase.str2Int(getParameter("gl_dx"), glXd);
131 glYd = JOGLNewtAppletBase.str2Int(getParameter("gl_dy"), glYd);
132 glWidth = JOGLNewtAppletBase.str2Int(getParameter("gl_width"), glWidth);
133 glHeight = JOGLNewtAppletBase.str2Int(getParameter("gl_height"), glHeight);
134 glUndecorated = JOGLNewtAppletBase.str2Bool(getParameter("gl_undecorated"), glUndecorated);
135 glAlwaysOnTop = JOGLNewtAppletBase.str2Bool(getParameter("gl_alwaysontop"), glAlwaysOnTop);
136 } catch (final Exception e) {
137 e.printStackTrace();
138 }
139 glStandalone = Integer.MAX_VALUE>glXd && Integer.MAX_VALUE>glYd && Integer.MAX_VALUE>glWidth && Integer.MAX_VALUE>glHeight;
140 final GLCapabilities caps = new GLCapabilities(GLProfile.get(glProfileName));
141 caps.setAlphaBits(glAlphaBits);
142 if(0<glNumMultisampleBuffer) {
143 caps.setSampleBuffers(true);
144 caps.setNumSamples(glNumMultisampleBuffer);
145 }
146 caps.setBackgroundOpaque(glOpaque);
147
148 final AbstractGraphicsDevice aDevice = NativeWindowFactory.createDevice(upstreamWin.getDisplayConnection(),
149 true /* own */); // open and own! (for upstreamLocOnScreen)
150 final AbstractGraphicsScreen aScreen = NativeWindowFactory.createScreen(aDevice, upstreamWin.getScreenIndex());
151 upstreamSizePosHook = new UpstreamWindowHookMutableSizePos(upstreamWin.getX(), upstreamWin.getY(),
152 upstreamWin.getWidth(), upstreamWin.getHeight(),
153 upstreamWin.getWidth(), upstreamWin.getHeight()); // FIXME: pixel-dim == window-dim 'for now' ?
154 browserWin = NativeWindowFactory.createWrappedWindow(aScreen, 0 /* surfaceHandle */, upstreamWin.getWindowHandle(),
155 upstreamSizePosHook);
156 upstreamLocOnScreen = NativeWindowFactory.getLocationOnScreen(browserWin);
157 if(DEBUG) {
158 System.err.println("JOGLNewtApplet3Run Configuration:");
159 System.err.println("glStandalone: "+glStandalone);
160 System.err.println("glProfileName: "+glProfileName);
161 System.err.println("glOpaque: "+glOpaque);
162 System.err.println("glAlphaBits: "+glAlphaBits);
163 System.err.println("glNumMultisampleBuffer: "+glNumMultisampleBuffer);
164 System.err.println("glUndecorated: "+glUndecorated);
165 System.err.println("glAlwaysOnTop: "+glAlwaysOnTop);
166 System.err.println("UpstreamWin: "+upstreamWin+", LOS "+upstreamLocOnScreen);
167 if(glStandalone) {
168 System.err.println("pos-size: "+glXd+"/"+glYd+" "+glWidth+"x"+glHeight);
169 }
170 }
171
172 final Window w = NewtFactory.createWindow(glStandalone ? null : browserWin, caps);
173 glWindow = GLWindow.create(w);
174 glWindow.setUndecorated(glUndecorated);
175 glWindow.setAlwaysOnTop(glAlwaysOnTop);
176 glWindow.setSize(browserWin.getWidth(), browserWin.getHeight());
177
178 return new NativeWindowDownstream() {
179 @Override
180 public void setVisible(final boolean v) {
181 if( null != glWindow ) {
182 glWindow.setVisible(v);
183 }
184 }
185
186 @Override
187 public void setSize(final int width, final int height) {
188 upstreamSizePosHook.setWinSize(width, height);
189 if( null != glWindow ) {
190 glWindow.setSize(width, height);
191 }
192 }
193
194 @Override
195 public void requestFocus() {
196 if( null != glWindow ) {
197 glWindow.requestFocus();
198 }
199 }
200
201 @Override
202 public void destroy() {
203 if( null != glWindow ) {
204 glWindow.destroy();
205 }
206 }
207
208 @Override
209 public NativeWindowUpstream getParent() {
210 return upstreamWin;
211 }
212
213 @Override
214 public long getWindowHandle() {
215 if( null != glWindow ) {
216 return glWindow.getWindowHandle();
217 } else {
218 return 0;
219 }
220 }
221
222 @Override
223 public void display() {
224 if( null != glWindow ) {
225 glWindow.display();
226 }
227 }
228
229 @Override
230 public void notifyPositionChanged(final NativeWindowUpstream nw) {
231 upstreamSizePosHook.setWinPos(nw.getX(), nw.getY());
232 if( null != glWindow ) {
233 glWindow.setPosition(nw.getX(), nw.getY());
234 }
235 }
236 };
237 }
238
239 @Override
240 public void init(final Applet3Context ctx) {
241 if(DEBUG) {
242 System.err.println("JOGLNewtApplet1Run.init() START - "+currentThreadName());
243 }
244 this.ctx = ctx;
245 String glEventListenerClazzName=null;
246 int glSwapInterval=1;
247 boolean glDebug=false;
248 boolean glTrace=false;
249 boolean glNoDefaultKeyListener = false;
250 boolean glCloseable=false;
251
252 try {
253 glEventListenerClazzName = getParameter("gl_event_listener_class");
254 glSwapInterval = JOGLNewtAppletBase.str2Int(getParameter("gl_swap_interval"), glSwapInterval);
255 glDebug = JOGLNewtAppletBase.str2Bool(getParameter("gl_debug"), glDebug);
256 glTrace = JOGLNewtAppletBase.str2Bool(getParameter("gl_trace"), glTrace);
257 glNoDefaultKeyListener = JOGLNewtAppletBase.str2Bool(getParameter("gl_nodefaultkeyListener"), glNoDefaultKeyListener);
258 glCloseable = JOGLNewtAppletBase.str2Bool(getParameter("gl_closeable"), glCloseable);
259 } catch (final Exception e) {
260 e.printStackTrace();
261 }
262 if(null==glEventListenerClazzName) {
263 throw new RuntimeException("No applet parameter 'gl_event_listener_class'");
264 }
265 if(DEBUG) {
266 System.err.println("JOGLNewtApplet1Run Configuration:");
267 System.err.println("glEventListenerClazzName: "+glEventListenerClazzName);
268 System.err.println("glSwapInterval: "+glSwapInterval);
269 System.err.println("glDebug: "+glDebug);
270 System.err.println("glTrace: "+glTrace);
271 System.err.println("glNoDefaultKeyListener: "+glNoDefaultKeyListener);
272 System.err.println("glCloseable: "+glCloseable);
273 }
274
275 base = new JOGLNewtAppletBase(glEventListenerClazzName,
276 glSwapInterval,
277 glNoDefaultKeyListener,
278 glCloseable,
279 glDebug,
280 glTrace);
281
282 try {
285 base.init(glWindow);
286 } catch (final Throwable t) {
287 throw new RuntimeException(t);
288 }
289 if(DEBUG) {
290 System.err.println("JOGLNewtApplet1Run.init() END - "+currentThreadName());
291 }
292 }
293
294 private static String currentThreadName() { return "["+Thread.currentThread().getName()+"]"; }
295
296 @Override
297 public void start() {
298 if(DEBUG) {
299 System.err.println("JOGLNewtApplet1Run.start() START (isVisible "+glWindow.isVisible()+") - "+currentThreadName());
300 }
301 if( glStandalone ) {
302 glWindow.setSize(glWidth, glHeight);
303 glWindow.setPosition(upstreamLocOnScreen.getX()+glXd, upstreamLocOnScreen.getY()+glYd);
304 glWindow.setVisible(true);
305 glWindow.requestFocus();
306 }
307 if(DEBUG) {
308 System.err.println("JOGLNewtApplet1Run start:");
309 System.err.println("GLWindow Pos: "+glWindow.getX()+"/"+glWindow.getY()+" rel, "+glWindow.getLocationOnScreen(null)+" screen");
310 System.err.println("GLWindow: "+glWindow);
311 }
312 base.start();
313 if(DEBUG) {
314 System.err.println("JOGLNewtApplet1Run.start() END - "+currentThreadName());
315 }
316 }
317
318 @Override
319 public void stop() {
320 if(DEBUG) {
321 System.err.println("JOGLNewtApplet1Run.stop() START - "+currentThreadName());
322 }
323 base.stop();
324 if(DEBUG) {
325 System.err.println("JOGLNewtApplet1Run.stop() END - "+currentThreadName());
326 }
327 }
328
329 @Override
330 public void destroy() {
331 if(DEBUG) {
332 System.err.println("JOGLNewtApplet1Run.destroy() START - "+currentThreadName());
333 }
334 glWindow.setVisible(false); // hide 1st
335 base.destroy(); // destroy glWindow unrecoverable
336 base=null;
337 glWindow=null;
338 browserWin.destroy(); // make sure the open display connection gets closed!
339 browserWin = null;
340 if(DEBUG) {
341 System.err.println("JOGLNewtApplet1Run.destroy() END - "+currentThreadName());
342 }
343 }
344
345 @Override
346 public String getAppletInfo() {
347 return null;
348 }
349
350 @Override
351 public Locale getLocale() {
352 return null;
353 }
354
355 @Override
356 public String[][] getParameterInfo() {
357 return null;
358 }
359
360}
361
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.
Provides a pluggable mechanism for arbitrary window toolkits to adapt their components to the NativeW...
static Point getLocationOnScreen(final NativeWindow nw)
static AbstractGraphicsDevice createDevice(final String displayConnection, final boolean own)
Creates a native device type, following getNativeWindowType(true).
static NativeWindow createWrappedWindow(final AbstractGraphicsScreen aScreen, final long surfaceHandle, final long windowHandle, final UpstreamWindowHookMutableSizePos hook)
Creates a wrapped NativeWindow with given native handles and AbstractGraphicsScreen.
static AbstractGraphicsScreen createScreen(final AbstractGraphicsDevice device, int screen)
final void setWinSize(final int winWidth, final int winHeight)
static Window createWindow(final CapabilitiesImmutable caps)
Create a top level Window entity on the default Display and default Screen.
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 long getWindowHandle()
Returns the window handle for this NativeWindow.
Definition: GLWindow.java:1040
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
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
Simple GLEventListener deployment as an applet using JOGL.
NativeWindowDownstream createNativeWindow(final Applet3Context ctx, final NativeWindowUpstream upstreamWin)
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.
A interface describing a graphics device in a toolkit-independent manner.
A interface describing a graphics screen in a toolkit-independent manner.
Extend the NativeSurface interface with windowing information such as window-handle,...
void destroy()
Destroys this window incl.
Specifying NEWT's Window functionality:
Definition: Window.java:115
FPSCounter feature.
Definition: FPSCounter.java:37
void setUpdateFPSFrames(int frames, PrintStream out)
static final int DEFAULT_FRAMES_PER_INTERVAL
Definition: FPSCounter.java:38