Creating a Java Applet

Creating a Java Applet

Creating applets is different from creating a simple application, because Java applets run and are displayed inside a Web page with other page elements and as such have special rules for how they behave. Because of these special rules for applets in many cases (particularly the simple ones), creating an applet may be more complex than creating an application.For example, to do a simple Hello World applet, instead of merely being able to print a message,you have to create an applet to make space for your message and then use graphics operations to paint the message to the screen.
The Hello World applet
import java.awt.Graphics;
class HelloWorldApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
drawString(“Hello world!”, 5, 25);
}
}

Now, compile the applet just as you did the application, using  javac, the Java compiler.
javac HelloWorldApplet.java
Again, just as for applications, you should now have a file called HelloWorldApplet.class in your
HTML directory.To include an applet in a Web page, you refer to that applet in the HTML code for that Webpage. Here, you create a very simple HTML file in the HTML directory
The HTML with the applet in it.
<HTML>
<HEAD>
<TITLE>Hello to Everyone!</TITLE>
</HEAD><BODY>
<P>My Java applet says:
<APPLET CODE=”HelloWorldApplet.class” WIDTH=150 HEIGHT=25>
          </BODY>
      </HTML>
If you don’t have a Web browser with Java capabilities built into it, you can use the appletviewer program to view your Java applet. To run appletviewer, just indicate the path to the HTML file on the command line:
                             appletviewer HTML/HelloWorldApplet.html
error: Content is protected !!