/**myArtTrip3.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 myArtTrip3 extends Applet implements Runnable { //----- // data fields //----- int rr,gg,bb;//color variables int x, y; int z=0; int a=0; int zz=1; int xx=1; int yy=1; int aa=1; 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("myArtTrip3 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+16 makes it stripey rr=rr+16; 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 //classic up-down counters z=z+zz; if(z>600){zz=-1;} if(z<0){zz=1;} a=a+aa; if(a>400){aa=-2;} if(a<0){aa=1;} x=x+xx; if(x>600){xx=-3;} if(x<00){xx=1;} y=y+yy; if(y>400){yy=-1;} if(y<0){yy=2;} //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); bufferGraphics.drawLine(50+ x, y, z,100+a); bufferGraphics.drawLine( 550-x,y,600-z,100+a); //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( x, z, y,a); bufferGraphics.drawLine( 600-x,z,600-y,a); bufferGraphics.drawLine(50+ x, z, y,100+a); bufferGraphics.drawLine( 550-x,z,600-y,100+a); }//end of for loop //draw a persistant peace sign in black Color dNew = new Color(0, 0, 0); bufferGraphics.setColor(dNew); for (int j=0; j<10; j++) { bufferGraphics.drawOval(200-j,100-j,200+j+j,200+j+j); bufferGraphics.drawLine(300-j,100,300+j,300); bufferGraphics.drawLine(300+j,100,300-j,300); bufferGraphics.drawLine(300,200,380+j,280-j); bufferGraphics.drawLine(300,200,220-j,280-j); } // [1]"then copy the off-screen buffer onto the screen" // (think: why 0,0 for coordinates, here?) g.drawImage(buffer, 0, 0, this); } }