JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
Bug898AnimatorFromEDTAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 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.opengl.test.junit.jogl.acore.anim;
29
30import java.awt.BorderLayout;
31import java.awt.Dimension;
32
33import com.jogamp.common.util.InterruptSource;
34import com.jogamp.opengl.GLAutoDrawable;
35import com.jogamp.opengl.GLCapabilities;
36import com.jogamp.opengl.GLEventListener;
37import com.jogamp.opengl.GLProfile;
38import com.jogamp.opengl.awt.GLCanvas;
39import javax.swing.SwingUtilities;
40
41import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
42import com.jogamp.opengl.util.Animator;
43
44/**
45 * Manual test case to validate Animator pause/resume on AWT-EDT.
46 * <p>
47 * Even though (AWT) Animator is not able to block until pause/resume is finished
48 * when issued on AWT-EDT, best effort shall be made to preserve functionality.
49 * </p>
50 * Original Author: <i>kosukek</i> from JogAmp forum; Modifier a bit.
51 */
52@SuppressWarnings("serial")
53public class Bug898AnimatorFromEDTAWT extends javax.swing.JFrame {
54
56 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
57 //Layout
58 setMinimumSize(new Dimension(640, 480));
59 getContentPane().setLayout(new BorderLayout());
60 final GLCanvas panel = new GLCanvas(new GLCapabilities(GLProfile.getMaxProgrammable(true)));
61 getContentPane().add(panel, BorderLayout.CENTER);
62 pack();
63 //Animator
64 final Animator animator = new Animator();
65 animator.add(panel);
66 //GLEventListener
67 panel.addGLEventListener(new GearsES2(1));
69 long startTime = 0, lastTime = 0;
70 long step = 1;
71
72 @Override
73 public void init(final GLAutoDrawable glad) {
74 startTime = System.currentTimeMillis();
75 }
76
77 @Override
78 public void dispose(final GLAutoDrawable glad) {
79 }
80
81 @Override
82 public void display(final GLAutoDrawable glad) {
83 final long time = System.currentTimeMillis();
84 if (animator.isAnimating() && step * 2000 < time - startTime) {
85 final long td = time - lastTime;
86 lastTime = time;
87 animator.pause();
88 System.out.println(Thread.currentThread().getName()+": #"+step+" "+td+" ms: animator.pause(): paused "+animator);
89 new InterruptSource.Thread() {
90 public void run() {
91 try {
92 java.lang.Thread.sleep(1000);
93 } catch (final InterruptedException e) {
94 e.printStackTrace();
95 }
96 SwingUtilities.invokeLater(new Runnable() {
97 @Override
98 public void run() {
99 final long td = System.currentTimeMillis() - lastTime;
100 if (animator.isPaused()) {
101 animator.resume(); //Doesn't work on v2.0.2 or higher
102 System.out.println(java.lang.Thread.currentThread().getName()+": #"+step+" "+td+" ms: animator.resume(): animating "+animator);
103 } else {
104 System.out.println(java.lang.Thread.currentThread().getName()+": #"+step+" "+td+" ms: animator.resume(): Ooops - not paused! - animating "+animator);
105 }
106 } } );
107 }
108 }.start();
109 step++;
110 }
111 }
112
113 @Override
114 public void reshape(final GLAutoDrawable glad, final int i, final int i1, final int i2, final int i3) {
115 }
116 });
117 //Start animation
118 animator.start();
119 System.out.println("animator.start()");
120 }
121
122 public static void main(final String args[]) {
123 java.awt.EventQueue.invokeLater(new Runnable() {
124 @Override
125 public void run() {
126 new Bug898AnimatorFromEDTAWT().setVisible(true);
127 }
128 });
129 }
130}
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getMaxProgrammable(final AbstractGraphicsDevice device, final boolean favorHardwareRasterizer)
Returns the highest profile, implementing the programmable shader pipeline.
Definition: GLProfile.java:831
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
Manual test case to validate Animator pause/resume on AWT-EDT.
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
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
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.