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 package com.jogamp.opengl.test.junit.jogl.awt;
29 
30 import java.awt.Dimension;
31 import java.lang.reflect.InvocationTargetException;
32 
33 import com.jogamp.opengl.GLCapabilities;
34 import com.jogamp.opengl.GLCapabilitiesImmutable;
35 import com.jogamp.opengl.GLProfile;
36 import com.jogamp.opengl.awt.GLJPanel;
37 import javax.swing.JFrame;
38 import javax.swing.JPanel;
39 import javax.swing.SwingUtilities;
40 
41 import org.junit.Assume;
42 import org.junit.BeforeClass;
43 import org.junit.FixMethodOrder;
44 import org.junit.Test;
45 import org.junit.runners.MethodSorters;
46 
47 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
48 import com.jogamp.opengl.test.junit.util.MiscUtils;
49 import com.jogamp.opengl.test.junit.util.UITestCase;
50 
51 /**
52  * Multiple GLJPanels in a JFrame
53  */
54 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
55 public class TestGLJPanelResize01AWT extends UITestCase {
56 
57     @BeforeClass
initClass()58     public static void initClass() {
59         GLProfile.initSingleton();
60     }
61 
62     static Dimension[] esize00 = {
63         new Dimension(281, 151),
64         new Dimension(282, 151),
65         new Dimension(283, 151),
66         new Dimension(284, 151),
67 
68         new Dimension(284, 152),
69         new Dimension(283, 152),
70         new Dimension(282, 152),
71         new Dimension(281, 152),
72 
73         new Dimension(291, 153),
74         new Dimension(292, 153),
75         new Dimension(293, 153),
76         new Dimension(294, 153),
77 
78         new Dimension(281, 154),
79         new Dimension(282, 154),
80         new Dimension(283, 154),
81         new Dimension(284, 154)
82     };
83     static Dimension[] esize01 = {
84         new Dimension(283, 154), // #3: new sub-aligned image in pixelBuffer-1
85         new Dimension(291, 154), // #2: new pixelBuffer-1
86         new Dimension(282, 154), // #1: new pixelBuffer-0
87     };
88     static Dimension[] esize02 = {
89         new Dimension(291, 154), // #2: new pixelBuffer-1
90         new Dimension(282, 154), // #1: new pixelBuffer-0
91     };
92 
test(final GLCapabilitiesImmutable caps, final Dimension[] dims, final boolean useSwingDoubleBuffer)93     public void test(final GLCapabilitiesImmutable caps, final Dimension[] dims, final boolean useSwingDoubleBuffer) {
94         final int cols = 4;
95         final int rows = dims.length / cols + ( dims.length % cols > 0 ? 1 : 0 );
96         final JFrame[] frame = new JFrame[] { null };
97 
98         System.err.println("Frame size: cols x rows "+cols+"x"+rows);
99         try {
100             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
101                 public void run() {
102                     frame[0] = new JFrame();
103                     frame[0].setLocation(64, 64);
104                     final JPanel panel = new JPanel();
105                     panel.setLayout(null); // new BorderLayout());
106                     panel.setDoubleBuffered(useSwingDoubleBuffer);
107                     frame[0].getContentPane().add(panel);
108 
109                     final int x0 = 4;
110                     int x = x0, y = 4;
111                     int maxRowWidth = 0;
112                     for(int i=0; i<rows; i++) {
113                         int maxColHeight = 0;
114                         for(int j=0; j<cols; j++) {
115                             final int idx = i*cols+j;
116                             if( idx >= dims.length ) { break; }
117                             final Dimension d = dims[idx];
118                             if( d.height > maxColHeight ) {
119                                 maxColHeight = d.height;
120                             }
121                             final GLJPanel glad = createGLJPanel(useSwingDoubleBuffer, caps, d, "[r "+i+", c "+j+"]");
122                             panel.add(glad);
123                             glad.setLocation(x, y);
124                             x+=d.width+4;
125                         }
126                         if( x > maxRowWidth ) {
127                             maxRowWidth = x;
128                         }
129                         x = x0;
130                         y += maxColHeight+4;
131                     }
132                     frame[0].setSize(maxRowWidth+4+64, y+4+64);
133                     // frame[0].pack();
134                     frame[0].setVisible(true);
135                 } } );
136         } catch( final Throwable throwable ) {
137             throwable.printStackTrace();
138             Assume.assumeNoException( throwable );
139         }
140         try {
141             Thread.sleep(duration);
142         } catch (final InterruptedException e1) {
143             e1.printStackTrace();
144         }
145         try {
146             SwingUtilities.invokeAndWait(new Runnable() {
147                     public void run() {
148                         frame[0].dispose();
149                     } } );
150         } catch (final Exception e1) {
151             e1.printStackTrace();
152         }
153     }
154 
createGLJPanel(final boolean useSwingDoubleBuffer, final GLCapabilitiesImmutable caps, final Dimension size, final String name)155     private GLJPanel createGLJPanel(final boolean useSwingDoubleBuffer, final GLCapabilitiesImmutable caps, final Dimension size, final String name) {
156         final GLJPanel canvas = new GLJPanel(caps);
157         canvas.setName(name);
158         canvas.setSize(size);
159         canvas.setPreferredSize(size);
160         canvas.setMinimumSize(size);
161         canvas.setDoubleBuffered(useSwingDoubleBuffer);
162         final GearsES2 g = new GearsES2(0);
163         g.setVerbose(false);
164         canvas.addGLEventListener(g);
165         return canvas;
166     }
167 
168     static GLCapabilitiesImmutable caps = null;
169 
170     // @Test
test00()171     public void test00() throws InterruptedException, InvocationTargetException {
172         test(new GLCapabilities(null), esize00, false /*useSwingDoubleBuffer*/);
173     }
174 
175     @Test
test01()176     public void test01() throws InterruptedException, InvocationTargetException {
177         test(new GLCapabilities(null), esize01, false /*useSwingDoubleBuffer*/);
178     }
179 
180     @Test
test02()181     public void test02() throws InterruptedException, InvocationTargetException {
182         test(new GLCapabilities(null), esize02, false /*useSwingDoubleBuffer*/);
183     }
184 
185     static long duration = 600; // ms
186 
main(final String[] args)187     public static void main(final String[] args) {
188         boolean useSwingDoubleBuffer=false, manual=false;
189 
190         for(int i=0; i<args.length; i++) {
191             if(args[i].equals("-time")) {
192                 i++;
193                 duration = MiscUtils.atol(args[i], duration);
194             } else if(args[i].equals("-swingDoubleBuffer")) {
195                 useSwingDoubleBuffer = true;
196             } else if(args[i].equals("-manual")) {
197                 manual = true;
198             }
199         }
200         if( manual ) {
201             GLProfile.initSingleton();
202             final TestGLJPanelResize01AWT demo = new TestGLJPanelResize01AWT();
203             demo.test(new GLCapabilities(null), esize01, useSwingDoubleBuffer);
204         } else {
205             org.junit.runner.JUnitCore.main(TestGLJPanelResize01AWT.class.getName());
206         }
207     }
208 
209 }
210