A Simple Timer Applet

In this program we implement a timer using Applet class. This applet also implements Runnable. This is necessary, since the applet will be creating a second thread of execution that will be used to update the time every second. Inside init( ), the foreground and background colors of the applet are set. After initialization, the AWT run-time system calls start( ) to start the applet running. Inside start( ), a new thread of execution is created and assigned to the Thread variable clock. Then, the boolean variable value, which controls the execution of the applet, is set to false. Next, the thread is started by a call to t.start( ). t.start( ) calls a method defined by Thread class, which causes run( ) to begin executing. It does not cause a call to the version of start( ) defined by Applet. These are two separate methods.

Inside run( ), the time is updated is every second. Between each updation, a call to repaint( ) is made. This eventually causes the paint( ) method to be called and the current time in hours , minutes and seconds is displayed. Between each iteration, run( ) sleeps for a quarter of a second. The net effect of run( ) is that the time in hours , minutes and seconds is displayed each second.

If a browser is displaying the applet when a new page is viewed, the stop( ) method is called, which sets value to true, causing run( ) to terminate. This is the mechanism used to stop the thread when its page is no longer in view. When the applet is brought back into view, start( ) is once again called, which starts a new thread to execute the timer.

A Simple Banner Applet

Banner extends Applet and implements Runnable. This is necessary, since the applet will be creating a second thread of execution that will scroll the banner. Inside init( ), the foreground and background colors of the applet are set.

After initialization, the AWT run-time system calls start( ) to start the applet running. Inside start( ), a new thread of execution is created and assigned to the Thread variable t. Then, the boolean variable stopFlag, which controls the execution of the applet, is set to false. Next, the thread is started by a call to t.start( ). t.start( ) calls a method defined by Thread, which causes run( ) to begin executing. It does not cause a call to the version of start( ) defined by Applet. These are two separate methods.Inside run( ), the characters in the string contained in msg are repeatedly rotated left. Between each rotation, a call to repaint( ) is made. This eventually causes the paint( ) method to be called and the current contents of msg is displayed. Between each iteration, run( ) sleeps for a quarter of a second. The net effect of run( ) is that the contents of msg is scrolled right to left in a constantly moving display. The stopFlag variable is checked on each iteration. When it is true, the run( ) method terminates. If a browser is displaying the applet when a new page is viewed, the stop( ) method is called, which sets stopFlag to true, causing run( ) to terminate. This is the mechanism used to stop the thread when its page is no longer in view. When the applet is brought back into view, start( ) is once again called, which starts a new thread to execute the banner.

A Bouncing Ball example

In the following example class bounce extends Applet and implements Runnable. Runnable is implemented to create a thread which will help in changing the position of the ball and hence check for the boundary conditions of the applet window. If the boundary conditions are met the ball is traced in opposite path else the ball proceeds. The change in position is drawn by paint() by continuous calling of repaint().

A Patterned Circle Example

We have tried a different types of pattern making in console based programs. But an Applet can give a nice graphical pattern using circles. The following code try to make pattern using black and white balls. To change the number of rows and columns change the limit of i and j i.e change the number 3 to the desired number.

Code:

import java.applet.*;
import java.awt.*;
/* <applet code="pattern" height=200 width=300>
</applet>
*/
/*
A Special Note:
While specifying an oval we have to pass the top-left corner (x,y) co-ordinates of the
bounding box and the height and width of the bounding box
A bounding box is defined as the smallest rectangle which can completely contain the oval
If the height and width is same then a circle is drawn
*/
public class pattern extends Applet
{
public void paint(Graphics g)
{
int i,j;
for(i=0;i<3;i++) // Three rows
{
for(j=0;j<3;j++) //Three columns
{
if((i+j)%2==0)
//if sum of i and j is even then draw empty circle else draw filled in circle
g.drawOval(i*60,j*60,30,30);
else
g.fillOval(i*60,j*60,30,30);
}
}
}
}

Output:-

Concentric Circle Example

Code:

import java.applet.*;
import java.awt.*;
/*<applet code="ConcCircles" height=250 width=300>
</applet>
*/
/*
A Special Note:
While specifying an oval we have to pass the top-left corner (x,y) co-ordinates of the bounding box and the height and width of the bounding box
A bounding box is defined as the smallest rectangle which can completely contain the oval
If the height and width is same then a circle is drawn
*/
public class ConcCircles extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.RED); //Set the colour to red
g.drawOval(50,50,150,150); // Refer to special note above for explanation of parameters
g.setColor(Color.BLUE); // Set the colour to blue
g.drawOval(75,75,100,100); // 100 pixel diameter circle is drawn
g.setColor(Color.GREEN); //Set the colour to green
g.drawOval(100,100,50,50);
}
}

Output:-

A Simple Image Applet

This example illustrates how to display images in an applet. MediaTracker is a utility class of java.awt.* package that tracks the status of a number of media objects. This type of object can also include images and audio clips. The addImage() method of the MediaTracker class is used after creating an instance of this class. The getImage() method is used to return the image for the object ( img ) of the Image class taking two arguments, first is getCodeBase() and another is image name. Syntax of the addImage() function is MediaTracker.addImage(img, x, y, x1, y1).
Arguments are :
img - image name type of Image.
x - lower X - Coordinate type of int.
y - lower Y - Coordinate type of int.
x1 - upper X - Coordinate type of int.
y1 - upper Y - Coordinate type of int.

A simple Line Animation Example

This applet implements Runnable as it creates a separate stream of execution, to perform a background task. The body of the thread's code is in the run() function which is executed after called start() method. In this case, the purpose of the thread is to increment the variable i once every 1000 milliseconds, and cause the applet to redraw itself. The result is a simple animation.