JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestAWT03GLJPanelRecreate01.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.awt;
30
31import com.jogamp.opengl.GLProfile;
32import com.jogamp.opengl.awt.GLJPanel;
33import javax.swing.JFrame;
34import javax.swing.JLabel;
35
36import com.jogamp.opengl.util.Animator;
37import com.jogamp.opengl.test.junit.util.UITestCase;
38import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
39import com.jogamp.opengl.test.junit.util.MiscUtils;
40
41import java.awt.BorderLayout;
42import java.awt.Dimension;
43
44import jogamp.nativewindow.SurfaceScaleUtils;
45
46import org.junit.Assert;
47import org.junit.Assume;
48import org.junit.Before;
49import org.junit.BeforeClass;
50import org.junit.After;
51import org.junit.Test;
52import org.junit.FixMethodOrder;
53import org.junit.runners.MethodSorters;
54
55
56@FixMethodOrder(MethodSorters.NAME_ASCENDING)
58 static long durationPerTest = 500; // ms
59
60 final static int sizeEps = 64;
61 final static Dimension size1 = new Dimension(512, 512-sizeEps-1);
62 final static Dimension size2 = new Dimension(512+sizeEps+1+256, 512+256);
63 final static Dimension size3 = new Dimension(512-256, 512-sizeEps-1-256);
64
65 JFrame frame1=null;
66 JFrame frame2=null;
67 JFrame frame3=null;
68 GLJPanel glComp=null;
69 JLabel label1 = null;
70 JLabel label2 = null;
71 JLabel label3 = null;
72 Animator animator = null;
73
74 @BeforeClass
75 public static void startup() {
76 System.out.println("GLProfile "+GLProfile.glAvailabilityToString());
77 }
78
79 @Before
80 public void init() {
81 glComp = new GLJPanel();
82 Assert.assertNotNull(glComp);
83 glComp.addGLEventListener(new GearsES2());
84
85 animator = new Animator(glComp);
86 animator.start();
87
88 label1 = new JLabel("L1 - No GLJPanel");
89 label1.setMinimumSize(size1);
90 label1.setPreferredSize(size1);
91 frame1 = new JFrame("Frame 1");
92 Assert.assertNotNull(frame1);
93 frame1.add(label1);
94 frame1.setLocation(0, 0);
95
96 label2 = new JLabel("L2 - No GLJPanel");
97 label2.setMinimumSize(size2);
98 label2.setPreferredSize(size2);
99 frame2 = new JFrame("Frame 2");
100 Assert.assertNotNull(frame2);
101 frame2.add(label2);
102 frame2.setLocation(size1.width, 0);
103
104 label3 = new JLabel("L3 - No GLJPanel");
105 label3.setMinimumSize(size3);
106 label3.setPreferredSize(size3);
107 frame3 = new JFrame("Frame 3");
108 Assert.assertNotNull(frame3);
109 frame3.add(label3);
110 frame3.setLocation(0, size1.height);
111 }
112
113 @After
114 public void release() {
115 Assert.assertNotNull(frame1);
116 Assert.assertNotNull(frame2);
117 Assert.assertNotNull(glComp);
118 try {
119 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
120 public void run() {
121 glComp.destroy();
122 frame1.dispose();
123 frame2.dispose();
124 frame3.dispose();
125 }});
126 } catch (final Throwable t) {
127 t.printStackTrace();
128 Assume.assumeNoException(t);
129 }
130 frame1=null;
131 frame2=null;
132 frame3=null;
133 glComp=null;
134
135 animator.stop();
136 animator=null;
137 }
138
139 private void addCanvas(final JFrame frame, final JLabel label, final Dimension size) {
140 try {
141 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
142 public void run() {
143 frame.getContentPane().remove(label);
144 glComp.setPreferredSize(size);
145 glComp.setMinimumSize(size);
146 frame.getContentPane().add(glComp, BorderLayout.CENTER);
147 frame.pack();
148 }});
149 } catch (final Throwable t) {
150 t.printStackTrace();
151 Assume.assumeNoException(t);
152 }
153 }
154
155 private void removeCanvas(final JFrame frame, final JLabel label) {
156 try {
157 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
158 public void run() {
159 frame.getContentPane().remove(glComp);
160 frame.getContentPane().add(label);
161 frame.pack();
162 frame.repaint();
163 }});
164 } catch (final Throwable t) {
165 t.printStackTrace();
166 Assume.assumeNoException(t);
167 }
168 }
169
170 private void setVisible(final JFrame frame, final boolean v) {
171 try {
172 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
173 public void run() {
174 frame.pack();
175 frame.setVisible(v);
176 }});
177 } catch (final Throwable t) {
178 t.printStackTrace();
179 Assume.assumeNoException(t);
180 }
181 }
182
183 private void assertSize(final Dimension expSize) {
184 final float[] scale = { 1f, 1f };
185 glComp.getMaximumSurfaceScale(scale);
186
187 final Dimension hasSize = glComp.getSize(null);
188
189 Assert.assertTrue("AWT Size.width mismatch: expected "+expSize+", has "+hasSize,
190 Math.abs(expSize.width-hasSize.width) <= sizeEps);
191 Assert.assertTrue("AWT Size.height mismatch: expected "+expSize+", has "+hasSize,
192 Math.abs(expSize.height-hasSize.height) <= sizeEps);
193
194 final int expSurfWidth = SurfaceScaleUtils.scale(expSize.width, scale[0]);
195 final int expSurfHeight = SurfaceScaleUtils.scale(expSize.height, scale[0]);
196 final int hasSurfWidth = glComp.getSurfaceWidth();
197 final int hasSurfHeight = glComp.getSurfaceHeight();
198
199 Assert.assertTrue("GL Size.width mismatch: expected "+expSurfWidth+", has "+hasSurfWidth,
200 Math.abs(expSurfWidth-hasSurfWidth) <= sizeEps);
201 Assert.assertTrue("GL Size.height mismatch: expected "+expSurfHeight+", has "+hasSurfHeight,
202 Math.abs(expSurfHeight-hasSurfHeight) <= sizeEps);
203 }
204
205 @Test
206 public void testAddRemove3Times() throws InterruptedException {
207 setVisible(frame1, true);
208 setVisible(frame2, true);
209 setVisible(frame3, true);
210
211 // Init Frame 1
212 addCanvas(frame1, label1, size1);
213 Thread.sleep(durationPerTest);
214 assertSize(size1);
215
216 // Frame 1 -> Frame 2
217 removeCanvas(frame1, label1);
218 addCanvas(frame2, label2, size2);
219 Thread.sleep(durationPerTest);
220 assertSize(size2);
221
222 // Frame 2 -> Frame 3
223 removeCanvas(frame2, label2);
224 addCanvas(frame3, label3, size3);
225 Thread.sleep(durationPerTest);
226 assertSize(size3);
227
228 // Frame 3 -> Frame 1
229 removeCanvas(frame3, label3);
230 addCanvas(frame1, label1, size1);
231 Thread.sleep(durationPerTest);
232 assertSize(size1);
233 }
234
235 public static void main(final String args[]) {
236 for(int i=0; i<args.length; i++) {
237 if(args[i].equals("-time")) {
238 durationPerTest = MiscUtils.atoi(args[++i], (int)durationPerTest);
239 }
240 }
241 org.junit.runner.JUnitCore.main(TestAWT03GLJPanelRecreate01.class.getName());
242 }
243}
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static String glAvailabilityToString(final AbstractGraphicsDevice device)
Definition: GLProfile.java:333
A lightweight Swing component which provides OpenGL rendering support.
Definition: GLJPanel.java:189
void destroy()
Just an alias for removeNotify.
Definition: GLJPanel.java:531
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLJPanel.java:1217
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLJPanel.java:1222
float[] getMaximumSurfaceScale(final float[] result)
Returns the maximum pixel scale of the associated NativeSurface.The maximum pixel scale maybe used to...
Definition: GLJPanel.java:678
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLJPanel.java:989
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368