Java Applets:
In this chapter let us see how to create a Java applet. Some Java applet can be embedded into a HTML file and a dynamic web page could be designed. Whenever we develop an applet, we have to import two important Java groups. They are
Java.awt
Java.applet
Java.awt is known as the Java abstract windows tool kit. A Java.applet is the applet group. We must import all the classes in these two groups as given below:
Import java.awt.*;
Import java.applet.*;
Within statements the asterix symbol (*) is used as wild card. Let us write a small applet to show the message,
Welcomes you to the java world
The names of the class are welcome applet, that is a subclass of applet class the given is the java code for the applet.
Import java.awt.*;
Import java.applet.*;
public class welcomeapplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("welcomes you to the java world " , 20,20);
}
}
Let us see how variant this is from out first program class welcomes. The import statements have already been explained. A statement, public class welcomesapplet extends Applet declares which welcomesApplet is a new class that is a subclass of Applet class. That means that welcomesApllet ia an applet. To this class we declare a method known as paint that has only one statement. A statement g.drawString ("welcomes you to the java world",20,20) describes us in which the message welcomes you to the java world will be printed at the position 20,20 Graphics is a class that is available in java and drawString() is a behaviour of Graphics class.
Whenever a java applet is ready we have to first compile it by using the compiler javac. For our program we compile using the comman. Javac welcomes applet .java.
When the compilation process is over, the class file is ready and then it could be embedded in an HTML file.
Before it embedded in a HTML. File, we can verify the output the output using a program known as applet viewer. We embed the applet in a HTML page and then open it along with an internet browser. For instance we can embed the above applet welcomes applet in a HTML page as given below.
<html>
<head>
<title>
In a HTML document Embedding an applet
</title>
<body>
<applet code= welcomesapplet.class width=300 height=100>
</applet>
</body>
</html>
when we open this HTML document by using Internet explorer we get the window along with welcome message.