JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestTranslucentParentingAWT.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.Color;
33import java.awt.Container;
34import java.awt.Dimension;
35import java.awt.Frame;
36import java.awt.GraphicsConfiguration;
37import java.awt.GraphicsDevice;
38import java.awt.GraphicsEnvironment;
39import java.awt.Label;
40import java.awt.Transparency;
41import java.awt.image.ColorModel;
42import java.io.IOException;
43import java.lang.reflect.InvocationTargetException;
44
45import com.jogamp.opengl.GLAnimatorControl;
46import com.jogamp.opengl.GLCapabilities;
47import com.jogamp.opengl.GLEventListener;
48
49import org.junit.Assert;
50import org.junit.BeforeClass;
51import org.junit.Test;
52import org.junit.FixMethodOrder;
53import org.junit.runners.MethodSorters;
54
55import com.jogamp.common.util.ReflectionUtil;
56import com.jogamp.newt.Window;
57import com.jogamp.newt.awt.NewtCanvasAWT;
58import com.jogamp.newt.opengl.GLWindow;
59import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
60import com.jogamp.opengl.test.junit.util.MiscUtils;
61import com.jogamp.opengl.test.junit.util.UITestCase;
62import com.jogamp.opengl.util.Animator;
63
64@FixMethodOrder(MethodSorters.NAME_ASCENDING)
66 static Dimension size;
67 static long durationPerTest = 400;
68 static long waitAdd2nd = 200;
69 static GLCapabilities glCaps;
70
71 @BeforeClass
72 public static void initClass() {
73 size = new Dimension(400,200);
74 glCaps = new GLCapabilities(null);
75 glCaps.setBackgroundOpaque(false);
76 }
77
78 @Test
79 public void testWindowParenting1AWTOneNewtChild01() throws InterruptedException, InvocationTargetException {
80 testWindowParenting1AWTOneNewtChild();
81 }
82
83 static Frame getTranslucentFrame() {
84 GraphicsConfiguration gc=null;
85 final GraphicsDevice[] devices= GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
86 for (int i = 0; i < devices.length ; i++)
87 {
88 final GraphicsConfiguration[] configs = devices[i].getConfigurations();
89 for (int j = 0; j < configs.length ; j++) {
90 final GraphicsConfiguration config = configs[j];
91 final ColorModel tcm = config.getColorModel(Transparency.TRANSLUCENT);
92 final boolean capable1 = ( null != tcm ) ? tcm.getTransparency() == Transparency.TRANSLUCENT : false;
93 boolean capable2 = false;
94 try {
95 capable2 = ((Boolean)ReflectionUtil.callStaticMethod(
96 "com.sun.awt.AWTUtilities", "isTranslucencyCapable",
97 new Class<?>[] { GraphicsConfiguration.class },
98 new Object[] { config } ,
99 GraphicsConfiguration.class.getClassLoader())).booleanValue();
100 System.err.println("com.sun.awt.AWTUtilities.isTranslucencyCapable(config) passed: "+capable2);
101 } catch (final RuntimeException re) {
102 System.err.println("com.sun.awt.AWTUtilities.isTranslucencyCapable(config) failed: "+re.getMessage());
103 }
104 System.err.println(i+":"+j+" "+config+", "+tcm+", capable "+capable1+"/"+capable2);
105 if(capable1&&capable2) {
106 gc=configs[j];
107 System.err.println("Chosen "+i+":"+j+" "+config+", "+tcm+", capable "+capable1+"/"+capable2);
108 break;
109 }
110 }
111 }
112 final Frame frame = new Frame(gc);
113 if(null!=gc) {
114 frame.setUndecorated(true);
115 frame.setBackground(new Color(0, 0, 0, 0));
116 }
117 frame.setTitle("AWT Parent Frame (opaque: "+(null==gc)+")");
118 return frame;
119 }
120
121 public void testWindowParenting1AWTOneNewtChild() throws InterruptedException, InvocationTargetException {
122 final Frame frame1 = getTranslucentFrame();
123 final GLWindow glWindow1 = GLWindow.create(glCaps);
124 glWindow1.setUpdateFPSFrames(1, null);
125 glWindow1.setUndecorated(true);
126 final NewtCanvasAWT newtCanvasAWT1 = new NewtCanvasAWT(frame1.getGraphicsConfiguration(), glWindow1);
127 newtCanvasAWT1.setPreferredSize(size);
128
129 final GLEventListener demo1 = new GearsES2(1);
130 setDemoFields(demo1, glWindow1, false);
131 glWindow1.addGLEventListener(demo1);
132 final GLAnimatorControl animator1 = new Animator(glWindow1);
133 animator1.start();
134
135 final Container cont1 = new Container();
136 cont1.setLayout(new BorderLayout());
137 cont1.add(newtCanvasAWT1, BorderLayout.CENTER);
138 cont1.setVisible(true);
139
140 frame1.setLayout(new BorderLayout());
141 frame1.add(cont1, BorderLayout.EAST);
142 frame1.add(new Label("center"), BorderLayout.CENTER);
143 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
144 public void run() {
145 frame1.setLocation(0, 0);
146 frame1.setSize((int)size.getWidth(), (int)size.getHeight());
147 frame1.pack();
148 frame1.setVisible(true);
149 }});
150
151 Assert.assertEquals(newtCanvasAWT1.getNativeWindow(),glWindow1.getParent());
152 Assert.assertEquals(true, animator1.isAnimating());
153 Assert.assertEquals(false, animator1.isPaused());
154 Assert.assertNotNull(animator1.getThread());
155
156 Thread.sleep(durationPerTest);
157
158 animator1.stop();
159 Assert.assertEquals(false, animator1.isAnimating());
160 Assert.assertEquals(false, animator1.isPaused());
161 Assert.assertEquals(null, animator1.getThread());
162
163 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
164 public void run() {
165 frame1.dispose();
166 } } );
167 glWindow1.destroy();
168 }
169
170 public static void setDemoFields(final GLEventListener demo, final GLWindow glWindow, final boolean debug) {
171 Assert.assertNotNull(demo);
172 Assert.assertNotNull(glWindow);
173 final Window window = glWindow.getDelegatedWindow();
174 if(debug) {
175 MiscUtils.setFieldIfExists(demo, "glDebug", true);
176 MiscUtils.setFieldIfExists(demo, "glTrace", true);
177 }
178 if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
179 MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
180 }
181 }
182
183 public static void main(final String args[]) throws IOException {
184 for(int i=0; i<args.length; i++) {
185 if(args[i].equals("-time")) {
186 durationPerTest = MiscUtils.atol(args[++i], durationPerTest);
187 } else if(args[i].equals("-wait")) {
188 waitAdd2nd = MiscUtils.atol(args[++i], waitAdd2nd);
189 }
190 }
191 final String tstname = TestTranslucentParentingAWT.class.getName();
192 /*
193 org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
194 tstname,
195 "filtertrace=true",
196 "haltOnError=false",
197 "haltOnFailure=false",
198 "showoutput=true",
199 "outputtoformatters=true",
200 "logfailedtests=true",
201 "logtestlistenerevents=true",
202 "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
203 "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); */
204 org.junit.runner.JUnitCore.main(tstname);
205 }
206
207}
void setBackgroundOpaque(final boolean opaque)
Sets whether the surface shall be opaque or translucent.
AWT Canvas containing a NEWT Window using native parenting.
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 setUndecorated(final boolean value)
Definition: GLWindow.java:337
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.
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
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
Specifying NEWT's Window functionality:
Definition: Window.java:115
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 isPaused()
Indicates whether this animator is started and either manually paused or paused automatically due to ...
boolean isAnimating()
Indicates whether this animator is started and is not paused.
boolean stop()
Stops this animator.
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.