Section 16.5 - Writing Ada Applets

In this section we'll first describe event driven programming, the basic mindset of applets (and most other graphical user interface programs). We'll then examine a simple "Hello, World" applet in Ada. The section closes with a list of some other useful Applet methods.

Event Driven Programming

Most graphical user interface (GUI) programs do not run "top to bottom" in a simple linear way. Instead, most such programs are structured as components which wait for an "event" (such as a mouse button click) to occur. That event is processed, and then the component returns so that the next event can be processed. Events are queued up, so your program only needs to respond to one event at a time; later ones will not be lost. It's important that the component return, or no further event will be processed. This approach is called "event driven programming".

To create an Ada applet, we'll need to create a new type that extends the Java "Applet" class in Java package "java.applet". We can then override various methods of Applet to process events we're interested in. The default reaction to events is to return immediately (i.e. do nothing), so any events we don't override will be ignored.

A Simple Ada Applet

Here is a simple Ada applet to show the basic idea of how to implement Java applets in Ada. Below is the canonical "Hello World!" program as written by Tucker Taft; it simply displays the phrase "Hello, world!" on the screen:

with java.applet.Applet; use java.applet.Applet;
with java.awt.Graphics; use java.awt.Graphics;
package Hello is
 type Hello_Obj is new Applet_Obj with null record;
 procedure paint(Obj : access Hello_Obj; g : Graphics_Ptr);
end Hello;


with interfaces.Java; use interfaces.Java; -- for "+" on strings
package body Hello is
 procedure paint(Obj : access Hello_Obj; g : Graphics_Ptr) is
 begin
 drawString(g, +"Hello, Java world!", x => 10, y => size(Obj).height/2);
 end paint;
end Hello;

So what does this program do? Let's break it down step by step:

  1. To create an applet we must extend the tagged type "Applet_Obj" defined in the package java.applet.Applet. Since we need package java.applet.Applet, we must "with" it. Note that standard Java package names have a very simple correspondence to Ada package names.
  2. To handle graphics, we'll need package java.awt.Graphics.
  3. We'll create a new package named "Hello".
  4. We'll create a new tagged type "Hello_Obj" that extends the Java library tagged type "Applet_Obj". This new type represents our new applet, per the conventions described earlier.
  5. Almost any applet will override the default "paint" operation of Applet_Obj with something more interesting. The "paint" operation is called whenever the system determines that the graphical area controlled by the applet has been uncovered and needs to be drawn again. The parameters for the paint operation are an object representing the applet itself and an object representing the applet drawing surface (you could read the package specification of java.applet.Applet to see what other operations are defined).
  6. The package body of Hello withs "interfaces.Java", an Ada package that provides useful operations when interfacing from Ada to Java. Of particular use is a "+" prefix operation that converts an Ada string into a Java string.
  7. Paint is implemented by making a call to subprogram "drawString" in java.awt.Graphics, which draws the text into graphical viewing area "g". Note the little "+" sign used to convert an Ada String into a Java string (String_Ptr). Note that more complex Ada expressions work as well, such as the expression giving the y-coordinate for drawing.

You'll need to compile the code above, but to see it execute you also need a web page that references the applet. The web page will need to include an APPLET command. Here's a simple web page that references the applet (you can type this text into a file using a text editor and call it "hello.html"):

<HTML>
<HEAD><TITLE>Hello World</TITLE></HEAD>
<BODY>
<H1>Hello World</H1>
Below is the hello world applet.

<APPLET CODE="Hello.class" WIDTH=200 HEIGHT=100>
</APPLET>

</BODY>
</HTML>

The text beginning with "<APPLET " tells the web browser to run a Java applet. The quoted text after "CODE" indicates which program to run. The "WIDTH" and "HEIGHT" parameters specify the width and height in pixels of the graphical area the applet may use. There are other possible parameters. In particular, if the class you wish to run is not in the same directory as the web page, you need to add a "CODEBASE=" parameter that gives the directory of the class to run.

Any text between <APPLET> and </APPLET> will be displayed by web browsers that don't handle Java, which is useful so you can handle such browsers (for example, you could give them a static picture or a form instead). You can also use that area to pass parameters to the applet.

To view this applet, use a web browser that supports Java and view the web page, or run an applet viewing program (a program designed to run Java applets). If you're using a Java-capable browser, you can see the Hello applet right now. Use the "back" key on your browser to return to this page.

Other Applet Methods

A descendent of Java class Applet can override methods other than "paint" to do useful things. Here are some of those methods:

  1. init is called when the applet starts up, before any other events are processed. You can override this method to initialize some variables. Netscape also calls this method when an applet is reloaded or you return to the page containing the applet, so make sure it can handle being called multiple times.
  2. stop is called when the user is no longer looking at the page that contains the applet.
  3. start is called when a user brings their attention back to an applet, and is called after the init method.
  4. mouseDown is called whenever the mouse button is pressed. Typical uses are to highlight the item being pressed (like a button) to make it clear to the user what they're about to select.
  5. mouseUp is called whenever the mouse button is released in the applet's viewing area. In many cases you want to react to a mouse button being released (mouseUp), not when it's been pressed (mouseDown), so that users can change their mind by moving the mouse away without releasing the button.
  6. mouseDrag is called when the user moves the mouse while holding the mouse button down.
  7. keyDown is called when the user pressed a key while the applet is active.

The Java library is quite extensive; see Sun's on-line Java documentation or one of the many books on Java.


Quiz:

Which of the following statements is true?

  1. Most applets that use the mouse button should cause important actions to happen on mouseDown so that if the user changes his mind, he can move the mouse away.
  2. Most applets will override the "paint" operation to draw whatever it is that they want to draw.
  3. There's no way to provide alternative text or graphics for users with a Java-less web browser.
  1. Statement 1.
  2. Statement 2.
  3. Statement 3.

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 16 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s16s5.htm".