The Applet and Threading - blending an animation


How to combine applets and thereading to create a simple animation, why there is flickering and what is solution for it moving object animation. How would you make a rotating banner using applet and therads.

This topic comes at the end of Applet programming, we hope you have gone through both our previous articles on Applet and you also know the threading concepts if not so we suggest you move back a little and study following.

Java Multi Threading - Basic Concepts
Java Multi Threading - Advance Concepts
Java Applet Programming - First Step to Applets
Java Applet Programming - Playing with Utilities

We hope that by now you are quite familiar with the threads as well as applets. So here is the final part of our applet series which will help you with the applets as well as thread concept. We are now going to make a moving banner, which will be moving like a HTML marquee however this will be accomplished by using repaint() method of awt which is commonly known for animation purposes in Java.

The biggest con you'll find in the application is flickering, which occurs because of the repainting but our focus will remain on the login however there is a technique which is used by Java community to avoid the flickering effect, this is known as "Double Buffering". Let us now move on to the program but before that, let us tell you what you are going to learn from this program.

The program creates and applet in which it will take message from the param tag and will render it on the applet window. Then, using the thread we will move it from right to left and when it will cross the left boundary it will restart from far right. We will be using repaint method in the run() to move the applet and message will be moved only for the x-axis so that y-axis remains constant.


import java.applet.Applet;
import java.awt.*;

public class FirstApplet extends Applet implements Runnable{
    private Thread th;
    private int x;
    private boolean flag;
        private String msg,tag;

        public void init(){
            x = 400;
            flag = true;
            th = new Thread(this);
            msg = getParameter("examsmyantra");
            tag = getParameter("tagline");
    }

    public void start(){
            if(!flag){
        th.resume();
        flag = true;
            }
            else{
                th.start();
        }
    }
   
    public void stop(){
            th.suspend();
            flag = false;
    }

    public void destroy(){
    }

    public void run(){
        try{
                    while(true){
                if((x+ msg.length()) < 0){
                            x=500;
                        }
                        x-=2;
            Thread.sleep(100);
            repaint();
                    }
        }
        catch(InterruptedException ie){
            System.out.println(ie);
        }
    }

    public void paint(Graphics g){
        g.drawString(msg, x, 100);
        showStatus(tag);
    }
}

/*
<applet code="FirstApplet" width="500" height="200">
    <param name="examsmyantra" value="www.examsmyantra.com" >
    <param name="tagline" value="Study with Awesomeness" >
</applet>
*/

I hope you noticed the use of suspend and resume method call in the program. These are the methods from the Thread class, the purpose it to temporarily suspend the execution and resume whenever we want. We have used it here because we don't want our thread to keep running even when we are not watching it or using it, we mean when the applet is minimized. So we use suspend and when it starts again by the virtue of resume function.

We have used a flag variable which is just to check the status of the thread so that we only use the proper function. So when you run it you will see the Applet like this.

Moving string

As an exercise we suggest you to draw various other drawing objects like rectangle and circle, and move them instead of string. you may find more about the drawing objects in the class Graphics, use following command to find out the method signature, javap java.awt.Graphics.