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.
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.
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:
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.
A descendent of Java class Applet can override methods other than "paint" to do useful things. Here are some of those methods:
The Java library is quite extensive; see Sun's on-line Java documentation or one of the many books on Java.
Which of the following statements is true?
Go back to the previous section | Skip to the next section | 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".