JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestJScrollPaneMixHwLw01AWT.java
Go to the documentation of this file.
1package com.jogamp.opengl.test.junit.jogl.awt;
2
3import java.awt.BorderLayout;
4import java.awt.Component;
5import java.awt.Dimension;
6import java.awt.GraphicsConfiguration;
7import java.awt.Rectangle;
8import java.awt.ScrollPane;
9import java.awt.Shape;
10import java.io.IOException;
11
12import com.jogamp.opengl.GLCapabilities;
13import com.jogamp.opengl.GLProfile;
14import com.jogamp.opengl.awt.GLCanvas;
15import javax.swing.JFrame;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18import javax.swing.JTextArea;
19import javax.swing.WindowConstants;
20
21import org.junit.Assert;
22import org.junit.Assume;
23import org.junit.Test;
24import org.junit.FixMethodOrder;
25import org.junit.runners.MethodSorters;
26
27import com.jogamp.common.util.ReflectionUtil;
28import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
29import com.jogamp.opengl.test.junit.util.MiscUtils;
30import com.jogamp.opengl.test.junit.util.UITestCase;
31import com.jogamp.opengl.util.Animator;
32
33/**
34 * Documenting Bug 586
35 *
36 * <p>
37 * JScrollPane cannot mix hw/lw components, only if setting property '-Dsun.awt.disableMixing=true'.
38 * </p>
39 * <p>
40 * You can use ScrollPane, or maybe a slider and fwd the panning to the GLCanvas,
41 * which could change it's GL viewport accordingly.
42 * </p>
43 * See git commit '8df12ca151dfc577c90b485d4ebfe491b88e55aa'.
44 */
45@FixMethodOrder(MethodSorters.NAME_ASCENDING)
47 static long durationPerTest = 500;
48
49 static {
50 // too late: use at cmd-line '-Dsun.awt.disableMixing=true' works
51 // System.setProperty("sun.awt.disableMixing", "true");
52 }
53
54 /**
55 * Doesn't work either ..
56 */
57 @SuppressWarnings("serial")
58 public static class TransparentJScrollPane extends JScrollPane {
59
60 public TransparentJScrollPane(final Component view) {
61 super(view);
62
63 setOpaque(false);
64
65 try {
66 ReflectionUtil.callStaticMethod(
67 "com.sun.awt.AWTUtilities", "setComponentMixingCutoutShape",
68 new Class<?>[] { Component.class, Shape.class },
69 new Object[] { this, new Rectangle() } ,
70 GraphicsConfiguration.class.getClassLoader());
71 System.err.println("com.sun.awt.AWTUtilities.setComponentMixingCutoutShape(..) passed");
72 } catch (final RuntimeException re) {
73 System.err.println("com.sun.awt.AWTUtilities.setComponentMixingCutoutShape(..) failed: "+re.getMessage());
74 }
75 }
76
77 @Override
78 public void setOpaque(final boolean isOpaque) {
79 }
80 }
81
82 protected void runTestGL(final GLCapabilities caps, final boolean useJScroll) throws InterruptedException {
83 final String typeS = useJScroll ? "LW" : "HW";
84 final JFrame frame = new JFrame("Mix Hw/Lw Swing - ScrollPane "+typeS);
85 Assert.assertNotNull(frame);
86
87 final Dimension f_sz = new Dimension(600,400);
88 final Dimension glc_sz = new Dimension(500,600);
89
90 final GLCanvas glCanvas = new GLCanvas(caps);
91 Assert.assertNotNull(glCanvas);
92 glCanvas.addGLEventListener(new GearsES2());
93 glCanvas.setPreferredSize(glc_sz);
94
95 final JPanel panel = new JPanel(new BorderLayout());
96 panel.setOpaque(false);
97 if(useJScroll) {
98 final JScrollPane scrollPane = new TransparentJScrollPane(glCanvas);
99 panel.add(scrollPane, BorderLayout.CENTER);
100 } else {
101 final ScrollPane scrollPane = new ScrollPane();
102 scrollPane.add(glCanvas);
103 panel.add(scrollPane, BorderLayout.CENTER);
104 }
105
106 final JTextArea textArea = new JTextArea();
107 textArea.setText("Test\nTest\nTest\nTest\n");
108
109 panel.add(textArea, BorderLayout.NORTH);
110
111 frame.add(panel);
112 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
113
114 try {
115 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
116 public void run() {
117 frame.setLocationRelativeTo(null);
118 frame.setTitle("GLCanvas in JScrollPane example");
119 frame.setSize(f_sz);
120 frame.setVisible(true);
121 }});
122 } catch (final Throwable t) {
123 t.printStackTrace();
124 Assume.assumeNoException(t);
125 }
126
127 final Animator animator = new Animator(glCanvas);
128 animator.start();
129
130 Thread.sleep(durationPerTest);
131
132 animator.stop();
133
134 try {
135 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
136 public void run() {
137 frame.setVisible(false);
138 frame.dispose();
139 }});
140 } catch (final Throwable t) {
141 t.printStackTrace();
142 Assume.assumeNoException(t);
143 }
144 }
145
146 // @Test doesn't work
147 public void test01JScrollPane() throws InterruptedException {
148 final GLProfile glp = GLProfile.getGL2ES2();
149 final GLCapabilities caps = new GLCapabilities(glp);
150 runTestGL(caps, true);
151 }
152
153 @Test
154 public void test01ScrollPane() throws InterruptedException {
155 final GLProfile glp = GLProfile.getGL2ES2();
156 final GLCapabilities caps = new GLCapabilities(glp);
157 runTestGL(caps, false);
158 }
159
160 public static void main(final String args[]) throws IOException {
161 for(int i=0; i<args.length; i++) {
162 if(args[i].equals("-time")) {
163 durationPerTest = MiscUtils.atol(args[++i], durationPerTest);
164 }
165 }
166 /**
167 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
168 System.err.println("Press enter to continue");
169 System.err.println(stdin.readLine());
170 */
171 System.out.println("durationPerTest: "+durationPerTest);
172 final String tstname = TestJScrollPaneMixHwLw01AWT.class.getName();
173 org.junit.runner.JUnitCore.main(tstname);
174 }
175
176}
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
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
void runTestGL(final GLCapabilities caps, final boolean useJScroll)
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
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