/**myArtTrip2.java modified by gregvan v1.1 *based on work by Dr.Sharon Tuttle *Humboldt State University, California,USA * RECOMMENDED SIZE: height 400, width 600 **/ import java.applet.*; import java.awt.*; public class myArtTrip2 extends Applet implements Runnable { //----- // data fields //----- int rr,gg,bb;//color variables int x, y; int z=0; int a=0; Thread myRunner; Color cNew = new Color(125, 0, 255); Color bNew = new Color(0, 0, 0); Image buffer; // [1]"the off-screen image for double-buffering" Dimension appletSize; // [1]"the size of the applet" Graphics bufferGraphics; // [1]"a Graphics object for the buffer" //----- // methods //----- public void init() { Font chosenFont = new Font("Dialog", Font.PLAIN, 30); setBackground(Color.black); System.out.println("myArtTrip2 Version 1.0"); // get size of applet (appletSize is a Dimension object) appletSize = this.getSize(); // buffer is now created to be the same size as the applet buffer = this.createImage(appletSize.width, appletSize.height); // get a Graphics object for buffer bufferGraphics = buffer.getGraphics(); } public void start() { if (myRunner == null) { // not bothering to name the thread, this time; myRunner = new Thread(this); myRunner.start(); } } public void run() { Thread executingThread; executingThread = Thread.currentThread(); while (myRunner == executingThread) { repaint(); try { // sleep for 1 second (1000 msec = 1 sec) Thread.sleep(1000); } catch(InterruptedException e) { } while (myRunner != null) { repaint(); try { // pause for .1 second between // repaintings Thread.sleep(100); // .1 second pause } catch (InterruptedException e) { } } } //end while thread executing } //suspends execution when user leaves page public void stop() { if (myRunner != null) { myRunner = null; } } // VERY IMPORTANT --- [1] "It is important to override this method // like this for double-buffering" --- this new version of update() // doesn't clear the screen, it just calls paint() immediately. public void update(Graphics g) { paint(g); } // now, when double-buffering, here in paint() I will not paint on // Graphics object g --- I will paint on my buffer's Graphics object, // instead public void paint(Graphics g) { //a for loop to paint 10 lines for (int i=0;i<10;i++) { //color changer rr=rr+1; if(rr>255){rr=rr-255;} gg=gg+2; if(gg>255){gg=gg-255;} bb=bb+3; if(bb>255){bb=bb-255;} //move the endpoints of the lines z=z+1; if(z>600){z=0;} a=a+2; if(a>400){a=0;} x=x+3; if(x>600){x=0;} y=y-1; if(y<0){y=400;} //apply the new numbers for color Color cNew = new Color(rr, gg, bb); bufferGraphics.setColor(cNew); //draw the lines bufferGraphics.drawLine( x, y, z,a); bufferGraphics.drawLine( 600-x,y,600-z,a); //draw the lines with x and z switched bufferGraphics.drawLine( z, y, x,a); bufferGraphics.drawLine( 600-z,y,600-x,a); //draw the lines with x and z switched bufferGraphics.drawLine( z, y, a,x); bufferGraphics.drawLine( 600-z,y,600-a,x); //bNew is black and white because all values are the same Color bNew = new Color(rr, rr, rr); bufferGraphics.setColor(bNew); //draw lines with x-y switched and a-z switched bufferGraphics.drawLine( y, 400-x, a,400-z); bufferGraphics.drawLine( 600-y,400-x,600-a,400-z); }//end of for loop // [1]"then copy the off-screen buffer onto the screen" // (think: why 0,0 for coordinates, here?) g.drawImage(buffer, 0, 0, this); } }