JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestAnimatorGLJPanel01AWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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 */
28
29package com.jogamp.opengl.test.junit.jogl.acore.anim;
30
31import java.awt.Frame;
32import java.lang.reflect.InvocationTargetException;
33
34import com.jogamp.opengl.GLCapabilities;
35import com.jogamp.opengl.awt.GLJPanel;
36
37import com.jogamp.opengl.util.Animator;
38import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
39import com.jogamp.opengl.test.junit.util.GLTestUtil;
40import com.jogamp.opengl.test.junit.util.UITestCase;
41import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
42
43import org.junit.Assert;
44import org.junit.Test;
45import org.junit.FixMethodOrder;
46import org.junit.runners.MethodSorters;
47
48@FixMethodOrder(MethodSorters.NAME_ASCENDING)
50 static final int width = 400;
51 static final int height = 400;
52
53 protected GLJPanel createGLJPanel(final GLCapabilities caps, final Frame frame, final int x, final int y, final GearsES2 gears) throws InterruptedException {
54 final GLJPanel glCanvas = new GLJPanel(caps);
55 Assert.assertNotNull(glCanvas);
56 glCanvas.addGLEventListener(gears);
57 frame.add(glCanvas);
58 frame.setLocation(x, y);
59 frame.setSize(width, height);
60 frame.setTitle("GLJPanel: "+x+"/"+y);
61 return glCanvas;
62 }
63
64 static void pauseAnimator(final Animator animator, final boolean pause) {
65 if(pause) {
66 animator.pause();
67 Assert.assertEquals(true, animator.isStarted());
68 Assert.assertEquals(true, animator.isPaused());
69 Assert.assertEquals(false, animator.isAnimating());
70 } else {
71 animator.resume();
72 Assert.assertEquals(true, animator.isStarted());
73 Assert.assertEquals(false, animator.isPaused());
74 Assert.assertEquals(true, animator.isAnimating());
75 }
76 }
77 static void stopAnimator(final Animator animator) {
78 animator.stop();
79 Assert.assertEquals(false, animator.isStarted());
80 Assert.assertEquals(false, animator.isPaused());
81 Assert.assertEquals(false, animator.isAnimating());
82 }
83
84 @Test
85 public void test01SyncedOneAnimator() throws InterruptedException, InvocationTargetException {
86 final GLCapabilities caps = new GLCapabilities(null);
87 final Frame f1 = new Frame();
88 final Animator animator = new Animator();
89 animator.start();
90 Assert.assertEquals(true, animator.isStarted());
91 Assert.assertEquals(true, animator.isPaused());
92 Assert.assertEquals(false, animator.isAnimating());
93
94 final GearsES2 g1 = new GearsES2(0);
95 final GLJPanel c1 = createGLJPanel(caps, f1, 0, 0, g1);
96 animator.add(c1);
97 Assert.assertEquals(true, animator.isStarted());
98 Assert.assertEquals(false, animator.isPaused());
99 Assert.assertEquals(true, animator.isAnimating());
100
101 final Frame f2 = new Frame();
102 final GearsES2 g2 = new GearsES2(0);
103 final GLJPanel c2 = createGLJPanel(caps, f2, f1.getX()+width,
104 f1.getY()+0, g2);
105 animator.add(c2);
106
107 final Frame f3 = new Frame();
108 final GearsES2 g3 = new GearsES2(0);
109 final GLJPanel c3 = createGLJPanel(caps, f3, f1.getX()+0,
110 f1.getY()+height, g3);
111 animator.add(c3);
112
113 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
114 @Override
115 public void run() {
116 f1.setVisible(true);
117 f2.setVisible(true);
118 f3.setVisible(true);
119 } } );
120
121 Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, true, null));
122 Assert.assertTrue(AWTRobotUtil.waitForVisible(c1, true, null));
123 Assert.assertTrue(GLTestUtil.waitForContextCreated(c1, true, null));
124 Assert.assertTrue("Gears1 not initialized", g1.waitForInit(true));
125
126 Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, true, null));
127 Assert.assertTrue(AWTRobotUtil.waitForVisible(c2, true, null));
128 Assert.assertTrue(GLTestUtil.waitForContextCreated(c2, true, null));
129 Assert.assertTrue("Gears2 not initialized", g2.waitForInit(true));
130
131 Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, true, null));
132 Assert.assertTrue(AWTRobotUtil.waitForVisible(c3, true, null));
133 Assert.assertTrue(GLTestUtil.waitForContextCreated(c3, true, null));
134 Assert.assertTrue("Gears3 not initialized", g3.waitForInit(true));
135
136 try {
137 Thread.sleep(duration/3);
138 } catch(final Exception e) {
139 e.printStackTrace();
140 }
141
142 pauseAnimator(animator, true);
143
144 try {
145 Thread.sleep(duration/3);
146 } catch(final Exception e) {
147 e.printStackTrace();
148 }
149
150 pauseAnimator(animator, false);
151
152 try {
153 Thread.sleep(duration/3);
154 } catch(final Exception e) {
155 e.printStackTrace();
156 }
157
158 // Stopped animator allows native windowing system 'repaint' event
159 // to trigger GLAD 'display'
160 stopAnimator(animator);
161
162 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
163 @Override
164 public void run() {
165 try {
166 f1.dispose();
167 f2.dispose();
168 f3.dispose();
169 } catch (final Throwable t) {
170 throw new RuntimeException(t);
171 }
172 }});
173
174 Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, false, null));
175 Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, false, null));
176 Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, false, null));
177 }
178
179 @Test
180 public void test02AsyncEachAnimator() throws InterruptedException, InvocationTargetException {
181 final GLCapabilities caps = new GLCapabilities(null);
182 final Frame f1 = new Frame();
183 final Animator a1 = new Animator();
184 final GearsES2 g1 = new GearsES2(0);
185 final GLJPanel c1 = createGLJPanel(caps, f1, 0, 0, g1);
186 a1.add(c1);
187 a1.start();
188 Assert.assertEquals(true, a1.isStarted());
189 Assert.assertEquals(false, a1.isPaused());
190 Assert.assertEquals(true, a1.isAnimating());
191 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
192 @Override
193 public void run() {
194 f1.setVisible(true);
195 } } );
196
197 final Frame f2 = new Frame();
198 final Animator a2 = new Animator();
199 final GearsES2 g2 = new GearsES2(0);
200 final GLJPanel c2 = createGLJPanel(caps, f2, f1.getX()+width, f1.getY()+0, g2);
201 a2.add(c2);
202 a2.start();
203 Assert.assertEquals(true, a2.isStarted());
204 Assert.assertEquals(false, a2.isPaused());
205 Assert.assertEquals(true, a2.isAnimating());
206 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
207 @Override
208 public void run() {
209 f2.setVisible(true);
210 } } );
211
212 final Frame f3 = new Frame();
213 final Animator a3 = new Animator();
214 final GearsES2 g3 = new GearsES2(0);
215 final GLJPanel c3 = createGLJPanel(caps, f3, f1.getX()+0, f1.getY()+height, g3);
216 a3.add(c3);
217 a3.start();
218 Assert.assertEquals(true, a3.isStarted());
219 Assert.assertEquals(false, a3.isPaused());
220 Assert.assertEquals(true, a3.isAnimating());
221 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
222 @Override
223 public void run() {
224 f3.setVisible(true);
225 } } );
226
227 Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, true, null));
228 Assert.assertTrue(AWTRobotUtil.waitForVisible(c1, true, null));
229 Assert.assertTrue(GLTestUtil.waitForContextCreated(c1, true, null));
230 Assert.assertTrue("Gears1 not initialized", g1.waitForInit(true));
231
232 Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, true, null));
233 Assert.assertTrue(AWTRobotUtil.waitForVisible(c2, true, null));
234 Assert.assertTrue(GLTestUtil.waitForContextCreated(c2, true, null));
235 Assert.assertTrue("Gears2 not initialized", g2.waitForInit(true));
236
237 Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, true, null));
238 Assert.assertTrue(AWTRobotUtil.waitForVisible(c3, true, null));
239 Assert.assertTrue(GLTestUtil.waitForContextCreated(c3, true, null));
240 Assert.assertTrue("Gears3 not initialized", g3.waitForInit(true));
241
242 try {
243 Thread.sleep(duration/3);
244 } catch(final Exception e) {
245 e.printStackTrace();
246 }
247
248 pauseAnimator(a1, true);
249 pauseAnimator(a2, true);
250 pauseAnimator(a3, true);
251
252 try {
253 Thread.sleep(duration/3);
254 } catch(final Exception e) {
255 e.printStackTrace();
256 }
257
258 pauseAnimator(a1, false);
259 pauseAnimator(a2, false);
260 pauseAnimator(a3, false);
261
262 try {
263 Thread.sleep(duration/3);
264 } catch(final Exception e) {
265 e.printStackTrace();
266 }
267
268 // Stopped animator allows native windowing system 'repaint' event
269 // to trigger GLAD 'display'
270 stopAnimator(a1);
271 stopAnimator(a2);
272 stopAnimator(a3);
273
274 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
275 @Override
276 public void run() {
277 try {
278 f1.dispose();
279 f2.dispose();
280 f3.dispose();
281 } catch (final Throwable t) {
282 throw new RuntimeException(t);
283 }
284 }});
285
286 Assert.assertTrue(AWTRobotUtil.waitForRealized(c1, false, null));
287 Assert.assertTrue(AWTRobotUtil.waitForRealized(c2, false, null));
288 Assert.assertTrue(AWTRobotUtil.waitForRealized(c3, false, null));
289 }
290
291 static long duration = 3*500; // ms
292
293 public static void main(final String args[]) {
294 for(int i=0; i<args.length; i++) {
295 if(args[i].equals("-time")) {
296 i++;
297 try {
298 duration = Integer.parseInt(args[i]);
299 } catch (final Exception ex) { ex.printStackTrace(); }
300 }
301 }
302 /**
303 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
304 System.err.println("Press enter to continue");
305 System.err.println(stdin.readLine()); */
306 org.junit.runner.JUnitCore.main(TestAnimatorGLJPanel01AWT.class.getName());
307 }
308}
Specifies a set of OpenGL capabilities.
A lightweight Swing component which provides OpenGL rendering support.
Definition: GLJPanel.java:189
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLJPanel.java:989
GLJPanel createGLJPanel(final GLCapabilities caps, final Frame frame, final int x, final int y, final GearsES2 gears)
boolean waitForInit(final boolean initialized)
Definition: GearsES2.java:187
static boolean waitForRealized(final java.awt.Component comp, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final java.awt.Component comp, final boolean visible, final Runnable waitAction)
static boolean waitForContextCreated(final GLAutoDrawable autoDrawable, final boolean created, final Runnable waitAction)
Definition: GLTestUtil.java:42
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
synchronized boolean isStarted()
Indicates whether this animator has been started.
final synchronized boolean pause()
Pauses this animator.
Definition: Animator.java:382
final synchronized boolean resume()
Resumes animation if paused.
Definition: Animator.java:397
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean isPaused()
Indicates whether this animator is started and either manually paused or paused automatically due to ...
Definition: Animator.java:326