How to create a Java applet
Make sure that both files are located in the same directory (folder)!
- Write source code and save it as .java file.
A sample code (Hello.java) might look like:
import java.applet.Applet;
import java.awt.*;
public class Hello extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello",0,20);
}
}
- Compile the source code.
If you compile Hello.java, you will get Hello.class
(You can do it from
the command line prompt javac filename.java or using bluej )
- Create a simple HTML file that contains <APPLET> tag.
A sample HTML might look like:
<HTML>
<HEAD>
<TITLE>Hello Applet</TITLE>
</HEAD>
<BODY>
<APPLET code="Hello.class" width=400 height=400>
</APPLET>
</BODY>
</HTML>
Note: You may use
any text editor to create html files. You may also give any name to the html
file, as long as the extension is .html or .htm
- Open the html file in one of the following two ways: