// Sierpinsky Gadget in JavaGL import java.applet.Applet; import java.awt.*; import java.util.Random; // must import javagl.GL.... import javagl.GL; import javagl.GLAUX; public class Gasket extends Applet { // must use GL to use JavaGL..... // and use GLAUX to use the aux functions..... // remember to give GL to initialize GLAUX GL gl = new GL (); GLAUX aux = new GLAUX (gl); float vertices[][]={{(float)0.0,(float)0.0}, {(float)250.0,(float)500.0}, {(float)500.0,(float)0.0}}; float p[] = {(float)75.0,(float)50.0}; Random rand = new Random(0); public void start () { // Do off-line drawing gl.glClearColor((float)1.0,(float)1.0,(float)1.0,(float)1.0); /* white background */ gl.glClear(GL.GL_COLOR_BUFFER_BIT); /*clear the window */ gl.glColor3f((float)1.0,(float)0.0,(float)0.0); /* draw in red */ /* set up viewing */ /* 500 x 500 window with origin lower left */ gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrtho((float)0.0,(float)500.0,(float)0.0,(float)500.0,(float)-1.0,(float)1.0); gl.glMatrixMode(GL.GL_MODELVIEW); int i=0, j=0, k=0; /* computes and plots 5000 new points */ for( k=0; k<5000; k++) { j=Math.abs(rand.nextInt()%3); /* pick a vertex at random */ /* Compute point halfway between vertex and old point */ p[0] = (p[0]+vertices[j][0])/(float)2.0; p[1] = (p[1]+vertices[j][1])/(float)2.0; /* plot new point */ gl.glBegin(GL.GL_POINTS); gl.glVertex2fv(p); gl.glEnd(); } gl.glFlush(); /* clear buffers */ } public void paint (Graphics g) { gl.glXSwapBuffers (g, this); } public void init () { aux.auxInitPosition (0, 0, 500, 500); aux.auxInitWindow (this); } }