JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestAWTCardLayoutAnimatorStartStopBug532.java
Go to the documentation of this file.
1package com.jogamp.opengl.test.junit.jogl.acore.anim;
2
3import java.awt.BorderLayout;
4import java.awt.CardLayout;
5import java.awt.Component;
6import java.awt.Dimension;
7import java.awt.event.ItemEvent;
8import java.awt.event.ItemListener;
9import java.awt.event.WindowAdapter;
10import java.awt.event.WindowEvent;
11import java.lang.reflect.InvocationTargetException;
12
13import com.jogamp.opengl.GLAnimatorControl;
14import com.jogamp.opengl.GLCapabilities;
15import com.jogamp.opengl.GLProfile;
16import com.jogamp.opengl.awt.GLCanvas;
17import javax.swing.JComboBox;
18import javax.swing.JFrame;
19import javax.swing.JLabel;
20import javax.swing.JPanel;
21import javax.swing.WindowConstants;
22
23import org.junit.Test;
24import org.junit.FixMethodOrder;
25import org.junit.runners.MethodSorters;
26
27import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
28import com.jogamp.opengl.test.junit.util.MiscUtils;
29import com.jogamp.opengl.test.junit.util.UITestCase;
30import com.jogamp.opengl.util.Animator;
31import com.jogamp.opengl.util.FPSAnimator;
32
33@FixMethodOrder(MethodSorters.NAME_ASCENDING)
34@SuppressWarnings({ "unchecked", "rawtypes" })
36 static final String LABEL = "Label";
37 static final String CANVAS = "GLCanvas";
38
40 StartStop, PauseResume, Continue;
41 }
42
43 static long durationPerTest = 200*4; // ms
44 static boolean manual = false;
45 static volatile boolean shouldStop = false;
46
47 private String selected = LABEL;
48
49 @Test
50 public void testFPSAnimatorStartStop() throws InterruptedException, InvocationTargetException {
51 testImpl(AnimatorControlBehavior.StartStop, true);
52 }
53
54 @Test
55 public void testFPSAnimatorResumePause() throws InterruptedException, InvocationTargetException {
57 }
58
59 @Test
60 public void testFPSAnimatorContinue() throws InterruptedException, InvocationTargetException {
61 testImpl(AnimatorControlBehavior.Continue, true);
62 }
63
64 @Test
65 public void testAnimatorStartStop() throws InterruptedException, InvocationTargetException {
66 testImpl(AnimatorControlBehavior.StartStop, false);
67 }
68
69 @Test
70 public void testAnimatorResumePause() throws InterruptedException, InvocationTargetException {
71 testImpl(AnimatorControlBehavior.PauseResume, false);
72 }
73
74 @Test
75 public void testAnimatorContinue() throws InterruptedException, InvocationTargetException {
76 testImpl(AnimatorControlBehavior.Continue, false);
77 }
78
79 private static String id(final Object obj) { return "0x" + ( null!=obj ? Integer.toHexString(obj.hashCode()) : "nil" ); }
80 private static String str(final Component c) {
81 return id(c)+": "+c.getClass().getSimpleName()+"[visible "+c.isVisible()+", showing "+c.isShowing()+", valid "+c.isValid()+
82 ", displayable "+c.isDisplayable()+", "+c.getX()+"/"+c.getY()+" "+c.getWidth()+"x"+c.getHeight()+"]";
83 }
84 void testImpl(final AnimatorControlBehavior animCtrl, final boolean useFPSAnimator) throws InterruptedException, InvocationTargetException {
85 final GLProfile glp = GLProfile.get(GLProfile.GL2);
86 final GLCapabilities caps = new GLCapabilities(glp);
87 final GLCanvas canvas = new GLCanvas(caps);
88 canvas.setPreferredSize(new Dimension(640, 480));
89
90 final GLAnimatorControl animatorCtrl = useFPSAnimator ? new FPSAnimator(canvas, 60) : new Animator(canvas);
91 animatorCtrl.setUpdateFPSFrames(60, null);// System.err);
92 switch (animCtrl) {
93 case PauseResume:
94 animatorCtrl.start();
95 animatorCtrl.pause();
96 break;
97 case Continue:
98 animatorCtrl.start();
99 break;
100 default:
101 }
102
103 canvas.addGLEventListener(new GearsES2(1));
104 /* if(Platform.OS_TYPE == Platform.OSType.WINDOWS) {
105 canvas.addGLEventListener(new GLEventListener() {
106 public void init(GLAutoDrawable drawable) { }
107 public void dispose(GLAutoDrawable drawable) { }
108 public void display(GLAutoDrawable drawable) {
109 final NativeWindow win = (NativeWindow) drawable.getNativeSurface();
110 long hdc = win.getSurfaceHandle();
111 long hdw = win.getWindowHandle();
112 long hdw_hdc = GDI.WindowFromDC(hdc);
113 System.err.println("*** hdc 0x"+Long.toHexString(hdc)+", hdw(hdc) 0x"+Long.toHexString(hdw_hdc)+", hdw 0x"+Long.toHexString(hdw) + " - " + Thread.currentThread().getName() + ", " + animatorCtrl);
114 // System.err.println(drawable.getNativeSurface().toString());
115 }
116 public void reshape(GLAutoDrawable drawable, int x, int y, int width,
117 int height) { }
118 });
119 } */
120
121 final JFrame frame = new JFrame();
122 frame.setTitle(getSimpleTestName(" - "));
123 frame.addWindowListener(new WindowAdapter() {
124 @Override
125 public void windowClosing(final WindowEvent e) {
126 animatorCtrl.stop();
127 shouldStop = true;
128 }
129 });
130 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
131
132 final JLabel label = new JLabel("A label to cover the canvas");
133
134 final JPanel cards = new JPanel(new CardLayout());
135 final JPanel comboBoxPanel = new JPanel(); // nicer look ..
136 final JComboBox comboBox = new JComboBox(new String[] { LABEL, CANVAS });
137 comboBox.setEditable(false);
138 comboBox.addItemListener(new ItemListener() {
139 @Override
140 public void itemStateChanged(final ItemEvent evt) {
141 final CardLayout cl = (CardLayout)(cards.getLayout());
142 final String newSelection = (String)evt.getItem();
143 if(!newSelection.equals(selected)) {
144 final String oldSelected = selected;
145 if(newSelection.equals(CANVAS)) {
146 System.err.println("XXX Card.SHOW Canvas PRE: ");
147 System.err.println(" CANVAS "+str(canvas));
148 System.err.println(" LABEL "+str(label));
149 cl.show(cards, CANVAS);
150 System.err.println("XXX Card.SHOW Canvas POST: ");
151 System.err.println(" CANVAS "+str(canvas));
152 System.err.println(" LABEL "+str(label));
153 switch (animCtrl) {
154 case StartStop:
155 animatorCtrl.start();
156 break;
157 case PauseResume:
158 animatorCtrl.resume();
159 break;
160 default:
161 }
162 selected = CANVAS;
163 } else if(newSelection.equals(LABEL)) {
164 switch (animCtrl) {
165 case StartStop:
166 animatorCtrl.stop();
167 break;
168 case PauseResume:
169 animatorCtrl.pause();
170 break;
171 default:
172 }
173 System.err.println("XXX Card.SHOW Label PRE: ");
174 System.err.println(" CANVAS "+str(canvas));
175 System.err.println(" LABEL "+str(label));
176 cl.show(cards, LABEL);
177 System.err.println("XXX Card.SHOW Label POST: ");
178 System.err.println(" CANVAS "+str(canvas));
179 System.err.println(" LABEL "+str(label));
180 selected = LABEL;
181 } else {
182 throw new RuntimeException("oops .. unexpected item: "+evt);
183 }
184 System.err.println("Item Change: "+oldSelected+" -> "+selected+", "+animatorCtrl);
185 } else {
186 System.err.println("Item Stays: "+selected+", "+animatorCtrl);
187 }
188 }
189 });
190 comboBoxPanel.add(comboBox);
191
192 cards.add(label, LABEL);
193 cards.add(canvas, CANVAS);
194
195 frame.add(comboBoxPanel, BorderLayout.PAGE_START);
196 frame.add(cards, BorderLayout.CENTER);
197
198 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
199 @Override
200 public void run() {
201 frame.pack();
202 frame.setVisible(true);
203 }});
204
205 if(manual) {
206 for(long w=durationPerTest; !shouldStop && w>0; w-=100) {
207 Thread.sleep(100);
208 }
209 } else {
210 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
211 @Override
212 public void run() {
213 comboBox.setSelectedItem(LABEL);
214 }});
215 Thread.sleep(durationPerTest/4);
216
217 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
218 @Override
219 public void run() {
220 comboBox.setSelectedItem(CANVAS);
221 }});
222 Thread.sleep(durationPerTest/4);
223
224 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
225 @Override
226 public void run() {
227 comboBox.setSelectedItem(LABEL);
228 }});
229 Thread.sleep(durationPerTest/4);
230
231 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
232 @Override
233 public void run() {
234 comboBox.setSelectedItem(CANVAS);
235 }});
236 Thread.sleep(durationPerTest/4);
237 }
238
239 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
240 @Override
241 public void run() {
242 frame.setVisible(false);
243 frame.dispose();
244 }});
245
246 }
247
248 public static void main(final String args[]) {
249 for(int i=0; i<args.length; i++) {
250 if(args[i].equals("-time")) {
251 durationPerTest = MiscUtils.atoi(args[++i], (int)durationPerTest);
252 } else if(args[i].equals("-manual")) {
253 manual = true;
254 }
255 }
256 org.junit.runner.JUnitCore.main(TestAWTCardLayoutAnimatorStartStopBug532.class.getName());
257 }
258}
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
Definition: GLProfile.java:579
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
void setUpdateFPSFrames(int frames, PrintStream out)
An animator control interface, which implementation may drive a com.jogamp.opengl....
boolean start()
Starts this animator, if not running.
boolean resume()
Resumes animation if paused.
boolean stop()
Stops this animator.
boolean pause()
Pauses this animator.