|
|
The Java(TM) programming language is powerful and elegant. Ironically, however, many people think of it only in terms of its use for developing applets. In reality, the Java programming language is becoming the language of choice for a broad range of other development areas. Nevertheless, applets play an role important in many intranet environments because they provide an (elegant) way of implementing Web-based user interfaces to enterprise-wide computing services. An applet is an instance of a user-defined class that specializes (inherits from) Applet (java.applet.Applet). Class inheritance is beyond the scope of this tutorial, but, for now, to specialize a class is to extend its capabilities. Applet is a placeholder class with an empty paint() method. Thus, to develop a minimal applet that displays in a portion of a Web browser window, you implement a paint() method that renders graphical output. Applets employ the Java Abstract Windowing Toolkit (AWT) for the Graphics class, which provides drawing primitives, as well as for GUI components such as Button and TextField. With these components it's straightforward to design graphical forms-entry utilities that corporate-wide users access from a Web browser. Although applet programmers often develop task-specific implementations of several methods such as init(), start(), stop() that control the applet lifecycle in the browser window, a minimal example with init() and paint() is sufficient here. DogApplet.java implements a simple applet that renders a graphical barking message:
import java.awt.*;
import java.applet.Applet;
public class DogApplet extends Applet {
public void init() {
setBackground(Color.pink);
}
public void paint(Graphics g) {
g.drawString("Woof!", 10, 20);
}
}
init() set the background to an uncommon color to ensure that its allocated browser window area is visible. Java-enabled Web browsers execute init() only once, and prior to other methods. paint() uses the Graphics instance, passed as an argument by the browser environment, to draw a string at coordinates (10, 20) relative to the applet's window area. To specify an applet in a Web page, you must provide an HTML applet tag that specifies the class file (code="class-file") and its relative location (codebase="location"), as well as a width and height request for the applet's window area relative to other components in the Web page. For example, this document includes the following applet tag:
<applet code="DogApplet" codebase="classes"
width="100" height="50">
</applet>
In processing this tag the Web browser:
DogApplet appears as follows: Applets are addressed in the developerWorks tutorial "Building Java applets." One reason for mentioning them in this introduction is to point out that applet development is not trivial, and in many cases, is not the best solution for simple animations. It's true that applets can be used for iterating over a series of GIF images to present a simple animation. Recently, however, with the availability of several editors for animated GIF images, the latter is often more appropriate for simple animations. With GIF editors you can easily control common animation characteristics, whereas with applets you must program this functionality. Of course, the applet technology provides a more powerful range of programming facilities for handling complex animations. copyright 1996-2000 Magelang Institute dba jGuru |
|