1package com.jogamp.opengl.test.bugs;
3import java.applet.Applet;
4import java.awt.BorderLayout;
6import java.awt.Dimension;
7import java.awt.EventQueue;
9import java.awt.GraphicsDevice;
10import java.awt.GraphicsEnvironment;
11import java.awt.Insets;
12import java.awt.Rectangle;
13import java.awt.event.WindowAdapter;
14import java.awt.event.WindowEvent;
15import java.util.concurrent.CountDownLatch;
16import java.util.concurrent.TimeUnit;
18import com.jogamp.opengl.GLAutoDrawable;
19import com.jogamp.opengl.GLCapabilities;
20import com.jogamp.opengl.GLEventListener;
21import com.jogamp.opengl.GLProfile;
22import com.jogamp.opengl.awt.GLCanvas;
23import com.jogamp.common.os.Clock;
24import com.jogamp.common.util.InterruptSource;
25import com.jogamp.junit.util.JunitTracer;
26import com.jogamp.newt.awt.NewtCanvasAWT;
27import com.jogamp.newt.opengl.GLWindow;
28import com.jogamp.opengl.test.junit.jogl.demos.es2.LandscapeES2;
29import com.jogamp.opengl.test.junit.util.MiscUtils;
30import com.jogamp.opengl.test.junit.util.UITestCase;
46@SuppressWarnings(
"serial")
48 static public final int AWT = 0;
49 static public final int NEWT = 1;
51 static public final int APPLET_WIDTH = 500;
52 static public final int APPLET_HEIGHT = 290;
53 static public final int TARGET_FPS = 120;
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;
61 static boolean waitForKey =
false;
62 static private Frame frame;
72 private Thread thread;
74 private final long frameRatePeriod = 1000000000L / TARGET_FPS;
75 private int frameCount;
79 setSize(APPLET_WIDTH, APPLET_HEIGHT);
80 setPreferredSize(
new Dimension(APPLET_WIDTH, APPLET_HEIGHT));
82 height = APPLET_HEIGHT;
89 thread =
new InterruptSource.Thread(
null,
this,
"Animation Thread");
98 final int NO_DELAYS_PER_YIELD = 15;
99 final int TIMEOUT_SECONDS = 2;
101 long beforeTime = Clock.currentNanos();
102 long overSleepTime = 0L;
105 while (Thread.currentThread() == thread) {
106 if (frameCount == 1) {
107 EventQueue.invokeLater(
new Runnable() {
110 requestFocusInWindow();
117 final CountDownLatch latch =
new CountDownLatch(1);
120 latch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
121 }
catch (
final InterruptedException e) {
125 final long afterTime = Clock.currentNanos();
126 final long timeDiff = afterTime - beforeTime;
127 final long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime;
130 Thread.sleep(sleepTime / 1000000L, (
int) (sleepTime % 1000000L));
132 }
catch (
final InterruptedException ex) { }
133 overSleepTime = (Clock.currentNanos() - afterTime) - sleepTime;
137 if (noDelays > NO_DELAYS_PER_YIELD) {
142 beforeTime = Clock.currentNanos();
154 private void initGL() {
161 if (TOOLKIT == AWT) {
163 awtCanvas.setBounds(0, 0, applet.width, applet.height);
164 awtCanvas.setBackground(
new Color(0xFFCCCCCC,
true));
165 awtCanvas.setFocusable(
true);
167 applet.setLayout(
new BorderLayout());
168 applet.add(awtCanvas, BorderLayout.CENTER);
170 if (IGNORE_AWT_REPAINT) {
171 awtCanvas.setIgnoreRepaint(
true);
174 }
else if (TOOLKIT == NEWT) {
175 newtWindow = GLWindow.
create(caps);
176 newtCanvas =
new NewtCanvasAWT(newtWindow);
177 newtCanvas.setBounds(0, 0, applet.width, applet.height);
178 newtCanvas.setBackground(
new Color(0xFFCCCCCC,
true));
179 newtCanvas.setFocusable(
true);
181 applet.setLayout(
new BorderLayout());
182 applet.add(newtCanvas, BorderLayout.CENTER);
184 if (IGNORE_AWT_REPAINT) {
185 newtCanvas.setIgnoreRepaint(
true);
190 demo =
new LandscapeES2(SWAP_INTERVAL);
194 private void initDraw() {
195 if (TOOLKIT == AWT) {
196 awtCanvas.setVisible(
true);
201 awtCanvas.requestFocus();
203 }
else if (TOOLKIT == NEWT) {
204 newtCanvas.setVisible(
true);
209 newtCanvas.requestFocus();
214 static public void main(
final String[] args) {
215 for(
int i=0; i<args.length; i++) {
216 if(args[i].equals(
"-vsync")) {
219 }
else if(args[i].equals(
"-exclctx")) {
221 }
else if(args[i].equals(
"-wait")) {
225 System.err.println(
"swapInterval "+SWAP_INTERVAL);
226 System.err.println(
"exclusiveContext "+USE_ECT);
228 JunitTracer.waitForKey(
"Start");
231 final GraphicsEnvironment environment =
232 GraphicsEnvironment.getLocalGraphicsEnvironment();
233 final GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
235 frame =
new Frame(displayDevice.getDefaultConfiguration());
236 frame.setBackground(
new Color(0xCC, 0xCC, 0xCC));
237 frame.setTitle(
"TestBug735Inv2AppletAWT");
240 final Class<?> c = Thread.currentThread().getContextClassLoader().
243 }
catch (
final Exception e) {
244 throw new RuntimeException(e);
247 frame.setLayout(
null);
250 frame.setResizable(
false);
254 final Insets insets = frame.getInsets();
255 final int windowW = applet.width + insets.left + insets.right;
256 final int windowH = applet.height + insets.top + insets.bottom;
257 frame.setSize(windowW, windowH);
259 final Rectangle screenRect = displayDevice.getDefaultConfiguration().getBounds();
260 frame.setLocation(screenRect.x + (screenRect.width - applet.width) / 2,
261 screenRect.y + (screenRect.height - applet.height) / 2);
263 final int usableWindowH = windowH - insets.top - insets.bottom;
264 applet.setBounds((windowW - applet.width)/2,
265 insets.top + (usableWindowH - applet.height)/2,
266 applet.width, applet.height);
269 frame.addWindowListener(
new WindowAdapter() {
271 public void windowClosing(
final WindowEvent e) {
276 frame.setVisible(
true);
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,...
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Specifies a set of OpenGL capabilities.
void setSampleBuffers(final boolean enable)
Defaults to false.
Specifies the the OpenGL profile.
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
A heavyweight AWT component which provides OpenGL rendering support.
final GLDrawable getDelegatedDrawable()
If the implementation uses delegation, return the delegated GLDrawable instance, otherwise return thi...
static void main(final String[] args)
void requestDraw(final CountDownLatch latch)
static int atoi(final String str, final int def)
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
Thread setExclusiveContextThread(Thread t)
Dedicates this instance's GLContext to the given thread.
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.