/**myArtTrip.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 myArtTrip 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("myArtTrip 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) { //x += MOVE_INCR; repaint(); // to see train at new x-location try { // pause for .1 second between // repaintings Thread.sleep(100); // .1 second pause } catch (InterruptedException e) { } } //end while x <= END_X } //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) { //bufferGraphics.drawImage(myTrain, x, y, this); for (int i=0;i<60;i++) { rr=rr+1; if(rr>255){rr=rr-255;} gg=gg+3; if(gg>255){gg=gg-255;} bb=bb+5; if(bb>255){bb=bb-255;} z=z+5; if(z>600){z=0;} a=a+6; if(a>400){a=0;} x=x+3; if(x>600){x=0;} y=y+4; if(y>400){y=0;} Color cNew = new Color(rr, gg, bb); bufferGraphics.setColor(cNew); bufferGraphics.drawLine( x, y, z,a); bufferGraphics.drawLine( 600-x,y,600-z,a); bufferGraphics.setColor(bNew); bufferGraphics.drawLine( x, 400-y, z,400-a); bufferGraphics.drawLine( 600-x,400-y,600-z,400-a); } // [1]"then copy the off-screen buffer onto the screen" // (think: why 0,0 for coordinates, here?) g.drawImage(buffer, 0, 0, this); } }