JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestParenting04AWT.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.newt.parenting;
30
31import java.awt.BorderLayout;
32import java.awt.Button;
33import java.awt.Frame;
34import java.io.IOException;
35import java.lang.reflect.InvocationTargetException;
36
37import com.jogamp.opengl.GLCapabilities;
38import com.jogamp.opengl.GLEventListener;
39import javax.swing.SwingUtilities;
40
41import org.junit.Assert;
42import org.junit.BeforeClass;
43import org.junit.Test;
44import org.junit.FixMethodOrder;
45import org.junit.runners.MethodSorters;
46
47import com.jogamp.newt.Window;
48import com.jogamp.newt.awt.NewtCanvasAWT;
49import com.jogamp.newt.opengl.GLWindow;
50import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
51import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
52import com.jogamp.opengl.test.junit.util.MiscUtils;
53import com.jogamp.opengl.test.junit.util.UITestCase;
54import com.jogamp.opengl.util.Animator;
55
56/**
57 * Using {@link NewtCanvasAWT#setNEWTChild(Window)} for reparenting, i.e. NEWT/AWT hopping
58 */
59@FixMethodOrder(MethodSorters.NAME_ASCENDING)
60public class TestParenting04AWT extends UITestCase {
61 static int width, height;
62 static long durationPerTest = 800;
63 static GLCapabilities glCaps;
64
65 @BeforeClass
66 public static void initClass() {
67 width = 400;
68 height = 400;
69 glCaps = new GLCapabilities(null);
70 }
71
72 @Test
73 public void test01WinHopFrame2FrameDirectHop() throws InterruptedException, InvocationTargetException {
74 // Will produce some artifacts .. resizing etc
75 winHopFrame2Frame(false);
76 }
77
78 @Test
79 public void test02WinHopFrame2FrameDetachFirst() throws InterruptedException, InvocationTargetException {
80 // Note: detaching first setNEWTChild(null) is much cleaner visually
81 winHopFrame2Frame(true);
82 }
83
84 protected void winHopFrame2Frame(final boolean detachFirst) throws InterruptedException, InvocationTargetException {
85 final GLWindow glWindow1 = GLWindow.create(glCaps);
86 final GLEventListener demo1 = new RedSquareES2();
87 setDemoFields(demo1, glWindow1, false);
88 glWindow1.addGLEventListener(demo1);
89 final Animator anim1 = new Animator(glWindow1);
90
91 final GLWindow glWindow2 = GLWindow.create(glCaps);
92 final GLEventListener demo2 = new GearsES2();
93 setDemoFields(demo2, glWindow2, false);
94 glWindow2.addGLEventListener(demo2);
95 final Animator anim2 = new Animator(glWindow2);
96
97 final NewtCanvasAWT canvas1 = new NewtCanvasAWT(glWindow1);
98 final NewtCanvasAWT canvas2 = new NewtCanvasAWT(glWindow2);
99
100 final Frame frame1 = new Frame("AWT Parent Frame");
101 frame1.setLayout(new BorderLayout());
102 frame1.add(new Button("North"), BorderLayout.NORTH);
103 frame1.add(new Button("South"), BorderLayout.SOUTH);
104 frame1.add(new Button("East"), BorderLayout.EAST);
105 frame1.add(new Button("West"), BorderLayout.WEST);
106 SwingUtilities.invokeAndWait(new Runnable() {
107 public void run() {
108 frame1.setSize(width, height);
109 frame1.setLocation(0, 0);
110 frame1.setVisible(true);
111 frame1.validate();
112 }
113 });
114
115 SwingUtilities.invokeAndWait(new Runnable() {
116 public void run() {
117 frame1.add(canvas1, BorderLayout.CENTER);
118 frame1.validate();
119 }
120 });
121 Assert.assertEquals(canvas1.getNativeWindow(),glWindow1.getParent());
122
123 final Frame frame2 = new Frame("AWT Parent Frame");
124 frame2.setLayout(new BorderLayout());
125 frame2.add(new Button("North"), BorderLayout.NORTH);
126 frame2.add(new Button("South"), BorderLayout.SOUTH);
127 frame2.add(new Button("East"), BorderLayout.EAST);
128 frame2.add(new Button("West"), BorderLayout.WEST);
129 SwingUtilities.invokeAndWait(new Runnable() {
130 public void run() {
131 frame2.setSize(width, height);
132 frame2.setLocation(width+50, 0);
133 frame2.setVisible(true);
134 frame2.validate();
135 }
136 });
137
138 SwingUtilities.invokeAndWait(new Runnable() {
139 public void run() {
140 frame2.add(canvas2, BorderLayout.CENTER);
141 frame2.validate();
142 }
143 });
144 Assert.assertEquals(canvas2.getNativeWindow(),glWindow2.getParent());
145
146 anim1.start();
147 anim2.start();
148
149 int state;
150 for(state=0; state<3; state++) {
151 Thread.sleep(durationPerTest);
152 switch(state) {
153 case 0:
154 SwingUtilities.invokeAndWait(new Runnable() {
155 public void run() {
156 // 1 -> 2
157 if(detachFirst) {
158 canvas1.setNEWTChild(null);
159 canvas2.setNEWTChild(null);
160 } else {
161 canvas2.setNEWTChild(null); // free g2 of w2
162 }
163 canvas1.setNEWTChild(glWindow2); // put g2 -> w1. free g1 of w1
164 canvas2.setNEWTChild(glWindow1); // put g1 -> w2
165 frame1.invalidate();
166 frame2.invalidate();
167 frame1.validate();
168 frame2.validate();
169 }
170 });
171 break;
172 case 1:
173 SwingUtilities.invokeAndWait(new Runnable() {
174 public void run() {
175 // 2 -> 1
176 if(detachFirst) {
177 canvas1.setNEWTChild(null);
178 canvas2.setNEWTChild(null);
179 } else {
180 canvas2.setNEWTChild(null);
181 }
182 canvas1.setNEWTChild(glWindow1);
183 canvas2.setNEWTChild(glWindow2);
184 frame1.invalidate();
185 frame2.invalidate();
186 frame1.validate();
187 frame2.validate();
188 }
189 });
190 break;
191 }
192 }
193
194 SwingUtilities.invokeAndWait(new Runnable() {
195 public void run() {
196 frame1.dispose();
197 frame2.dispose();
198 } } );
199 glWindow1.destroy();
200 glWindow2.destroy();
201 Assert.assertEquals(false, glWindow1.isNativeValid());
202 Assert.assertEquals(false, glWindow2.isNativeValid());
203 }
204
205 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
206 Assert.assertNotNull(demo);
207 Assert.assertNotNull(glWindow);
208 final Window window = glWindow.getDelegatedWindow();
209 if(debug) {
210 MiscUtils.setFieldIfExists(demo, "glDebug", true);
211 MiscUtils.setFieldIfExists(demo, "glTrace", true);
212 }
213 if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
214 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
215 }
216 }
217
218 static int atoi(final String a) {
219 int i=0;
220 try {
221 i = Integer.parseInt(a);
222 } catch (final Exception ex) { ex.printStackTrace(); }
223 return i;
224 }
225
226 public static void main(final String args[]) throws IOException {
227 for(int i=0; i<args.length; i++) {
228 if(args[i].equals("-time")) {
229 durationPerTest = atoi(args[++i]);
230 }
231 }
232 final String tstname = TestParenting04AWT.class.getName();
233 org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
234 tstname,
235 "filtertrace=true",
236 "haltOnError=false",
237 "haltOnFailure=false",
238 "showoutput=true",
239 "outputtoformatters=true",
240 "logfailedtests=true",
241 "logtestlistenerevents=true",
242 "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
243 "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
244 }
245
246}
AWT Canvas containing a NEWT Window using native parenting.
Window setNEWTChild(final Window newChild)
Sets a new NEWT child, provoking reparenting.
NativeWindow getNativeWindow()
Returns the associated NativeWindow of this NativeWindowHolder, which is identical to getNativeSurfac...
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final NativeWindow getParent()
Definition: GLWindow.java:282
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final Window getDelegatedWindow()
If the implementation uses delegation, return the delegated Window instance, otherwise return this in...
Definition: GLWindow.java:277
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Specifies a set of OpenGL capabilities.
Using NewtCanvasAWT#setNEWTChild(Window) for reparenting, i.e.
static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug)
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
Definition: MiscUtils.java:193
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
Specifying NEWT's Window functionality:
Definition: Window.java:115
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.