/**flyingWords.java modified by gregvan *based on work by Dr.Sharon Tuttle *Humboldt State University, California,USA * RECOMMENDED SIZE: height 250, width 600 * you may change the words by modifying * the html on your webpage... parameters * pass what you want to say to the applet * * * * * **/ import java.applet.*; import java.awt.*; public class flyingWords6 extends Applet implements Runnable { //----- // data fields //----- int rr,gg,bb;//color variables int x, y; //locations int z; int a; int zz=1; //initial value of up-down counters int xx=1; int yy=1; int aa=1; String yourWords; String moreWords; String stationaryWords; Thread myRunner; 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() { // get the words from the webpages html yourWords=getParameter("yw"); if (yourWords==null){yourWords="";} moreWords=getParameter("mw"); if (moreWords==null){moreWords="";} stationaryWords=getParameter("sw"); if (stationaryWords==null){stationaryWords="";} Font chosenFont = new Font("Dialog", Font.PLAIN, 30); setBackground(Color.black); // 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) { //new random numbers to start animation x=(int)(Math.random() *300); y=(int)(Math.random() *100); z=(int)(Math.random() *300); a=(int)(Math.random() *100); repaint(); try { // sleep for 1 second (1000 msec = 1 sec) Thread.sleep(100); } 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) { for(int i=0; i<5;i++) { //color changer rr+16 makes it stripey rr=rr+4; 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 location of the words //classic up-down counters //we use x and z to define where to start the words //sometimes, zz=-1 and xx=1 //then the words stop moving... //y and a are used for vertical position... z=z+zz; if(z>200){zz=-1;} if(z<-50){zz=3;} a=a+aa; if(a>150){aa=-1;} if(a<-20){aa=2;} x=x+xx; if(x>200){xx=-2;} if(x<-50){xx=1;} y=y+yy; if(y>150){yy=-3;} if(y<0){yy=1;} Color eNew = new Color(rr, gg, bb); bufferGraphics.setColor(eNew); bufferGraphics.setFont(new Font("Serif",Font.PLAIN,36)); //use the parameters sent from the webpage // the small moving words bufferGraphics.drawString(""+yourWords , x+z,y+a); bufferGraphics.drawString(""+moreWords , x+z+15,y+a+30); //this color is different from the moving words //because i switched the rr and gg Color dNew = new Color(gg, rr, bb); bufferGraphics.setColor(dNew); bufferGraphics.drawString(""+stationaryWords ,15,40); }//end of for loop // [1]"then copy the off-screen buffer onto the screen" g.drawImage(buffer, 0, 0, this); } }