JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLJPanelReadd01Bug1310AWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2013-2023 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 */
28package com.jogamp.opengl.test.junit.jogl.awt;
29
30import java.awt.BorderLayout;
31import java.awt.Container;
32import java.awt.Dimension;
33import java.awt.GridLayout;
34import java.awt.event.ActionEvent;
35import java.lang.reflect.InvocationTargetException;
36import java.util.Arrays;
37
38import com.jogamp.opengl.GL;
39import com.jogamp.opengl.GL2;
40import com.jogamp.opengl.GLAutoDrawable;
41import com.jogamp.opengl.GLCapabilities;
42import com.jogamp.opengl.GLCapabilitiesImmutable;
43import com.jogamp.opengl.GLEventListener;
44import com.jogamp.opengl.GLProfile;
45import com.jogamp.opengl.awt.GLJPanel;
46
47import javax.swing.AbstractAction;
48import javax.swing.JFrame;
49import javax.swing.JPanel;
50import javax.swing.JToolBar;
51import javax.swing.SwingUtilities;
52import javax.swing.WindowConstants;
53
54import org.junit.Assert;
55import org.junit.Assume;
56import org.junit.BeforeClass;
57import org.junit.FixMethodOrder;
58import org.junit.Test;
59import org.junit.runners.MethodSorters;
60
61import com.jogamp.opengl.test.junit.util.MiscUtils;
62import com.jogamp.opengl.test.junit.util.UITestCase;
63import com.jogamp.opengl.util.FPSAnimator;
64import com.jogamp.opengl.util.GLReadBufferUtil;
65import com.jogamp.opengl.util.texture.TextureIO;
66
67/**
68 * Remove and re-add a GLJPanel from its Swing parent
69 */
70@FixMethodOrder(MethodSorters.NAME_ASCENDING)
72
73 @BeforeClass
74 public static void initClass() {
76 }
77
78 static final Dimension gljPanelSize = new Dimension(800, 600);
79 static GLCapabilitiesImmutable caps = null;
80 static long duration = 500; // ms
81
82 public void test(final GLCapabilitiesImmutable caps, final GLEventListener demo) {
83 final JFrame[] frame = { null };
84 final JPanel[] container = { null };
85 final GLJPanel[] glJPanel = { null };
86 final FPSAnimator animator = new FPSAnimator(60);
87
88 try {
89 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
90 @SuppressWarnings("serial")
91 @Override
92 public void run() {
93 final JFrame _frame = new JFrame("Testing");
94 frame[0] = _frame;
95 _frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
96 final Container content = _frame.getContentPane();
97 content.setLayout(new BorderLayout());
98
99 final JToolBar toolbar = new JToolBar();
100
101 final JPanel _container = new JPanel();
102 container[0] = _container;
103 _container.setLayout(new GridLayout(1, 1));
104 final GLJPanel _glJPanel = new GLJPanel(caps);
105 glJPanel[0] = _glJPanel;
106 _glJPanel.addGLEventListener(demo);
107 _glJPanel.setPreferredSize(gljPanelSize);
108 _container.add(_glJPanel);
109 animator.add(_glJPanel);
110
111 toolbar.add(new AbstractAction("Remove and add") {
112 @Override
113 public void actionPerformed(final ActionEvent e) {
114 System.err.println("XXX: Remove");
115 _container.removeAll();
116 System.err.println("XXX: ReAdd.0: glJPanel-Size: "+glJPanel[0].getSize());
117 _container.add(_glJPanel);
118 _glJPanel.invalidate();
119 _glJPanel.repaint();
120 System.err.println("XXX: ReAdd.X: glJPanel-Size: "+glJPanel[0].getSize());
121 }
122 });
123
124 content.add(toolbar, BorderLayout.NORTH);
125 content.add(_container, BorderLayout.CENTER);
126
127 _frame.pack();
128 _frame.setLocationRelativeTo(null);
129 _frame.setVisible(true);
130 } } );
131 } catch( final Throwable throwable ) {
132 throwable.printStackTrace();
133 Assume.assumeNoException( throwable );
134 }
135 animator.start();
136
137 try {
138 Thread.sleep(500);
139 } catch (final InterruptedException e1) {
140 e1.printStackTrace();
141 }
142 try {
143 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
144 @Override
145 public void run() {
146 System.err.println("XXX: Remove");
147 container[0].removeAll();
148 System.err.println("XXX: ReAdd.0: glJPanel-Size: "+glJPanel[0].getSize());
149 container[0].add(glJPanel[0]);
150 glJPanel[0].invalidate();
151 glJPanel[0].repaint();
152 System.err.println("XXX: ReAdd.X: glJPanel-Size: "+glJPanel[0].getSize());
153 }
154 });
155 } catch( final Throwable throwable ) {
156 throwable.printStackTrace();
157 Assume.assumeNoException( throwable );
158 }
159 try {
160 Thread.sleep(duration);
161 } catch (final InterruptedException e1) {
162 e1.printStackTrace();
163 }
164 try {
165 SwingUtilities.invokeAndWait(new Runnable() {
166 @Override
167 public void run() {
168 frame[0].dispose();
169 } } );
170 } catch (final Exception e1) {
171 e1.printStackTrace();
172 }
173 }
174
175 @Test
176 public void test00() throws InterruptedException, InvocationTargetException {
177 // test(new GLCapabilities(null), new RedSquareES2());
178 test(new GLCapabilities(null), new MyRotTriangle());
179 System.err.println("Exp GL_Viewport: "+Arrays.toString(exp_gl_viewport));
180 System.err.println("Has GL_Viewport: "+Arrays.toString(has_gl_viewport));
181 Assert.assertArrayEquals(exp_gl_viewport, has_gl_viewport);
182 }
183 final int[] exp_gl_viewport = { -1, -1, -1, -1 };
184 final int[] has_gl_viewport = { -1, -1, -1, -1 };
185
186 public static void main(final String[] args) {
187 for(int i=0; i<args.length; i++) {
188 if(args[i].equals("-time")) {
189 i++;
190 duration = MiscUtils.atol(args[i], duration);
191 }
192 }
193 org.junit.runner.JUnitCore.main(TestGLJPanelReadd01Bug1310AWT.class.getName());
194 }
195
196 class MyRotTriangle implements GLEventListener {
197 private final GLReadBufferUtil screenshot;
198 private int sn = 0;
199
200 private double theta = 0;
201 private double s = 0;
202 private double c = 0;
203 private boolean doScreenshot = false;
204
205 public MyRotTriangle() {
206 screenshot = new GLReadBufferUtil(true, false);
207 }
208 @Override
209 public void display(final GLAutoDrawable drawable) {
210 update();
211 render(drawable);
212 if( doScreenshot ) {
213 snapshot(sn++, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
214 doScreenshot = false;
215 }
216 }
217
218 @Override
219 public void dispose(final GLAutoDrawable drawable) {
220 System.err.println("GLEL dispose");
221 }
222
223 @Override
224 public void init(final GLAutoDrawable drawable) {
225 System.err.println("GLEL init: Surface "+drawable.getSurfaceWidth()+"x"+drawable.getSurfaceWidth()+
226 ", "+drawable.getClass().getSimpleName()+
227 ", swap-ival "+drawable.getGL().getSwapInterval());
228 theta = 0;
229 s = 0;
230 c = 0;
231 doScreenshot = true;
232 }
233
234 @Override
235 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int w, final int h) {
236 exp_gl_viewport[0] = x;
237 exp_gl_viewport[1] = y;
238 exp_gl_viewport[2] = w;
239 exp_gl_viewport[3] = h;
240 System.err.println("GLEL reshape: Surface "+drawable.getSurfaceWidth()+"x"+drawable.getSurfaceWidth()+
241 ", reshape "+x+"/"+y+" "+w+"x"+h);
242 final GL2 gl = drawable.getGL().getGL2();
243 gl.glGetIntegerv(GL.GL_VIEWPORT, has_gl_viewport, 0);
244 }
245
246 private void update() {
247 theta += 0.01;
248 s = Math.sin(theta);
249 c = Math.cos(theta);
250 }
251
252 private void render(final GLAutoDrawable drawable) {
253 final GL2 gl = drawable.getGL().getGL2();
254
255 gl.glClear(GL.GL_COLOR_BUFFER_BIT);
256
257 // draw a triangle filling the window
258 gl.glBegin(GL.GL_TRIANGLES);
259 gl.glColor3f(1, 0, 0);
260 gl.glVertex2d(-c, -c);
261 gl.glColor3f(0, 1, 0);
262 gl.glVertex2d(0, c);
263 gl.glColor3f(0, 0, 1);
264 gl.glVertex2d(s, -s);
265 gl.glEnd();
266 }
267 }
268}
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static void initSingleton()
Static initialization of JOGL.
Definition: GLProfile.java:204
A lightweight Swing component which provides OpenGL rendering support.
Definition: GLJPanel.java:189
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLJPanel.java:989
void test(final GLCapabilitiesImmutable caps, final GLEventListener demo)
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
An Animator subclass which attempts to achieve a target frames-per-second rate to avoid using all CPU...
final synchronized boolean start()
Starts this animator, if not running.
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
int getSwapInterval()
Return the current swap interval.
Specifies an immutable set of OpenGL capabilities.
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void repaint()
Schedules a repaint of the component at some point in the future.