Part 3: Applets
The last chapter was rooted solidly in the 1970's. It used techniques often referred to as "structuredor "procedural programmingwhich were popular then. (We skipped right over the most popular innovation of the 60's and the Basic programmer, spaghetti code). Certain programmers are sometimes said to "Write Fortran in any language,and that's more or less what we did. You now have the knowledge to accomplish with Java anything that can be done within the bounds of ANSI-standard Fortran 77.
In this chapter we're going to move into the 1980's. In particular we're going to work with event driven programming . This style of programming should be very familiar to Macintosh and Windows programmers. In those environments program logic doesn't flow from the top to the of the program as it does in most procedural code. Rather the operating system collects events and the program responds to them. These events may be mouse clicks, keypresses, network data arriving on the Ethernet port, or any of about two dozen other possibilities. The operating system looks at each event, determines what program it was intended for, and places the event in the appropriate program's event queue .
Every application program has an event loop . This is just a while loop which loops continuously. On every pass through the loop the application retrieves the next event from its event queue and responds accordingly.
Java applets behave similarly. However the runtime environment (i.e. the browser) takes care of the event loop for the applet so there's no need to write one explicitly. Rather you need to have methods in your applet subclass that respond to each kind of event you want to process.
This is all fairly abstract until you see some concrete examples. Let's begin with a simple one.
The reason people are excited about Java as more than just another OOP language is because it allows them to write interactive applets on the web. Hello World isn't a very interactive program, but let's look at a webbed version.
import java.applet.Applet; import java.awt.Graphics; public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello world!, 50, 25); } } This applet version of HelloWorld is a little more complicated than the HelloWorld application of the previous chapter, and it will take a little more effort to run it as well.
First type in the source code and save it into file called HelloWorldApplet.java in the javahtml/classes directory. Compile this file by typing javac HelloWorldApplet.java at the command line prompt.
If all is well a file called HelloWorldApplet.class will be created. This file must be in your classes directory.
Now you need to create an HTML file that will include your applet. The following simple HTML file will do.
<HTML <HEAD <TITLE Hello World </TITLE </HEAD <BODY This is the applet:<P <APPLET codebaseclassescodeHelloWorldApplet.class=200 =200 </APPLET </BODY </HTML Save this file as "HelloWorldAppletin the javahtml directory. When you've done that load the HTML file into a Java enabled browser such as HotJava or Netscape 2.0. You should see the following:
This is the applet: Hello World!
If the applet compiled without error and produced a HelloWorldApplet.class file, and yet you don't see the string "Hello Worldin your browser chances are that the class file is in the wrong place. Make sure the file is in the javahtml directory and the compiled .class file is in the javahtml/classes directory.