Friday 19 April 2013

How I Created a Simple Java Applet

I used vi to create a file like this:

UBUNTU > cat Andrew_Was_Here.java
// A simple applet.
import java.awt.Graphics;
public class Andrew_Was_Here
extends java.applet.Applet
{
public void paint(Graphics g)
  {
  g.drawString("Andrew Was Here!",20,40);
  }
}
UBUNTU >

I compiled it:

UBUNTU > javac Andrew_Was_Here.java
UBUNTU

Applets run within a browser so I wrote a few lines of HTML:

UBUNTU > cat Andrew_Was_Here.html
<html>
<head>
<title>Andrew Was Here</title>
</head>
<body>
<applet name="Andrew Was Here"
 code="Andrew_Was_Here.class"
 width = 400 height = 200>
</applet>
</body>
</html>
UBUNTU >


Then I typed the following command at the UBUNTU prompt:

appletviewer Andrew_Was_Here.html

... and the applet was displayed like this. As usual, click on the image to enlarge it and bring it into focus:

I was also able to run it in a browser as shown below: