14.5 Applets


14.5 Applets

In addition to console and graphical applications, Java and KJP support applets. These are not standalone programs, because they require a Web browser to run. The code of the compiled class for an applet is placed in an HTML file with the appropriate tags. When a user uses his Web browser to start an applet, the compiled classes of the applet in the HTML file are downloaded from the server and execute.

Suppose the class for an applet is named Akjplogo, the appropriate tags in the HTML file with the compiled class are as follows:

       <applet       code = "Akjplogo.class" width=300 height=400>       </applet> 

An applet normally includes graphical components in addition to any computation that may appear in a program. A Web browser displays the complete Web page, including the GUI for the applet. A small and complete HTML file with an applet embedded in it is shown next.

       <HTML>         <HEAD>            <TITLE> The KJP Applet </TITLE>         </HEAD>       <BODY BGCOLOR=blue TEXT=white>         This is a simple applet showing the KJP logo.         Any text included here in the HTML document.         <CENTER>           <H1> The KJP Applet </H1>           <P>               <APPLET CODE="Akjplogo.class"               WIDTH=250 HEIGHT=150>               </APPLET>         </CENTER>       </BODY>       </HTML> 

There are various aspects of an applet to consider when defining the corresponding class, and that differentiates it from a conventional class. In an applet, the class definition must inherit class JApplet from the Swing library, or the Applet class from the AWT library. As mentioned before, applets are not standalone programs, so function main is not used. Instead, function init is included. A frame for a window is not defined because the applet automatically constructs a window. The size of the applet window is set in the HTML file. The Web browser makes the applet visible.

Class Akjplogo defines an applet that displays the KJP logo. It defines three graphical components that are labels, in a similar manner to class Kjplogo. The KJP code with the implementation of the applet class Akjplogo is presented as follows.

       import all javax.swing  // Library for graphics       import all java.awt       description           This applet creates and displays a frame window           with an image and a text label.    */       class Akjplogo inherits JApplet is         public         description           This is the main function of the application. */         function init is           objects             object cpane of class Container             object blabel1 of class JLabel   // text label             object blabel2 of class JLabel             object kjplabel of class JLabel  // for image             object kjpimage of class ImageIcon    // image             object lmanager of class BorderLayout           begin             create blabel1 of class JLabel                  using "Kennesaw Java Preprocessor"             create blabel2 of class JLabel                  using "The Language for OOP"             create kjpimage of class ImageIcon                  using "kjplogo.gif"             create kjplabel of class JLabel                  using kjpimage             create lmanager of class BorderLayout             set cpane = call getContentPane             call setLayout of cpane using lmanager             // add the text image label and text label             // components to the content pane             call add of cpane using                  kjplabel, BorderLayout.CENTER             call add of cpane using                  blabel1, BorderLayout.NORTH             call add of cpane using                  blabel2, BorderLayout.SOUTH          endfun init       endclass Akjplogo 

On the CD

The KJP code with the implementation for class Akjplogo is stored in the file Akjplogo.kpl. The Java implementation is stored in the file Akjplogo.java.

To execute the applet, a Web browser is used to run the applet class. To test the applet the appletviewer utility can be used. When the applet class Akjplogo executes with the appletviewer, the GUI shown in Figure 14.9 appears on the screen.

click to expand
Figure 14.9: An applet showing the KJP logo.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net