Tuesday, October 21, 2014

Animated Banner program with Java



Save this file as: bannar.java
……………………………………………………………………………………………
import java.awt.*;
import java.applet.*;
public class bannar extends Applet implements Runnable
{

        String msg="Hello World";
        Thread t;
        int state;
        boolean flag;
        public void init()
        {

        }
        public void start()
        {
                t=new Thread(this);
                flag=false;
                t.start();
        }
        public void run()
        {
                char ch;
                for(;;)
                {

                        try{

                                repaint();
                                Thread.sleep(250);
                                ch=msg.charAt(0);
                                msg=msg.substring(1,msg.length());
                                msg+=ch;
                                if(flag)
                                break;

                        }catch(InterruptedException e){}

                }
        }
        public void stop()
        {
                flag=true;
                t=null;

        }
        public void paint(Graphics g)
        {
                g.drawString(msg,30,30);
        }


}
……………………………………………………………………………………
compile this file:   javac bannar.java
……………………………………………………………………………………..
html file : bannar.html
<applet code ="bannar.class"
width=200
 height=200>
</applet>

……………………………………………………………………………………..
Run it: appletviewer bannar.html
……………………………………………………………………………………..
output

                                 

No comments:

Post a Comment