1 /* 2 * Copyright (c) 2002-2008 LWJGL Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'LWJGL' nor the names of 17 * its contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.lwjgl.test.opengl.awt; 33 34 import java.awt.Frame; 35 import java.awt.GridLayout; 36 import java.awt.event.WindowAdapter; 37 import java.awt.event.WindowEvent; 38 39 import org.lwjgl.LWJGLException; 40 import org.lwjgl.opengl.AWTGLCanvas; 41 42 import static org.lwjgl.opengl.GL11.*; 43 import static org.lwjgl.util.glu.GLU.*; 44 45 /** 46 * <p> 47 * Tests AWTGLCanvas functionality 48 * <p> 49 * @version $Revision$ 50 * @author $Author$ 51 * $Id$ 52 */ 53 public class AWTTest extends Frame { 54 55 /** AWT GL canvas */ 56 private AWTGLCanvas canvas0, canvas1; 57 58 private volatile float angle; 59 60 /** 61 * C'tor 62 */ AWTTest()63 public AWTTest() throws LWJGLException { 64 setTitle("LWJGL AWT Canvas Test"); 65 setSize(640, 320); 66 setLayout(new GridLayout(1, 2)); 67 add(canvas0 = new AWTGLCanvas() { 68 int current_height; 69 int current_width; 70 public void paintGL() { 71 try { 72 if (getWidth() != current_width || getHeight() != current_height) { 73 current_width = getWidth(); 74 current_height = getHeight(); 75 glViewport(0, 0, current_width, current_height); 76 } 77 glViewport(0, 0, getWidth(), getHeight()); 78 glClearColor(1.0f, 0.0f, 0.0f, 1.0f); 79 glClear(GL_COLOR_BUFFER_BIT); 80 glMatrixMode(GL_PROJECTION); 81 glLoadIdentity(); 82 gluOrtho2D(0.0f, (float) getWidth(), 0.0f, (float) getHeight()); 83 glMatrixMode(GL_MODELVIEW); 84 glPushMatrix(); 85 glColor3f(1f, 1f, 0f); 86 glTranslatef(getWidth() / 2.0f, getHeight() / 2.0f, 0.0f); 87 glRotatef(angle, 0f, 0f, 1.0f); 88 glRectf(-50.0f, -50.0f, 50.0f, 50.0f); 89 glPopMatrix(); 90 swapBuffers(); 91 repaint(); 92 } catch (LWJGLException e) { 93 throw new RuntimeException(e); 94 } 95 } 96 }); 97 add(canvas1 = new AWTGLCanvas() { 98 int current_height; 99 int current_width; 100 public void paintGL() { 101 try { 102 angle += 1.0f; 103 if (getWidth() != current_width || getHeight() != current_height) { 104 current_width = getWidth(); 105 current_height = getHeight(); 106 glViewport(0, 0, current_width, current_height); 107 } 108 glViewport(0, 0, getWidth(), getHeight()); 109 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 110 glClear(GL_COLOR_BUFFER_BIT); 111 glMatrixMode(GL_PROJECTION); 112 glLoadIdentity(); 113 gluOrtho2D(0.0f, (float) getWidth(), 0.0f, (float) getHeight()); 114 glMatrixMode(GL_MODELVIEW); 115 glPushMatrix(); 116 glTranslatef(getWidth() / 2.0f, getHeight() / 2.0f, 0.0f); 117 glRotatef(2*angle, 0f, 0f, -1.0f); 118 glRectf(-50.0f, -50.0f, 50.0f, 50.0f); 119 glPopMatrix(); 120 swapBuffers(); 121 repaint(); 122 } catch (LWJGLException e) { 123 throw new RuntimeException(e); 124 } 125 } 126 }); 127 addWindowListener(new WindowAdapter() { 128 public void windowClosing(WindowEvent e) { 129 dispose(); 130 System.exit(0); 131 } 132 }); 133 setResizable(true); 134 setVisible(true); 135 } 136 main(String[] args)137 public static void main(String[] args) throws LWJGLException { 138 new AWTTest(); 139 } 140 } 141