|
All the very commonly used applets are basically derived by overriding the set of methods which provide us the basic mechanism by which the browser or the applet viewer interfaces to the applet and controls its execution. Let’s prove the this written statement by considering the case of few methods which an applet uses:
init( ), start( ), stop( ), and destroy( ). These methods are defined by Applet.
Lets us discuss now how each of these methods shown in the applet skeleton actually works and what is each one of their role
Immediately, the moment an applet begins the AWT calls these methods in the following sequence: first the init () method is called and here we initialize the variables and this method is called only once during the run time of the applet, then the start ( ) method is called and it as its name suggest it restarts the applet after it has been stopped and this method is called each time an applet’s HTML document is displayed on the screen and finally and the paint ( ) method is called and it is called every time when we need the output to be drawn. This method has one parameter of type graphics. It describes the graphics environment is which the applet is actually running.
And at the time of terminating the applet it first calls stop ( ) method which closes the applet’s HTML document. this method basically stops all the threads which are executing at that point of time and are not required by the applet and finally the destroy ( ) method is called which makes sure that the applet which we have been executing can be removed completely from the memory and at this point of time we need to free all the pre occupied resources which the applet might be using.
Simple Applet Display methods: As we have been continuously stating that applets are displayed in a window which is supported by AWT to perform the input and output operations, we can use drawstring ( ) to output a string to an applet. The syntax for which is defined below:
void drawString(String message, int x, int y)
Where, message is the string to be output beginning at x ,y.
To set the background colour for an applet’s window we should use setBackground ( )
Method, the syntax for this is:
void setBackground(Color newColor)
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class AppletSetBackgroundExample extends Applet {
public void paint(Graphics g) {
// Color is defined using RGB. // R - Red, G - Green, B - Blue.
// These are primary colors and other color can be drived
// through the right combination of these. Let us say we
// want a green background. Value range is 0-255.
Color color = new Color(0, 255, 0);
setBackground(color);
}
}
And to set the foreground use setForeground ( ) method.
The syntax for this is:
void setForeground(Color newColor)
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class AppletSetBackgroundExample extends Applet {
public void paint(Graphics g) {
// Color is defined using RGB. // R - Red, G - Green, B - Blue.
// These are primary colors and other color can be drived
// through the right combination of these. Let us say we
// want a green background. Value range is 0-255.
Color bgcolor = new Color(0, 255, 0); // green
Color border = new Color(0, 0, 0); // black
Color fgcolor = new Color(255, 0, 0); // red
setBackground(bgcolor);
setForeground(fgcolor);
g.draw3DRect(20, 20, 200, 100, true);
g.drawString("MESHPLEX", 90, 70);
}
}
|