Home E-Books Special Effects and Game Development in Java A configurable TextScroller

Special Effects and Game Development in Java(TM) - A configurable TextScroller (TextScroller II)

by Anibal Wainstein

4.2.2 A configurable TextScroller (TextScroller II)

As you may remember, the textscroller i chapter 3 was flimmering. We can fix it now that we know the double buffering technique. At the same time we make it more configurable. Let us start by adding and modifying some variables first:

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;

We have added some variables to set the background and foreground colors. The "delay" variable will be the delay in the animation, as it was in the slideshow applet. Do not forget to paste the getIntegerParameter() method. The init() method must also be altered a bit:

public void init()
//We read the message as a parameter instead at the same
   //time as we read a paramter called "delay" for the
    //picture delay.
    //Please note that we read the values in
     //HEXADECIMAL form (with the base 16)
    //and then create a color.
        new Color(getIntegerParameter("backgroundcolor",16));
    textcolor=new Color(getIntegerParameter("textcolor",16));
    //The following lines are just copied from
    Dimension d=size();

Please note that we get the colors for the applet with the getIntegerParameter() method and specifying that we want to read them in the number base 16, instead of 10. This is done so that these parameters can be specified with hexadecimal numbers by the applet user. The parameters could then look like this:

<APPLET CODE="textscroller2.class" WIDTH=100 HEIGHT=20>
<PARAM name="message" 
value="This is textscroller II, now configurable">
<PARAM name="backgroundcolor" value="ff0000">
<PARAM name="textcolor" value="0000ff">
<PARAM name="delay" value="50">

We have set the background color to red (FF0000) and the text color to blue (0000FF). If you are not used to hexadecimal color values then maybe the following table may be useful:

Red FF0000
Green 00FF00
Blue 0000FF
Cyan 00FFFF
Yellow FFFF00
Magenta FF00FF
Black 000000
Dark Gray 7F7F7F
Light Gray AFAFAF
Orange FF7F00
Dark red 7F0000
Dark green 007F00
Dark blue 00007F

All the drawing in the paint() method must be done in the buffer, instead of directly in the applet screen. When it is finished, then the buffer is displayed on the screen:

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.
      //Draw the message starting from
        //position "x".
        if (x<-400) x=100;

Note that we use the variables "backgroundcolor" and "textcolor" to specify the background color and the text color for the applet. Finally we make a small change in the run() method so that the frame delay for the applet can be set:

public void run()
   while (true)
          //The "delay" variable now makes
        //the delay configurable.
        try {Thread.sleep(delay);}
       catch(InterruptedException e) {}

Now the applet is ready. You should know that the text's length is limited and the applet dimensions must be 100x20 pixels. It does not make sense to change the dimensions right now, when you cannot control the text size anyway. You will learn this in chapter 6. Click here to see the applet.

Now you may feel ready to start your own Java company and flood the Internet with your applets, after this chapter. You would even now manage rather well as a special effects programmer, but there are some pieces left in your education. Some of them we will review in the next chapter which is about mouse messages.




Next Page >>