JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestTranslucencyAWT.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.caps;
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;
48import com.jogamp.opengl.awt.GLCanvas;
49
50import org.junit.Assert;
51import org.junit.BeforeClass;
52import org.junit.Test;
53import org.junit.FixMethodOrder;
54import org.junit.runners.MethodSorters;
55
56import com.jogamp.common.util.ReflectionUtil;
57import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
58import com.jogamp.opengl.test.junit.util.UITestCase;
59import com.jogamp.opengl.util.Animator;
60
61@FixMethodOrder(MethodSorters.NAME_ASCENDING)
62public class TestTranslucencyAWT extends UITestCase {
63 static Dimension size;
64 static long durationPerTest = 400;
65 static GLCapabilities glCaps;
66
67 @BeforeClass
68 public static void initClass() {
69 size = new Dimension(400,200);
70 glCaps = new GLCapabilities(null);
71 glCaps.setBackgroundOpaque(false);
72 }
73
74 static Frame getTranslucentFrame() {
75 GraphicsConfiguration gc=null;
76 final GraphicsDevice[] devices= GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
77 for (int i = 0; i < devices.length ; i++)
78 {
79 final GraphicsConfiguration[] configs = devices[i].getConfigurations();
80 for (int j = 0; j < configs.length ; j++) {
81 final GraphicsConfiguration config = configs[j];
82 final ColorModel tcm = config.getColorModel(Transparency.TRANSLUCENT);
83 final boolean capable1 = ( null != tcm ) ? tcm.getTransparency() == Transparency.TRANSLUCENT : false;
84 boolean capable2 = false;
85 try {
86 capable2 = ((Boolean)ReflectionUtil.callStaticMethod(
87 "com.sun.awt.AWTUtilities", "isTranslucencyCapable",
88 new Class<?>[] { GraphicsConfiguration.class },
89 new Object[] { config } ,
90 GraphicsConfiguration.class.getClassLoader())).booleanValue();
91 System.err.println("com.sun.awt.AWTUtilities.isTranslucencyCapable(config) passed: "+capable2);
92 } catch (final RuntimeException re) {
93 System.err.println("com.sun.awt.AWTUtilities.isTranslucencyCapable(config) failed: "+re.getMessage());
94 }
95 System.err.println(i+":"+j+" "+config+", "+tcm+", capable "+capable1+"/"+capable2);
96 if(capable1&&capable2) {
97 gc=configs[j];
98 System.err.println("Chosen "+i+":"+j+" "+config+", "+tcm+", capable "+capable1+"/"+capable2);
99 break;
100 }
101 }
102 }
103 final Frame frame = new Frame(gc);
104 if(null!=gc) {
105 frame.setUndecorated(true);
106 frame.setBackground(new Color(0, 0, 0, 0));
107 }
108 frame.setTitle("AWT Parent Frame (opaque: "+(null==gc)+")");
109 return frame;
110 }
111
112 @Test
113 public void test() throws InterruptedException, InvocationTargetException {
114 final Frame frame1 = getTranslucentFrame();
115 final GLCanvas glCanvas= new GLCanvas(glCaps);
116
117 glCanvas.setPreferredSize(size);
118
119 final GLEventListener demo1 = new GearsES2(1);
120 // setDemoFields(demo1, glCanvas, false);
121 glCanvas.addGLEventListener(demo1);
122 final GLAnimatorControl animator1 = new Animator(glCanvas);
123 animator1.start();
124
125 final Container cont1 = new Container();
126 cont1.setLayout(new BorderLayout());
127 cont1.add(glCanvas, BorderLayout.CENTER);
128 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
129 public void run() {
130 cont1.setVisible(true);
131 }});
132
133 frame1.setLayout(new BorderLayout());
134 frame1.add(cont1, BorderLayout.EAST);
135 frame1.add(new Label("center"), BorderLayout.CENTER);
136 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
137 public void run() {
138 frame1.setLocation(0, 0);
139 frame1.setSize((int)size.getWidth(), (int)size.getHeight());
140 frame1.pack();
141 frame1.setVisible(true);
142 }});
143
144 Assert.assertEquals(true, animator1.isAnimating());
145 Assert.assertEquals(false, animator1.isPaused());
146 Assert.assertNotNull(animator1.getThread());
147
148 Thread.sleep(durationPerTest);
149
150 animator1.stop();
151 Assert.assertEquals(false, animator1.isAnimating());
152 Assert.assertEquals(false, animator1.isPaused());
153 Assert.assertEquals(null, animator1.getThread());
154
155 frame1.dispose();
156 }
157
158 static int atoi(final String a) {
159 int i=0;
160 try {
161 i = Integer.parseInt(a);
162 } catch (final Exception ex) { ex.printStackTrace(); }
163 return i;
164 }
165
166 public static void main(final String args[]) throws IOException {
167 for(int i=0; i<args.length; i++) {
168 if(args[i].equals("-time")) {
169 durationPerTest = atoi(args[++i]);
170 }
171 }
172 final String tstname = TestTranslucencyAWT.class.getName();
173 org.junit.runner.JUnitCore.main(tstname);
174 }
175
176}
void setBackgroundOpaque(final boolean opaque)
Sets whether the surface shall be opaque or translucent.
Specifies a set of OpenGL capabilities.
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
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.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.