JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestBug1431NewtCanvasAWT.java
Go to the documentation of this file.
1package com.jogamp.opengl.test.junit.jogl.demos.es2.newt;
2
3import java.awt.Frame;
4import java.lang.reflect.InvocationTargetException;
5
6import javax.swing.SwingUtilities;
7
8import org.junit.Assert;
9import org.junit.Assume;
10import org.junit.FixMethodOrder;
11import org.junit.Test;
12import org.junit.runners.MethodSorters;
13
14import com.jogamp.nativewindow.util.DimensionImmutable;
15import com.jogamp.newt.awt.NewtCanvasAWT;
16import com.jogamp.newt.opengl.GLWindow;
17import com.jogamp.opengl.GL;
18import com.jogamp.opengl.GL2;
19import com.jogamp.opengl.GL2ES1;
20import com.jogamp.opengl.GL2ES3;
21import com.jogamp.opengl.GLAutoDrawable;
22import com.jogamp.opengl.GLCapabilities;
23import com.jogamp.opengl.GLEventListener;
24import com.jogamp.opengl.GLProfile;
25import com.jogamp.opengl.fixedfunc.GLLightingFunc;
26import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
27import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
28import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
29import com.jogamp.opengl.test.junit.util.MiscUtils;
30import com.jogamp.opengl.test.junit.util.NewtTestUtil;
31import com.jogamp.opengl.test.junit.util.UITestCase;
32import com.jogamp.opengl.util.Animator;
33
34/**
35 * Self-contained example (within a single class only to keep it simple)
36 * displaying a rotating quad
37 */
38@FixMethodOrder(MethodSorters.NAME_ASCENDING)
40
41 public static class JOGLQuadNewt implements GLEventListener {
42 final int[] exp_gl_viewport = { -1, -1, -1, -1 };
43 final int[] has_gl_viewport = { -1, -1, -1, -1 };
44 private float rotateT = 0.0f;
45
46 @Override
47 public void display(final GLAutoDrawable gLDrawable)
48 {
49 final GL2 gl = gLDrawable.getGL().getGL2();
52 gl.glLoadIdentity();
53 gl.glTranslatef(0.0f, 0.0f, -5.0f);
54
55 // rotate about the three axes
56 gl.glRotatef(rotateT, 1.0f, 0.0f, 0.0f);
57 gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f);
58 gl.glRotatef(rotateT, 0.0f, 0.0f, 1.0f);
59
60 // Draw A Quad
62 gl.glColor3f(0.0f, 1.0f, 1.0f); // set the color of the quad
63 gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
64 gl.glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
65 gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
66 gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
67 // Done Drawing The Quad
68 gl.glEnd();
69
70 // increasing rotation for the next iteration
71 rotateT += 0.2f;
72 }
73
74 @Override
75 public void init(final GLAutoDrawable glDrawable)
76 {
77 final GL2 gl = glDrawable.getGL().getGL2();
79 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
80 gl.glClearDepth(1.0f);
84 }
85
86 @Override
87 public void reshape(final GLAutoDrawable glDrawable, final int x, final int y, final int width, final int height)
88 {
89 final GL2 gl = glDrawable.getGL().getGL2();
90 exp_gl_viewport[0] = x;
91 exp_gl_viewport[1] = y;
92 exp_gl_viewport[2] = width;
93 exp_gl_viewport[3] = height;
94 gl.glGetIntegerv(GL.GL_VIEWPORT, has_gl_viewport, 0);
95 System.err.println("GLEL reshape: Surface "+glDrawable.getSurfaceWidth()+"x"+glDrawable.getSurfaceWidth()+
96 ", reshape "+x+"/"+y+" "+width+"x"+height);
97 System.err.println("GLEL reshape: Viewport "+has_gl_viewport[0]+"/"+has_gl_viewport[1]+", "+has_gl_viewport[2]+"x"+has_gl_viewport[3]);
98
99 final float aspect = (float) width / (float) height;
101 gl.glLoadIdentity();
102 final float fh = 0.5f;
103 final float fw = fh * aspect;
104 gl.glFrustumf(-fw, fw, -fh, fh, 1.0f, 1000.0f);
106 gl.glLoadIdentity();
107 }
108
109 @Override
110 public void dispose(final GLAutoDrawable gLDrawable)
111 {}
112 }
113
114 static long duration = 500; // ms
115
116 static void setFrameSize(final Frame frame, final boolean frameLayout, final int newWith, final int newHeight) {
117 try {
118 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
119 @Override
120 public void run() {
121 final java.awt.Dimension d = new java.awt.Dimension(newWith, newHeight);
122 frame.setSize(d);
123 if( frameLayout ) {
124 frame.validate();
125 }
126 } } );
127 } catch( final Throwable throwable ) {
128 throwable.printStackTrace();
129 Assume.assumeNoException( throwable );
130 }
131 }
132
133 @Test
134 public void test01() throws InterruptedException, InvocationTargetException {
135 // System.setProperty("sun.awt.noerasebackground", "true");
136 final GLProfile profile = GLProfile.getDefault();
137 final GLCapabilities glCapabilities = new GLCapabilities(profile);
138 final GLWindow glWindow = GLWindow.create(glCapabilities);
139 final GLEventListener demo = new JOGLQuadNewt();
140 // final GLEventListener demo = new RedSquareES2(1);
141 glWindow.addGLEventListener( demo );
142
144 snap.setPostSNDetail(getClass().getSimpleName());
145 glWindow.addGLEventListener(snap);
146
147 glWindow.addWindowListener(new com.jogamp.newt.event.WindowAdapter() {
148 @Override
149 public void windowResized(final com.jogamp.newt.event.WindowEvent e) {
150 System.err.println("window resized: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
151 }
152 @Override
153 public void windowMoved(final com.jogamp.newt.event.WindowEvent e) {
154 System.err.println("window moved: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight());
155 }
156 });
157
158 final NewtCanvasAWT canvas = new NewtCanvasAWT(glWindow);
159 final Frame frame = new Frame(getClass().getSimpleName());
160 final Animator animator = new Animator(glWindow);
161 final boolean[] isClosed = { false };
162
163 frame.add(canvas);
164 frame.setSize(640, 480);
165 // frame.setResizable(false);
166 frame.addWindowListener(new java.awt.event.WindowAdapter() {
167 @Override
168 public void windowClosing(final java.awt.event.WindowEvent e)
169 {
170 isClosed[0] = true;
171 animator.stop();
172 frame.dispose();
173 }
174 });
175
176 SwingUtilities.invokeAndWait(new Runnable() {
177 @Override
178 public void run() {
179 frame.validate();
180 frame.setVisible(true);
181 }
182 });
183 Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true, null));
184 Assert.assertEquals(true, NewtTestUtil.waitForRealized(glWindow, true, null));
185
186 animator.start();
187 Assert.assertTrue(animator.isAnimating());
188 Assert.assertTrue(animator.isStarted());
189
190 System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities());
191 System.err.println("GL chosen: "+glWindow.getChosenCapabilities());
192 System.err.println("window pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+", "+glWindow.getInsets());
193 System.err.println("XXXX");
194
195 canvas.requestFocus();
196 snap.setMakeSnapshot();
197
198 Thread.sleep(100);
199 setFrameSize(frame, false /* frameLayout */, 800, 600);
200 snap.setMakeSnapshot();
201 Thread.sleep(10);
202 snap.setMakeSnapshot();
203
204 final long t0 = System.currentTimeMillis();
205 long t1 = t0;
206 while(!isClosed[0] && t1-t0<duration) {
207 Thread.sleep(100);
208 t1 = System.currentTimeMillis();
209 }
210
211 if( !isClosed[0] ) {
212 animator.stop();
213 Assert.assertFalse(animator.isAnimating());
214 Assert.assertFalse(animator.isStarted());
215 SwingUtilities.invokeAndWait(new Runnable() {
216 @Override
217 public void run() {
218 frame.dispose();
219 }
220 });
221 glWindow.destroy();
222 Assert.assertEquals(true, NewtTestUtil.waitForRealized(glWindow, false, null));
223 }
224 }
225
226 public static void main(final String[] args)
227 {
228 for(int i=0; i<args.length; i++) {
229 if(args[i].equals("-time")) {
230 i++;
231 duration = MiscUtils.atol(args[i], duration);
232 }
233 }
234 System.err.println("duration "+duration);
235 org.junit.runner.JUnitCore.main(TestBug1431NewtCanvasAWT.class.getName());
236 }
237}
AWT Canvas containing a NEWT Window using native parenting.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
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.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
void display(final GLAutoDrawable gLDrawable)
Called by the drawable to initiate OpenGL rendering by the client.
void dispose(final GLAutoDrawable gLDrawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void init(final GLAutoDrawable glDrawable)
Called by the drawable immediately after the OpenGL context is initialized.
void reshape(final GLAutoDrawable glDrawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
Self-contained example (within a single class only to keep it simple) displaying a rotating quad.
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
synchronized boolean isStarted()
Indicates whether this animator has been started.
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
static final int GL_PERSPECTIVE_CORRECTION_HINT
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_PERSPECTIVE_CORRECTION_HINT" with expression '0x0C50',...
Definition: GL2ES1.java:124
static final int GL_QUADS
GL_ES_VERSION_3_2, GL_VERSION_1_1, GL_VERSION_1_0, GL_OES_tessellation_shader, GL_EXT_tessellation_sh...
Definition: GL2ES3.java:734
void glBegin(int mode)
Entry point to C language function: void {@native glBegin}(GLenum mode) Part of GL_VERSION_1_0
void glVertex3f(float x, float y, float z)
Entry point to C language function: void {@native glVertex3f}(GLfloat x, GLfloat y,...
void glEnd()
Entry point to C language function: void {@native glEnd}() Part of GL_VERSION_1_0
void glColor3f(float red, float green, float blue)
Entry point to C language function: void {@native glColor3f}(GLfloat red, GLfloat green,...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
void glClearDepth(double depth)
Aliased entrypoint of void {@native glClearDepth}(GLclampd depth); and void {@native glClearDepthf...
GL2 getGL2()
Casts this object to the GL2 interface.
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 glGetIntegerv(int pname, IntBuffer data)
Entry point to C language function: void {@native glGetIntegerv}(GLenum pname, GLint * data) Part ...
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
Definition: GL.java:390
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
static final int GL_DEPTH_TEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expr...
Definition: GL.java:43
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
void glHint(int target, int mode)
Entry point to C language function: void {@native glHint}(GLenum target, GLenum mode) Part of GL_E...
static final int GL_NICEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_NICEST" with expressi...
Definition: GL.java:801
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738
void glDepthFunc(int func)
Entry point to C language function: void {@native glDepthFunc}(GLenum func) Part of GL_ES_VERSION_...
static final int GL_LEQUAL
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LEQUAL" with expressi...
Definition: GL.java:469
static final int GL_VIEWPORT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_VIEWPORT" with expres...
Definition: GL.java:725
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
void glFrustumf(float left, float right, float bottom, float top, float zNear, float zFar)
Multiply the current matrix with the frustum matrix.
void glTranslatef(float x, float y, float z)
Translate the current matrix.
void glRotatef(float angle, float x, float y, float z)
Rotate the current matrix.
static final int GL_MODELVIEW
Matrix mode modelview.
void glLoadIdentity()
Load the current matrix with the identity matrix.
void glMatrixMode(int mode)
Sets the current matrix mode.