Banner Applet is an applet application which continuously rotates the text chars after given time interval.

For Example,

This is Demo.
his is Demo.T
is is Demo.Th

How to write program for this?

  1. We need applet first. Will follow the applet life cycle
    import java.applet.*;
    import java.awt.*;
    
    public class bannerDemo extends Applet
    {
    	public void init()
    	{
    	}
    	public void paint(Graphics g)
    	{
    	}		
    }
    /*
    <applet code="bannerDemo" width="500" height="150"></applet>
    */
    
  2. We want to draw a String on applet area. Which can be done by calling drawString() method of Graphics class inside paint() method.
    	public void paint(Graphics g)
    	{
    		Font f = new Font("Arial",Font.BOLD,36);
    		g.setFont(f);
    		g.drawString("Ankit Virparia (ankit.co)",50,75);
    	}	
    
  3. We want continuous change in the text which we have printed so, Will go for multithreading.
    public class bannerDemo extends Applet implements Runnable
    {
    	Thread t;
            public void run()
    	{}
    }
    
  4. Here one extra thread will be given a responsibility to update the text given (which is stored in a global variable – I mean common between paint() and run() methods)
    public class bannerDemo extends Applet implements Runnable
    {
    	Thread t;
    	String msg = "Ankit Virparia (ankit.co)";
    	
    	public void run()
    	{
    		try
    		{
    			while(true)
    			{
    				t.sleep(1000);
    				char c = msg.charAt(0);
    				String tmp = msg.substring(1,msg.length());
    				msg = tmp + c;
    				repaint();
    			}
    		}
    		catch(Exception e)
    		{
    			System.out.println(e);	
    		}
    	}
    		
    	public void paint(Graphics g)
    	{
    		Font f = new Font("Arial",Font.BOLD,36);
    		g.setFont(f);
    		g.drawString(msg,50,75);
    	}	
    	
    }
    
  5. We need to start the thread in the beginning. Which can be written in init() method.
    	Thread t;
    	public void init()
    	{
    		t = new Thread(this);
    		t.start();
    	}
    
  6. paint() is invoked only when a user does GUI related change. But here in our case we want to invoke paint() at the interval of 1 second.
    
    
  7. We are ready to go!

Program for Banner Applet

File Name:bannerDemo.java

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

public class bannerDemo extends Applet implements Runnable
{
	Thread t;
	String msg = "Ankit Virparia (ankit.co)";
	public void init()
	{
		t = new Thread(this);
		t.start();
	}
	
	public void run()
	{
		try
		{
			while(true)
			{
				t.sleep(1000);
				char c = msg.charAt(0);
				String tmp = msg.substring(1,msg.length());
				msg = tmp + c;
				repaint();
			}
		}
		catch(Exception e)
		{
			System.out.println(e);	
		}
	}
		
	public void paint(Graphics g)
	{
		Font f = new Font("Arial",Font.BOLD,36);
		g.setFont(f);
		g.drawString(msg,50,75);
	}	
	
}

/*
<applet code="bannerDemo" width="500" height="150"></applet>
*/

p5