JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Bug735Inv3AppletAWT.java
Go to the documentation of this file.
1package com.jogamp.opengl.test.bugs;
2
3import java.applet.Applet;
4import java.awt.BorderLayout;
5import java.awt.Color;
6import java.awt.Frame;
7import java.awt.GraphicsDevice;
8import java.awt.GraphicsEnvironment;
9import java.awt.Insets;
10import java.awt.Rectangle;
11import java.awt.event.WindowAdapter;
12import java.awt.event.WindowEvent;
13
14import com.jogamp.opengl.GLAutoDrawable;
15import com.jogamp.opengl.GLCapabilities;
16import com.jogamp.opengl.GLEventListener;
17import com.jogamp.opengl.GLProfile;
18import com.jogamp.opengl.awt.GLCanvas;
19import javax.swing.SwingUtilities;
20
21import com.jogamp.newt.awt.NewtCanvasAWT;
22import com.jogamp.newt.opengl.GLWindow;
23import com.jogamp.opengl.test.junit.jogl.demos.es2.LandscapeES2;
24import com.jogamp.opengl.test.junit.util.MiscUtils;
25import com.jogamp.opengl.test.junit.util.UITestCase;
26import com.jogamp.opengl.util.Animator;
27import com.jogamp.opengl.util.AnimatorBase;
28
29/**
30 * Difference to orig. Bug735Inv0AppletAWT:
31 * <pre>
32 * - Use GLEventListener
33 * - Add GLEventListener to GLAutoDrawable
34 * - Use GLAutoDrawable.display() instead of GLAutoDrawable.invoke(true, GLRunnable { init / render })
35 * - Removed MANUAL_FRAME_HANDLING, obsolete due to GLAutoDrawable/GLEventListener
36 * - Use Animator
37 * - Remove applet, component sizes, use frame based size via validate
38 * - Run frame validation/visibility on AWT-EDT
39 * - Add Wait-For-Key after init (perf-test)
40 * </pre>
41 * OSX Results:
42 * <pre>
43 * - Visible content
44 * - Fluent animation
45 * </pre>
46 */
47@SuppressWarnings("serial")
48public class Bug735Inv3AppletAWT extends Applet {
49 static public final int AWT = 0;
50 static public final int NEWT = 1;
51
52 static public final int APPLET_WIDTH = 500;
53 static public final int APPLET_HEIGHT = 290;
54 static public final int TOOLKIT = NEWT;
55 static public final boolean IGNORE_AWT_REPAINT = false;
56 static public boolean USE_ECT = false;
57 static public int SWAP_INTERVAL = 1;
58
59 //////////////////////////////////////////////////////////////////////////////
60
61 static boolean waitForKey = false;
62 static private Frame frame;
63 static private Bug735Inv3AppletAWT applet;
64 private GLCanvas awtCanvas;
65 private GLWindow newtWindow;
66 private GLAutoDrawable glad;
67 private NewtCanvasAWT newtCanvas;
68 private GLEventListener demo;
69 private AnimatorBase animator;
70
71 private int width;
72 private int height;
73
74 public void init() {
75 setSize(APPLET_WIDTH, APPLET_HEIGHT);
76 // JAU setPreferredSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT));
77 width = APPLET_WIDTH;
78 height = APPLET_HEIGHT;
79 initGL();
80 }
81
82 public void start() {
83 initDraw();
84 animator.start();
85 animator.setUpdateFPSFrames(60, System.err);
86 }
87
88 private void initGL() {
89 final GLProfile profile = GLProfile.getDefault();
90 final GLCapabilities caps = new GLCapabilities(profile);
91 caps.setBackgroundOpaque(true);
92 caps.setOnscreen(true);
93 caps.setSampleBuffers(false);
94
95 if (TOOLKIT == AWT) {
96 awtCanvas = new GLCanvas(caps);
97 // JAU awtCanvas.setBounds(0, 0, applet.width, applet.height);
98 awtCanvas.setBackground(new Color(0xFFCCCCCC, true));
99 awtCanvas.setFocusable(true);
100
101 applet.setLayout(new BorderLayout());
102 applet.add(awtCanvas, BorderLayout.CENTER);
103
104 if (IGNORE_AWT_REPAINT) {
105 awtCanvas.setIgnoreRepaint(true);
106 }
107 glad = awtCanvas;
108 } else if (TOOLKIT == NEWT) {
109 newtWindow = GLWindow.create(caps);
110 newtCanvas = new NewtCanvasAWT(newtWindow);
111 // JAU newtCanvas.setBounds(0, 0, applet.width, applet.height);
112 newtCanvas.setBackground(new Color(0xFFCCCCCC, true));
113 newtCanvas.setFocusable(true);
114
115 applet.setLayout(new BorderLayout());
116 applet.add(newtCanvas, BorderLayout.CENTER);
117
118 if (IGNORE_AWT_REPAINT) {
119 newtCanvas.setIgnoreRepaint(true);
120 }
121 glad = newtWindow;
122 }
123
124 demo = new LandscapeES2(SWAP_INTERVAL);
125 // demo = new GearsES2(SWAP_INTERVAL);
126 glad.addGLEventListener(demo);
127 animator = new Animator(glad);
128 animator.setExclusiveContext(USE_ECT);
129 }
130
131 private void initDraw() {
132 if (TOOLKIT == AWT) {
133 // JAU awtCanvas.setVisible(true);
134 if (awtCanvas.getDelegatedDrawable().isRealized()) {
135 // Request the focus here as it cannot work when the window is not visible
136 awtCanvas.requestFocus();
137 }
138 } else if (TOOLKIT == NEWT) {
139 // JAU newtCanvas.setVisible(true);
140 // Force the realization
141 // JAU newtWindow.display();
142 if (newtWindow.isRealized()) {
143 // Request the focus here as it cannot work when the window is not visible
144 newtCanvas.requestFocus();
145 }
146 }
147 }
148
149 static public void main(final String[] args) {
150 for(int i=0; i<args.length; i++) {
151 if(args[i].equals("-vsync")) {
152 i++;
153 SWAP_INTERVAL = MiscUtils.atoi(args[i], SWAP_INTERVAL);
154 } else if(args[i].equals("-exclctx")) {
155 USE_ECT = true;
156 } else if(args[i].equals("-wait")) {
157 waitForKey = true;
158 }
159 }
160 System.err.println("swapInterval "+SWAP_INTERVAL);
161 System.err.println("exclusiveContext "+USE_ECT);
162 if(waitForKey) {
163 UITestCase.waitForKey("Start");
164 }
165
166 final GraphicsEnvironment environment =
167 GraphicsEnvironment.getLocalGraphicsEnvironment();
168 final GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
169
170 frame = new Frame(displayDevice.getDefaultConfiguration());
171 frame.setBackground(new Color(0xCC, 0xCC, 0xCC));
172 frame.setTitle("TestBug735Inv3AppletAWT");
173
174 // This allows to close the frame.
175 frame.addWindowListener(new WindowAdapter() {
176 public void windowClosing(final WindowEvent e) {
177 System.exit(0);
178 }
179 });
180
181 try {
182 final Class<?> c = Thread.currentThread().getContextClassLoader().
183 loadClass(Bug735Inv3AppletAWT.class.getName());
184 applet = (Bug735Inv3AppletAWT) c.newInstance();
185 } catch (final Exception e) {
186 throw new RuntimeException(e);
187 }
188
189 // JAU frame.setLayout(null);
190 frame.add(applet);
191
192 applet.init();
193
194 final Insets insets = frame.getInsets();
195 final int windowW = applet.width + insets.left + insets.right;
196 final int windowH = applet.height + insets.top + insets.bottom;
197
198 try {
199 SwingUtilities.invokeAndWait(new Runnable() {
200 public void run() {
201 frame.setSize(windowW, windowH);
202 frame.validate();
203 // JAU frame.pack();
204 final Rectangle screenRect = displayDevice.getDefaultConfiguration().getBounds();
205 frame.setLocation(screenRect.x + (screenRect.width - applet.width) / 2,
206 screenRect.y + (screenRect.height - applet.height) / 2);
207
208 frame.setResizable(false);
209 frame.setVisible(true);
210 }
211 });
212 } catch (final Exception e) {
213 e.printStackTrace();
214 }
215
216 applet.start();
217 }
218}
void setBackgroundOpaque(final boolean opaque)
Sets whether the surface shall be opaque or translucent.
void setOnscreen(final boolean onscreen)
Sets whether the surface shall be on- or offscreen.
AWT Canvas containing a NEWT Window using native parenting.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
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
Specifies a set of OpenGL capabilities.
void setSampleBuffers(final boolean enable)
Defaults to false.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
final GLDrawable getDelegatedDrawable()
If the implementation uses delegation, return the delegated GLDrawable instance, otherwise return thi...
Definition: GLCanvas.java:1161
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
Base implementation of GLAnimatorControl
final synchronized Thread setExclusiveContext(final Thread t)
Dedicate all GLAutoDrawable's context to the given exclusive context thread.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
boolean start()
Starts this animator, if not running.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
boolean isRealized()
Returns true if this drawable is realized, otherwise false.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.