/* * Copyright (c) 1993-1997, Silicon Graphics, Inc. * ALL RIGHTS RESERVED * Permission to use, copy, modify, and distribute this software for * any purpose and without fee is hereby granted, provided that the above * copyright notice appear in all copies and that both the copyright notice * and this permission notice appear in supporting documentation, and that * the name of Silicon Graphics, Inc. not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. */ /* * Created on 23/02/2006 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /* * Aargb.c * This program draws shows how to draw anti-aliased lines. It draws * two diagonal lines to form an X; when 'r' is typed in the window, * the lines are rotated in opposite directions. * * * @converted to Java by Wu Shin - Ting * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; public class Aargb extends JFrame implements GLEventListener, KeyListener { public static final Dimension PREFERRED_FRAME_SIZE = new Dimension(200, 200); private static float rotAngle = 0.f; /** Constructor. */ public Aargb() { // init JFrame super ("Aargb - JMDM version "); System.out.println("Constructor"); } /** We'd like to be 200x200, please. */ public Dimension getPreferredSize(){ return PREFERRED_FRAME_SIZE; } /* * METHODS DEFINED BY GLEventListener */ /** Called by drawable to initiate drawing. * Draw 2 diagonal lines to form an X */ public void display(GLAutoDrawable drawable) { System.out.println("Display"); GL gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glColor3f(0.0f, 1.0f, 0.0f); gl.glPushMatrix(); gl.glRotatef(-rotAngle, 0.0f, 0.0f, 0.1f); gl.glBegin (GL.GL_LINES); gl.glVertex2f (-0.5f, 0.5f); gl.glVertex2f (0.5f, -0.5f); gl.glEnd (); gl.glPopMatrix(); gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glPushMatrix(); gl.glRotatef(rotAngle, 0.0f, 0.0f, 0.1f); gl.glBegin (GL.GL_LINES); gl.glVertex2f (0.5f, 0.5f); gl.glVertex2f (-0.5f, -0.5f); gl.glEnd (); gl.glPopMatrix(); gl.glFlush(); } /** Called by drawable to indicate mode or device has changed. */ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){} /** Called after OpenGL is init'ed */ public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); System.out.println("init()"); System.err.println("INIT GL IS: " + gl.getClass().getName()); System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR)); System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER)); System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION)); float[] values = new float[2]; gl.glGetFloatv (GL.GL_LINE_WIDTH_GRANULARITY, values, 0); System.out.println("GL_LINE_WIDTH_GRANULARITY value is" + values[0]); gl.glGetFloatv (GL.GL_LINE_WIDTH_RANGE, values, 0); System.out.println("GL_LINE_WIDTH_RANGE values are" + values[0] + " " + values[1]); gl.glEnable (GL.GL_LINE_SMOOTH); gl.glEnable (GL.GL_BLEND); gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); gl.glHint (GL.GL_LINE_SMOOTH_HINT, GL.GL_DONT_CARE); gl.glLineWidth (1.5f); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Add the key listener drawable.addKeyListener(this); } /** Called to indicate the drawing surface has been moved and/or resized. */ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){ System.out.println("reshape()"); GL gl = drawable.getGL(); gl.glViewport(0, 0, width, height); gl.glMatrixMode (GL.GL_PROJECTION); gl.glLoadIdentity (); if (width <= height) (new GLU()).gluOrtho2D(-1.0f, 1.0f, -1.0f*(float)height/(float)width, 1.0f*(float)height/(float)width); else (new GLU()).gluOrtho2D(-1.0f*(float)width/(float)height, 1.0f*(float)width/(float)height, -1.0f, 1.0f); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); } /* * OUR HELPER METHODS */ // Methods required for the implementation of KeyListener public void keyPressed(KeyEvent e){ System.out.println("Key pressed"); switch (e.getKeyChar()) { case 'r': case 'R': rotAngle += 20.; if (rotAngle >= 360.) rotAngle = 0.f; ((GLCanvas)(this.getContentPane().getComponentAt(0,0))).display(); break; case 27: /* Escape Key */ System.exit(0); break; } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} /** main creates and shows a AAindex-JFrame */ public static void main(String[] args){ Aargb g = new Aargb(); GLCapabilities gl_c = new GLCapabilities(); // Disable double buffer gl_c.setDoubleBuffered(false); // get a GLCanvas GLCanvas canvas = new GLCanvas(gl_c); // add a GLEventListener, which will get called when the // canvas is resized or needs a repaint canvas.addGLEventListener(g); // now add the canvas to the JFrame. Note we use BorderLayout.CENTER // to make the canvas stretch to fill the container (ie, the frame) g.getContentPane().add(canvas, BorderLayout.CENTER); g.pack(); g.setVisible(true); } }