JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestMultipleNewtCanvasAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 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;
30
31import java.awt.Component;
32import java.awt.Dimension;
33import java.io.IOException;
34
35import com.jogamp.opengl.GLAutoDrawable;
36import com.jogamp.opengl.GLCapabilities;
37import com.jogamp.opengl.GLProfile;
38import javax.swing.Box;
39import javax.swing.BoxLayout;
40import javax.swing.JFrame;
41import javax.swing.JPanel;
42import javax.swing.SwingUtilities;
43
44import org.junit.Assert;
45import org.junit.BeforeClass;
46import org.junit.Test;
47import org.junit.FixMethodOrder;
48import org.junit.runners.MethodSorters;
49
50import com.jogamp.newt.awt.NewtCanvasAWT;
51import com.jogamp.newt.opengl.GLWindow;
52import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
53import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
54import com.jogamp.opengl.test.junit.util.UITestCase;
55import com.jogamp.opengl.util.Animator;
56
57
58/**
59 * TestMultipleNewtCanvasAWT
60 */
61@FixMethodOrder(MethodSorters.NAME_ASCENDING)
63
64 static long durationPerTest = 1000;
65
66 @BeforeClass
67 public static void initClass() {
69 setTestSupported(false);
70 }
71 }
72
73 @Test
74 public void test01() throws InterruptedException {
75 testImpl();
76 }
77
78 public void testImpl() throws InterruptedException {
79 final JFrame frame = new JFrame(this.getSimpleTestName("."));
80
81 //
82 // GLDrawableFactory factory = GLDrawableFactory.getFactory(GLProfile.get(GLProfile.GL2));
83 // GLContext sharedContext = factory.getOrCreateSharedContext(factory.getDefaultDevice());
84 //
85 final GLCapabilities glCapabilities = new GLCapabilities(GLProfile.get(GLProfile.GL2));
86 glCapabilities.setSampleBuffers(true);
87 glCapabilities.setNumSamples(4);
88
89 final GearsES2 eventListener1 = new GearsES2(0);
90 final GearsES2 eventListener2 = new GearsES2(1);
91
92 final Component openGLComponent1;
93 final Component openGLComponent2;
94 final GLAutoDrawable openGLAutoDrawable1;
95 final GLAutoDrawable openGLAutoDrawable2;
96
97 final GLWindow glWindow1 = GLWindow.create(glCapabilities);
98 final NewtCanvasAWT newtCanvasAWT1 = new NewtCanvasAWT(glWindow1);
99 newtCanvasAWT1.setPreferredSize(new Dimension(640, 480));
100 glWindow1.addGLEventListener(eventListener1);
101 //
102 final GLWindow glWindow2 = GLWindow.create(glCapabilities);
103 final NewtCanvasAWT newtCanvasAWT2 = new NewtCanvasAWT(glWindow2);
104 newtCanvasAWT2.setPreferredSize(new Dimension(640, 480));
105 glWindow2.addGLEventListener(eventListener2);
106
107 openGLComponent1 = newtCanvasAWT1;
108 openGLComponent2 = newtCanvasAWT2;
109 openGLAutoDrawable1 = glWindow1;
110 openGLAutoDrawable2 = glWindow2;
111
112 // group both OpenGL canvases / windows into a horizontal panel
113 final JPanel openGLPanel = new JPanel();
114 openGLPanel.setLayout(new BoxLayout(openGLPanel, BoxLayout.LINE_AXIS));
115 openGLPanel.add(openGLComponent1);
116 openGLPanel.add(Box.createHorizontalStrut(5));
117 openGLPanel.add(openGLComponent2);
118
119 final JPanel mainPanel = (JPanel) frame.getContentPane();
120 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.LINE_AXIS));
121 mainPanel.add(Box.createHorizontalGlue());
122 mainPanel.add(openGLPanel);
123 mainPanel.add(Box.createHorizontalGlue());
124
125 final Animator animator = new Animator(Thread.currentThread().getThreadGroup());
126 animator.setUpdateFPSFrames(1, null);
127 animator.add(openGLAutoDrawable1);
128 animator.add(openGLAutoDrawable2);
129
130 // make the window visible using the EDT
131 SwingUtilities.invokeLater( new Runnable() {
132 public void run() {
133 frame.pack();
134 frame.setVisible(true);
135 }
136 });
137
138 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
139 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(openGLComponent1, true, null));
140 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(openGLComponent2, true, null));
141
142 animator.start();
143
144 // sleep for test duration, then request the window to close, wait for the window to close,s and stop the animation
145 while(animator.isAnimating() && animator.getTotalFPSDuration() < durationPerTest) {
146 Thread.sleep(100);
147 }
148
149 animator.stop();
150
151 // ask the EDT to dispose of the frame;
152 // if using newt, explicitly dispose of the canvases because otherwise it seems our destroy methods are not called
153 SwingUtilities.invokeLater( new Runnable() {
154 public void run() {
155 newtCanvasAWT1.destroy(); // removeNotify does not destroy GLWindow
156 newtCanvasAWT2.destroy(); // removeNotify does not destroy GLWindow
157 frame.dispose();
158 }
159 });
160 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, false, null));
161 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(openGLComponent1, false, null));
162 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(openGLComponent2, false, null));
163 }
164
165 static int atoi(final String a) {
166 int i=0;
167 try {
168 i = Integer.parseInt(a);
169 } catch (final Exception ex) { ex.printStackTrace(); }
170 return i;
171 }
172
173 public static void main(final String[] args) throws IOException {
174 for(int i=0; i<args.length; i++) {
175 if(args[i].equals("-time")) {
176 if (++i < args.length) {
177 durationPerTest = atoi(args[i]);
178 }
179 }
180 }
181 org.junit.runner.JUnitCore.main(TestMultipleNewtCanvasAWT.class.getName());
182 }
183
184}
185
AWT Canvas containing a NEWT Window using native parenting.
final void destroy()
Destroys this resource:
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
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.
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
void setSampleBuffers(final boolean enable)
Defaults to false.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
static final String GL2ES2
The intersection of the desktop GL3, GL2 and embedded ES2 profile.
Definition: GLProfile.java:594
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 boolean waitForRealized(final java.awt.Component comp, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final java.awt.Component comp, final boolean visible, final Runnable waitAction)
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
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
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.