Back to July Calendar 

Previous Entry Next Entry→

July 24, 2005
 

I've refigured out (after an interval of some years) how to compile .java code.  I wrote a little program in Notepad that consists of the following:

// a first java program
class Pun{
   public static void main(String argv[]) {
     System.out.println("Java the PUN!!");
  }
}

Then this html code: <applet code="Pun.class" codebase="applets" width=120 height=20></applet>

This doesn't actually work...as the gentle reader may observe. It does compile as run as shown at right, but it's not an applet, its just a stand alone, ver simple java console program.

C:\PROGRA~1\Java\JDK15~1.0_0\bin>javac pun.java

C:\PROGRA~1\Java\JDK15~1.0_0\bin>dir/w

Directory of C:\PROGRA~1\Java\JDK15~1.0_0\bin

[.] [..] appletviewer.exe apt.exe
beanreg.dll extcheck.exe HtmlConverter.exe idlj.exe
jar.exe jarsigner.exe java.exe javac.exe
javadoc.exe javah.exe javap.exe javaw.exe
javaws.exe jconsole.exe jdb.exe jps.exe
jstat.exe jstatd.exe keytool.exe kinit.exe
klist.exe ktab.exe native2ascii.exe orbd.exe
pack200.exe packager.exe policytool.exe Pun.class
pun.java rmic.exe rmid.exe rmiregistry.exe
serialver.exe servertool.exe tnameserv.exe unpack200.exe

C:\PROGRA~1\Java\JDK15~1.0_0\bin>java Pun
Java the PUN!!

 

 

I've finally got a java applet working, as shown at right.  This is Mondrian.class which I've located in a subdirectory of the current directory called applets, so that the html code used to access it is <applet code="Mondrian.class" codebase="applets" width=300 height=300></applet>.  This is not a bitmap type image, this is a java applet which draws the image by following java code below.

Every java applet is a subclass of the Applet class and, as such, inherits all of Applet's public methods. Mondrian overrides some of these: init(), start(), paint(), stop() and destroy(). The import java.applet.* statement gains access to the unqualified class names for all the classes in the package java.applet (what my "Java the PUN!!" program is missing.) 

So when Hamlet from Denmark loads this html file from the internet, the Mondrian class file is loaded by the JRE (Java Runtime Enverionment) bytecode loader in Hamlet's browser.  After ensuring that the code won't hurt his computer, the methods in Mondrian.class are invoked in order:
init() initializes the applet
start() starts the execution
paint() controls what graphics appear
stop() is called when the calling web page is hidden
destroy() frees up resources used by the applet.
 

 

import java.applet.*;
import java.awt.*;
public class Mondrian extends Applet {
  public void init() {
    System.out.println(">> init <<");
    setBackground(Color.black);
  }
  public void start() {
    System.out.println(">> start <<");
  }
  public void paint(Graphics g) {
    System.out.println(">> paint <<");
    g.setColor(Color.yellow);
    g.fillRect(0,0,90,90);
    g.fillRect(250,0,40,190);
    g.fillRect(80,110,100,20);
    g.setColor(Color.blue);
    g.fillRect(80,200,220,90);
    g.fillRect(100,10,90,80);
    g.setColor(Color.lightGray);
    g.fillRect(80,100,110,90);
    g.setColor(Color.red);
    g.fillRect(200,0,45,45);
    g.fillRect(0,100,70,200);
    g.setColor(Color.magenta);
    g.fillRect(200,55,60,135);
  }
  public void stop() {
    System.out.println(">> stop <<");
  }
}