Hello World Applet in Java


Here is everybody's favorite program in Java, this time as an applet:
import java.applet.*;
import java.awt.*;

public class HiWorld extends Applet
{
  public void paint (Graphics g)
  {
    g.drawString ("Hi, world!", 50, 100);
  }
}

This time, however, you need the following additional file:
<html>
<head>
  <title> Hi World Applet </title>
</head>

<body>
  <applet code="HiWorld.class" width=300 height=200></applet>
</body>
</html>

To run this program,

  1. Copy the Java applet exactly as written onto a file named HiWorld.java. (If you are on a PC, you may need to name the file HiWorld.jav.) Do not give the file some other name.

  2. Copy the HTML onto a file. HiWorld.html is the name used in this example, but it really doesn't matter.

  3. Compile the file by putting it in the same directory as the compiler and issuing the command
        javac HiWorld.java
    This should create a file named HiWorld.class (I'm not sure what it is if you only have 3-character extensions.)

  4. Run the file either by issuing the command
        appletviewer HiWorld.html
    or by opening the file HiWorld.html in your favorite browser. (Make sure Java is enabled in your browser.)
    This should print "Hi, World!"
Notes