Week 8

Back ] Up ] Next ]

Discussion group, leave your messages

Home
Week 1
Week 2
Week 3
Week 4
Week 5
Week 6
Week 7
Week 8
Week 9
Week 10
Week 11
Week 12

How to create a Java applet

Make sure that both files are located in the same directory (folder)!

  1. 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);
}

}

  1. 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 )

  1. 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

  1. Open the html file in one of the following two ways:
bulletOpen the html file from any Web browser such as Netscape or Internet Explorer using the option File, Open. or
bulletRun the appletviewer from command-line interface. For example, if the above sample html file is saved as Hello.html, enter the following command in DOS-Prompt

appletviewer Hello.html

 

Back to CS161 JAVA Homepage
This page was last modified January 10, 2002
wmorales@pcc.edu