import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.*; import java.util.*; import java.io.*; import java.util.*; import gl4java.GLContext; import gl4java.awt.GLCanvas; import gl4java.utils.glut.*; public class triangle extends Applet { triangleCanvas canvas = null; /** * void init() * * Initialise the applet. */ public void init() { Dimension d = getSize(); //We will use BorderLayout to layout the applet components setLayout(new BorderLayout()); //Create our canvas and add it to the center of the applet canvas = new triangleCanvas(d.width, d.height); add("Center", canvas); } /** * void start() * * Start the applet. */ public void start() { } /** * void stop() * * Stop the applet. */ public void stop() { } /* main, for when it is run from the command line. * Set up a window for the "applet" to run in. */ public static void main( String args[] ) { triangle applet = new triangle(); Frame f = new Frame("triangle"); f.addWindowListener( new WindowAdapter() { public void windowClosed(WindowEvent e) { System.exit(0); } public void windowClosing(WindowEvent e) { windowClosed(e); } }); f.setLayout(new BorderLayout()); f.add("Center", applet); applet.setSize(500,300); applet.init(); applet.start(); Dimension ps = applet.getPreferredSize(); f.setBounds(-100,-100,99,99); f.setVisible(true); f.setVisible(false); f.setVisible(true); Insets i = f.getInsets(); f.setBounds(0,0, ps.width+i.left+i.right, ps.height+i.top+i.bottom); f.setVisible(true); } /* GLCanvas class for this application. * We would use GLAnimCanvas because if we wanted the canvas * to be constantly redrawn. */ private class triangleCanvas extends GLCanvas implements MouseListener { int windW, windH; public triangleCanvas(int w, int h) { super(w, h); } /** * void preInit() * * Called just BEFORE the GL-Context is created. */ public void preInit() { //We want double buffering doubleBuffer = true; //But we dont want stereo view stereoView = false; } /** * void init() * * Called just AFTER the GL-Context is created. */ public void init() { windW = getSize().width; windH = getSize().height; addMouseListener(this); } /** * void reshape(int width, int height) * * Called after the first paint command and when the window is * resized by the user */ public void reshape(int width, int height) { windW = (int)width; windH = (int)height; // set the viewport to the entire window gl.glViewport(0, 0, windW, windH); } /** * void display() * * Draw to the canvas. */ public void display() { //Ensure GL is initialised correctly if( glj.gljMakeCurrent() == false ) { System.out.println("problem in use() method"); return; } // save the current matrix: good practice, even though we // know it's not used for anything in our program gl.glPushMatrix(); // reset the project matrix gl.glMatrixMode(GL_PROJECTION); gl.glLoadIdentity(); glu.gluOrtho2D(-5, 5, -5, 5); gl.glMatrixMode(GL_MODELVIEW); //Clear The Screen gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClear(GL_COLOR_BUFFER_BIT); // draw our "scene": a single triangle and some lines // first, draw a polygon for our triangle. All the points are // used to create a single polygon gl.glColor3f(0.4f, 0.7f, 0.1f); gl.glBegin(GL_POLYGON); gl.glVertex2f(1.0f, 1.0f); gl.glVertex2f(2.0f, -1.0f); gl.glVertex2f(-2.0f, 0.0f); gl.glEnd(); // next, draw some colored lines. Each pair of "vertex" commands // creates a new line gl.glBegin(GL_LINES); gl.glColor4f(0f, 0f, 1f, 1f); gl.glVertex3i( -1, -1, 0); gl.glVertex3i( 1, 1, 0); gl.glColor4f(0f, 1f, 0f, 1f); gl.glVertex3i( -1, 1, 0); gl.glVertex3i( 1, -1, 0); gl.glColor4f(1f, 0f, 0f, 1f); gl.glVertex3i( -1, 0, 0); gl.glVertex3i( 1, 0, 0); gl.glColor4f(1f, 1f, 1f, 1f); gl.glVertex3i( 0, -1, 0); gl.glVertex3i( 0, 1, 0); gl.glEnd(); // restore the old matrix gl.glPopMatrix(); // swap buffers to display what we just rendered glj.gljSwap(); glj.gljFree(); } /* * Methods required for the implementation of MouseListener */ public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mousePressed(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mouseClicked(MouseEvent evt) { int x1 = evt.getX(); int y1 = evt.getY(); repaint(); } } }