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

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


import java.applet.*;

import java.awt.*;



public class textscroller extends Applet implements Runnable {



public Thread animationthread = null;

public String message;

public int x=100;



public void init()

{

    message="Basic Course in Special Effects and ";

    message+="Game Development in Java(TM)";

}



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)

{

    //Paint the screen black.

    g.setColor(Color.black);

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



    //Draw the message using white paint starting

    //from position "x".

    g.setColor(Color.white);

    g.drawString(message,x,12);



    //Check that the "x" position for the message

    //is not less than x=-400. If it is, then

    //set "x" to the position 100 (this will make the

    //text invisible).

    if (x<-400) x=100;



    //Decrease "x" with 1 so that the text moves

    //to the left.

    x--;

}



public void update(Graphics g)

{

    paint(g);

}



public void run()

{

    while (true)

    {

        update(getGraphics());

        try {Thread.sleep(50);}

        catch(InterruptedException e) {}

    }

}

}