Home E-Books Special Effects and Game Development in Java The Applet TextScroller II

Special Effects and Game Development in Java(TM) - The Applet TextScroller II

 

 

 

 

import java.applet.*;

import java.awt.*;



public class textscroller2 extends Applet implements Runnable {



public Thread animationthread = null;

public String message;

public int x=100;

Image bufferimage;

Graphics bufferg;



//The following colors are fetched as

//HEXADECIMAL parameters and are used

//to set the background and text color

//of the applet.

Color backgroundcolor,textcolor;



//The "delay" variable is used as a delay

//for each picture frame.

int delay=0;



public void init()

{

    //We read the message as a paramter instead at the same

    //time as we read a paramter called "delay" for the

    //picture delay.

    message=getParameter("message");

    delay=getIntegerParameter("delay",10);



    //Please note that we read the values in

    //HEXADECIMAL form (with the base 16)

    //and then create a color.

    backgroundcolor=new Color(getIntegerParameter("backgroundcolor",16));

    textcolor=new Color(getIntegerParameter("textcolor",16));



    //The following lines are just copied from

    //slideshow2.java

    Dimension d=size();

    bufferimage=createImage(d.width,d.height);

    bufferg=bufferimage.getGraphics();

}



public int getIntegerParameter(String name, int base)

{

    String value=getParameter(name);

    int intvalue;

    try {intvalue=Integer.parseInt(value,base);}

    catch (NumberFormatException e) {return 0;}

    return intvalue;

}



public void start()

{

    if (animationthread == null)

    {

         animationthread = new Thread(this,"animationthread");

         animationthread.start();

    }

}



public void stop()

{

    if ((animationthread != null) && animationthread.isAlive())

        animationthread.stop();

    animationthread = null;

}



public synchronized void paint(Graphics g)

{

    //It is always a good idea to check

    //that bufferg has been initialized.

    if (bufferg!=null)

    {

        //Paint the screen with the fetched

        //background color.

        bufferg.setColor(backgroundcolor);

        bufferg.fillRect(0,0,100,20);



        //Draw the message starting from

        //position "x"

        bufferg.setColor(textcolor);

        bufferg.drawString(message,x,12);

        g.drawImage(bufferimage,0,0,this);

        if (x<-400) x=100;
x--;
}
} public void update(Graphics g) { paint(g); } public void run() { while (true) { update(getGraphics()); //The "delay" variable now makes //the delay configurable. try {Thread.sleep(delay);} catch(InterruptedException e) {} } } }