JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestTiledPrintingGearsSwingAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 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.tile;
30
31import java.awt.BorderLayout;
32import java.awt.Button;
33import java.awt.Container;
34import java.awt.Dimension;
35import java.awt.Frame;
36import java.awt.Label;
37import java.awt.event.ActionEvent;
38import java.awt.event.ActionListener;
39import java.awt.image.BufferedImage;
40import java.awt.print.PageFormat;
41import java.io.BufferedReader;
42import java.io.IOException;
43import java.io.InputStreamReader;
44import java.lang.reflect.InvocationTargetException;
45
46import com.jogamp.opengl.GLCapabilities;
47import com.jogamp.opengl.GLProfile;
48import com.jogamp.opengl.awt.GLJPanel;
49import javax.swing.BorderFactory;
50import javax.swing.JButton;
51import javax.swing.JComponent;
52import javax.swing.JFrame;
53import javax.swing.JLayeredPane;
54import javax.swing.JPanel;
55import javax.swing.SwingUtilities;
56
57import org.junit.AfterClass;
58import org.junit.Assert;
59import org.junit.BeforeClass;
60import org.junit.FixMethodOrder;
61import org.junit.Test;
62import org.junit.runners.MethodSorters;
63
64import com.jogamp.common.os.Platform;
65import com.jogamp.newt.event.TraceKeyAdapter;
66import com.jogamp.newt.event.TraceWindowAdapter;
67import com.jogamp.newt.event.awt.AWTKeyAdapter;
68import com.jogamp.newt.event.awt.AWTWindowAdapter;
69import com.jogamp.opengl.test.junit.jogl.demos.es1.RedSquareES1;
70import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
71import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
72import com.jogamp.opengl.test.junit.util.MiscUtils;
73import com.jogamp.opengl.test.junit.util.QuitAdapter;
74import com.jogamp.opengl.util.Animator;
75
76@FixMethodOrder(MethodSorters.NAME_ASCENDING)
78
79 static boolean waitForKey = false;
80 /** only when run manually .. */
81 static boolean allow600dpi = false;
82 static GLProfile glp;
83 static int width, height;
84
85 @BeforeClass
86 public static void initClass() {
89 Assert.assertNotNull(glp);
90 width = 640;
91 height = 480;
92 } else {
93 setTestSupported(false);
94 }
95 // Runtime.getRuntime().traceInstructions(true);
96 // Runtime.getRuntime().traceMethodCalls(true);
97 }
98
99 @AfterClass
100 public static void releaseClass() {
101 }
102
103 protected void runTestGL(final GLCapabilities caps, final boolean layered, final boolean skipGLOrientationVerticalFlip) throws InterruptedException, InvocationTargetException {
104 final int layerStepX = width/6, layerStepY = height/6;
105 final Dimension glc_sz = new Dimension(layered ? width - 2*layerStepX : width/2, layered ? height - 2*layerStepY : height);
106 final GLJPanel glJPanel1 = new GLJPanel(caps);
107 Assert.assertNotNull(glJPanel1);
108 glJPanel1.setSkipGLOrientationVerticalFlip(skipGLOrientationVerticalFlip);
109 glJPanel1.setMinimumSize(glc_sz);
110 glJPanel1.setPreferredSize(glc_sz);
111 if( layered ) {
112 glJPanel1.setBounds(layerStepX/2, layerStepY/2, glc_sz.width, glc_sz.height);
113 } else {
114 glJPanel1.setBounds(0, 0, glc_sz.width, glc_sz.height);
115 }
116 {
117 final Gears demo = new Gears();
118 demo.setFlipVerticalInGLOrientation(skipGLOrientationVerticalFlip);
119 glJPanel1.addGLEventListener(demo);
120 }
121
122 final GLJPanel glJPanel2 = new GLJPanel(caps);
123 Assert.assertNotNull(glJPanel2);
124 glJPanel2.setSkipGLOrientationVerticalFlip(skipGLOrientationVerticalFlip);
125 glJPanel2.setMinimumSize(glc_sz);
126 glJPanel2.setPreferredSize(glc_sz);
127 if( layered ) {
128 glJPanel2.setBounds(3*layerStepY, 2*layerStepY, glc_sz.width, glc_sz.height);
129 } else {
130 glJPanel2.setBounds(0, 0, glc_sz.width, glc_sz.height);
131 }
132 {
133 final RedSquareES1 demo = new RedSquareES1();
134 demo.setFlipVerticalInGLOrientation(skipGLOrientationVerticalFlip);
135 glJPanel2.addGLEventListener(demo);
136 }
137
138 final JComponent demoPanel;
139 if( layered ) {
140 glJPanel1.setOpaque(true);
141 glJPanel2.setOpaque(false);
142 final Dimension lsz = new Dimension(width, height);
143 demoPanel = new JLayeredPane();
144 demoPanel.setMinimumSize(lsz);
145 demoPanel.setPreferredSize(lsz);
146 demoPanel.setBounds(0, 0, lsz.width, lsz.height);
147 demoPanel.setBorder(BorderFactory.createTitledBorder("Layered Pane"));
148 demoPanel.add(glJPanel1, JLayeredPane.DEFAULT_LAYER);
149 demoPanel.add(glJPanel2, Integer.valueOf(1));
150 final JButton tb = new JButton("On Top");
151 tb.setBounds(4*layerStepY, 3*layerStepY, 100, 50);
152 demoPanel.add(tb, Integer.valueOf(2));
153 } else {
154 demoPanel = new JPanel();
155 demoPanel.add(glJPanel1);
156 demoPanel.add(glJPanel2);
157 }
158
159 final JFrame frame = new JFrame("Swing Print");
160 Assert.assertNotNull(frame);
161
162 final ActionListener print72DPIAction = new ActionListener() {
163 public void actionPerformed(final ActionEvent e) {
164 doPrintManual(frame, 72, 0, -1, -1);
165 } };
166 final ActionListener print300DPIAction = new ActionListener() {
167 public void actionPerformed(final ActionEvent e) {
168 doPrintManual(frame, 300, -1, -1, -1);
169 } };
170 final ActionListener print600DPIAction = new ActionListener() {
171 public void actionPerformed(final ActionEvent e) {
172 doPrintManual(frame, 600, -1, -1, -1);
173 } };
174 final Button print72DPIButton = new Button("72dpi");
175 print72DPIButton.addActionListener(print72DPIAction);
176 final Button print300DPIButton = new Button("300dpi");
177 print300DPIButton.addActionListener(print300DPIAction);
178 final Button print600DPIButton = new Button("600dpi");
179 print600DPIButton.addActionListener(print600DPIAction);
180
181 final JPanel printPanel = new JPanel();
182 printPanel.add(print72DPIButton);
183 printPanel.add(print300DPIButton);
184 printPanel.add(print600DPIButton);
185 final JPanel southPanel = new JPanel();
186 southPanel.add(new Label("South"));
187 final JPanel eastPanel = new JPanel();
188 eastPanel.add(new Label("East"));
189 final JPanel westPanel = new JPanel();
190 westPanel.add(new Label("West"));
191
192 final Animator animator = new Animator();
193 animator.add(glJPanel1);
194 animator.add(glJPanel2);
195 final QuitAdapter quitAdapter = new QuitAdapter();
196 new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter), glJPanel1).addTo(glJPanel1);
197 new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter), glJPanel2).addTo(glJPanel2);
198 new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter), glJPanel2).addTo(frame);
199
200 SwingUtilities.invokeAndWait(new Runnable() {
201 public void run() {
202 final Container fcont = frame.getContentPane();
203 fcont.setLayout(new BorderLayout());
204 fcont.add(printPanel, BorderLayout.NORTH);
205 fcont.add(demoPanel, BorderLayout.CENTER);
206 fcont.add(southPanel, BorderLayout.SOUTH);
207 fcont.add(eastPanel, BorderLayout.EAST);
208 fcont.add(westPanel, BorderLayout.WEST);
209 fcont.validate();
210 frame.pack();
211 frame.setVisible(true);
212 } } ) ;
213
214 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
215 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glJPanel1, true, null));
216 Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glJPanel2, true, null));
217
218 animator.setUpdateFPSFrames(60, System.err);
219 animator.start();
220 Assert.assertEquals(true, animator.isAnimating());
221
222 boolean printDone = false;
223 while(!quitAdapter.shouldQuit() && animator.isAnimating() && ( 0 == duration || animator.getTotalFPSDuration()<duration )) {
224 Thread.sleep(200);
225 if( !printDone ) {
226 printDone = true;
227 {
228 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, -1 /* offscreen-type */, 72, 0, -1, -1, false /* resizeWithinPrint */);
229 waitUntilPrintJobsIdle(p);
230 }
231 {
232 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, -1 /* offscreen-type */, 72, 8, -1, -1, false /* resizeWithinPrint */);
233 waitUntilPrintJobsIdle(p);
234 }
235 {
236 // No AA needed for 150 dpi and greater :)
237 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, -1 /* offscreen-type */, 150, -1, -1, -1, false /* resizeWithinPrint */);
238 waitUntilPrintJobsIdle(p);
239 }
240 {
241 // No AA needed for 150 dpi and greater :)
242 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, -1 /* offscreen-type */, 150, -1, 2048, 2048, false /* resizeWithinPrint */);
243 waitUntilPrintJobsIdle(p);
244 }
245 {
246 // No AA needed for 150 dpi and greater :)
247 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, -1 /* offscreen-type */, 150, -1, -1, -1, true /* resizeWithinPrint */);
248 waitUntilPrintJobsIdle(p);
249 }
250 {
251 // No AA needed for 150 dpi and greater :)
252 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, BufferedImage.TYPE_INT_ARGB_PRE /* offscreen-type */, 150, -1, -1, -1, false /* resizeWithinPrint */);
253 waitUntilPrintJobsIdle(p);
254 }
255 {
256 // No AA needed for 150 dpi and greater :)
257 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, BufferedImage.TYPE_INT_ARGB /* offscreen-type */, 150, -1, -1, -1, false /* resizeWithinPrint */);
258 waitUntilPrintJobsIdle(p);
259 }
260 {
261 // No AA needed for 150 dpi and greater :)
262 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, BufferedImage.TYPE_INT_RGB /* offscreen-type */, 150, -1, -1, -1, false /* resizeWithinPrint */);
263 waitUntilPrintJobsIdle(p);
264 }
265 {
266 // No AA needed for 150 dpi and greater :)
267 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, BufferedImage.TYPE_INT_BGR /* offscreen-type */, 150, -1, -1, -1, false /* resizeWithinPrint */);
268 waitUntilPrintJobsIdle(p);
269 }
270 {
271 // No AA needed for 150 dpi and greater :)
272 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, BufferedImage.TYPE_INT_ARGB_PRE /* offscreen-type */, 150, -1, -1, -1, true /* resizeWithinPrint */);
273 waitUntilPrintJobsIdle(p);
274 }
275 if( allow600dpi ) {
276 // No AA needed for 300 dpi and greater :)
277 final PrintableBase p = doPrintAuto(frame, PageFormat.LANDSCAPE, null, -1 /* offscreen-type */, 600, -1, -1, -1, false /* resizeWithinPrint */);
278 waitUntilPrintJobsIdle(p);
279 }
280 }
281 }
282
283 Assert.assertNotNull(frame);
284 Assert.assertNotNull(glJPanel1);
285 Assert.assertNotNull(glJPanel2);
286 Assert.assertNotNull(animator);
287
288 animator.stop();
289 Assert.assertEquals(false, animator.isAnimating());
290 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
291 public void run() {
292 frame.setVisible(false);
293 }});
294 Assert.assertEquals(false, frame.isVisible());
295 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
296 public void run() {
297 final Frame _frame = frame;
298 _frame.remove(demoPanel);
299 _frame.dispose();
300 }});
301 }
302
303 @Test
304 public void test01_flip1_aa0() throws InterruptedException, InvocationTargetException {
305 final GLCapabilities caps = new GLCapabilities(glp);
306 runTestGL(caps, false, false);
307 }
308
309 @Test
310 public void test01_flip1_aa0_layered() throws InterruptedException, InvocationTargetException {
311 final GLCapabilities caps = new GLCapabilities(glp);
312 caps.setAlphaBits(8);
313 runTestGL(caps, true, false);
314 }
315
316 @Test
317 public void test01_flip1_aa0_bitmap() throws InterruptedException, InvocationTargetException {
318 if( Platform.OSType.WINDOWS == Platform.getOSType() ) {
319 final GLCapabilities caps = new GLCapabilities(glp);
320 caps.setBitmap(true);
321 runTestGL(caps, false, false);
322 } // issues w/ AMD catalyst driver and pixmap surface ..
323 }
324
325 @Test
326 public void test01_flip1_aa0_bitmap_layered() throws InterruptedException, InvocationTargetException {
327 if( Platform.OSType.WINDOWS == Platform.getOSType() ) {
328 final GLCapabilities caps = new GLCapabilities(glp);
329 caps.setBitmap(true);
330 caps.setAlphaBits(8);
331 runTestGL(caps, true, false);
332 } // issues w/ AMD catalyst driver and pixmap surface ..
333 }
334
335 @Test
336 public void test02_flip1_aa8() throws InterruptedException, InvocationTargetException {
337 final GLCapabilities caps = new GLCapabilities(glp);
338 caps.setSampleBuffers(true);
339 caps.setNumSamples(8);
340 runTestGL(caps, false, false);
341 }
342
343 @Test
344 public void test11_flip0_aa0() throws InterruptedException, InvocationTargetException {
345 final GLCapabilities caps = new GLCapabilities(glp);
346 runTestGL(caps, false, true);
347 }
348
349 @Test
350 public void test11_flip0_aa0_layered() throws InterruptedException, InvocationTargetException {
351 final GLCapabilities caps = new GLCapabilities(glp);
352 caps.setAlphaBits(8);
353 runTestGL(caps, true, true);
354 }
355
356 @Test
357 public void test11_flip0_aa0_bitmap() throws InterruptedException, InvocationTargetException {
358 if( Platform.OSType.WINDOWS == Platform.getOSType() ) {
359 final GLCapabilities caps = new GLCapabilities(glp);
360 caps.setBitmap(true);
361 runTestGL(caps, false, true);
362 } // issues w/ AMD catalyst driver and pixmap surface ..
363 }
364
365 @Test
366 public void test11_flip0_aa0_bitmap_layered() throws InterruptedException, InvocationTargetException {
367 if( Platform.OSType.WINDOWS == Platform.getOSType() ) {
368 final GLCapabilities caps = new GLCapabilities(glp);
369 caps.setBitmap(true);
370 caps.setAlphaBits(8);
371 runTestGL(caps, true, true);
372 } // issues w/ AMD catalyst driver and pixmap surface ..
373 }
374
375 @Test
376 public void test12_flip0_aa8() throws InterruptedException, InvocationTargetException {
377 final GLCapabilities caps = new GLCapabilities(glp);
378 caps.setSampleBuffers(true);
379 caps.setNumSamples(8);
380 runTestGL(caps, false, true);
381 }
382
383 static long duration = 500; // ms
384
385 public static void main(final String args[]) {
386 for(int i=0; i<args.length; i++) {
387 if(args[i].equals("-time")) {
388 i++;
389 duration = MiscUtils.atol(args[i], duration);
390 } else if(args[i].equals("-width")) {
391 i++;
392 width = MiscUtils.atoi(args[i], width);
393 } else if(args[i].equals("-height")) {
394 i++;
395 height = MiscUtils.atoi(args[i], height);
396 } else if(args[i].equals("-600dpi")) {
397 allow600dpi = true;
398 } else if(args[i].equals("-wait")) {
399 waitForKey = true;
400 }
401 }
402 if(waitForKey) {
403 final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
404 System.err.println("Press enter to continue");
405 try {
406 System.err.println(stdin.readLine());
407 } catch (final IOException e) { }
408 }
409 org.junit.runner.JUnitCore.main(TestTiledPrintingGearsSwingAWT.class.getName());
410 }
411}
void setBitmap(final boolean enable)
Requesting offscreen bitmap mode.
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
AWT: printable: PRESSED (t0), TYPED (t0), RELEASED (t1) non-printable: PRESSED (t0),...
synchronized AWTAdapter addTo(final java.awt.Component awtComponent)
Due to the fact that some NEWT com.jogamp.newt.event.NEWTEventListener are mapped to more than one ja...
synchronized AWTAdapter addTo(final java.awt.Component awtComponent)
Due to the fact that some NEWT com.jogamp.newt.event.NEWTEventListener are mapped to more than one ja...
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 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
A lightweight Swing component which provides OpenGL rendering support.
Definition: GLJPanel.java:189
final void setSkipGLOrientationVerticalFlip(final boolean v)
Skip isGLOriented() based vertical flip, which usually is required by the offscreen backend,...
Definition: GLJPanel.java:1255
void setOpaque(final boolean opaque)
Definition: GLJPanel.java:981
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLJPanel.java:989
Gears.java author: Brian Paul (converted to Java by Ron Cemer and Sven Gothel)
Definition: Gears.java:34
void setFlipVerticalInGLOrientation(final boolean v)
Definition: Gears.java:84
void runTestGL(final GLCapabilities caps, final boolean layered, final boolean skipGLOrientationVerticalFlip)
Base unit test class implementing issuing PrinterJob#print() on a Printable implementation,...
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)
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
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.
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