/* * 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 02/03/2006 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * * Lines.java * This program demonstrates geometric primitives and * their attributes. * * @Translated 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 Lines extends JFrame implements GLEventListener, KeyListener { public static final Dimension PREFERRED_FRAME_SIZE = new Dimension(450, 150); /** Constructor. */ public Lines() { // init JFrame super ("Lines - JMDM version "); System.out.println("Constructor"); } /** We'd like to be 450x150, please. */ public Dimension getPreferredSize(){ return PREFERRED_FRAME_SIZE; } /* * METHODS DEFINED BY GLEventListener */ /** Called by drawable to initiate drawing. */ public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); /* select white for all lines */ gl.glColor3f (1.0f, 1.0f, 1.0f); /* in 1st row, 3 lines, each with a different stipple */ gl.glEnable(GL.GL_LINE_STIPPLE); gl.glLineStipple (1, (byte)0x0101); /* dotted */ drawOneLine (gl, 50.0f, 125.0f, 150.0f, 125.0f); gl.glLineStipple (1, (byte)0x00FF); /* dashed */ drawOneLine (gl, 150.0f, 125.0f, 250.0f, 125.0f); gl.glLineStipple (1, (byte)0x1C47); /* dash/dot/dash */ drawOneLine (gl, 250.0f, 125.0f, 350.0f, 125.0f); /* in 2nd row, 3 wide lines, each with different stipple */ gl.glLineWidth (5.0f); gl.glLineStipple (1, (byte) 0x0101); /* dotted */ drawOneLine (gl, 50.0f, 100.0f, 150.0f, 100.0f); gl.glLineStipple (1, (byte) 0x00FF); /* dashed */ drawOneLine (gl, 150.0f, 100.0f, 250.0f, 100.0f); gl.glLineStipple (1, (byte) 0x1C47); /* dash/dot/dash */ drawOneLine (gl, 250.0f, 100.0f, 350.0f, 100.0f); gl.glLineWidth (1.0f); /* in 3rd row, 6 lines, with dash/dot/dash stipple */ /* as part of a single connected line strip */ gl.glLineStipple (1, (byte)0x1C47); /* dash/dot/dash */ gl.glBegin (GL.GL_LINE_STRIP); for (int i = 0; i < 7; i++) gl.glVertex2f (50.0f + ((float) i * 50.0f), 75.0f); gl.glEnd (); /* in 4th row, 6 independent lines with same stipple */ for (int i = 0; i < 6; i++) { drawOneLine (gl, 50.0f + ((float) i * 50.0f), 50.0f, 50.0f + ((float)(i+1) * 50.0f), 50.0f); } /* in 5th row, 1 line, with dash/dot/dash stipple */ /* and a stipple repeat factor of 5 */ gl.glLineStipple (5, (byte) 0x1C47); /* dash/dot/dash */ drawOneLine (gl, 50.0f, 25.0f, 350.0f, 25.0f); gl.glDisable (GL.GL_LINE_STIPPLE); 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) { System.out.println("init()"); GL gl = drawable.getGL(); 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)); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel(GL.GL_FLAT); 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 (); (new GLU()).gluOrtho2D (0.0, (double) width, 0.0f, (double) height); } /* * OUR HELPER METHODS */ // Method for drawing lines public static void drawOneLine(GL gl, float x1, float y1, float x2, float y2) { gl.glBegin(GL.GL_LINES); gl.glVertex2f ((x1),(y1)); gl.glVertex2f ((x2),(y2)); gl.glEnd(); } // Methods required for the implementation of KeyListener public void keyPressed(KeyEvent e){ System.out.println("Key pressed"); if (e.getKeyChar() == 27){ System.exit(0); } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} /** main creates and shows a Bezcurve-JFrame */ public static void main(String[] args){ Lines g = new Lines(); //Set frame location g.setLocation(100,100); 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); } }