Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Writing the Standalone Application

It is not much work at all to convert an applet into a standalone Java application. To do so, follow these steps:

1.  A standalone application does not subclass Applet. Change this. Usually, your application’s main class will be Frame instead:
Applet version:
public class HelloApplet extends Applet

Standalone version:
public class HelloGUI extends Frame
2.  Delete the line import java.applet.Applet. Replace it with either import java.awt.Frame or import java.awt.*.
3.  A Java applet overrides the init () method. A standalone application doesn’t use init(). Instead, when the Java interpreter (java) runs an application, it looks for a public static void method named main().
Move all the code from the old init() into main() or into the Frame’s constructor. Suppose, for example, you had written
  public void init ()
  {
    f = new Font (“Helvetica”, Font.BOLD, 24);
  }

In the standalone version, you’d write
  // main: application initialization code goes here
  public static void main (String[] args)
  {
     Frame theFrame = new HelloGUI ();     
     theFrame.setTitle (“Hello World!”);
     theFrame.setSize (250, 100);
     f = new Font (“Helvetica”, Font.BOLD, 24);
     theFrame.show ();
   }
4.  Delete your applet’s init() method.
5.  A standalone application typically needs to do a little more setup work: organize its frame layout, set the window title, and so on, as shown in step 3 of this list.
6.  Finally, you need to run your standalone application by using the JDK java interpreter rather than the appletviewer or a Web browser.

Listing 37.12 shows a completed Java application.

Listing 37.12 HelloGUI.java—A Simple Standalone Java Application


/**
 * HelloGUI: Standalone Java application
 *
 * NOTES:
 * Compare this standalone Java application
 *    with “HelloApplet” applet.
 *
 * @version 1.0, DATE: 07.07.98
 * @author Mike Morgan
 */
import java.awt.*;              // User Interface components

public class HelloGUI extends Frame
{
  private static Font f;

  private void drawCenteredString (Graphics g, String s)
  {
    Dimension d = getSize ();
    FontMetrics fm = getFontMetrics (f);
    int x =
      (d.width - fm.stringWidth (s)) / 2;
    int y =
      d.height -
        ( (d.height - fm.getHeight ()) / 2 );

    g.drawString (s, x, y);
  }

  // paint: window refresh code goes here
  public void paint (Graphics g)
  {
  g.setFont (f);
    g.setColor (Color.red);
    drawCenteredString (g, “Hello from Java!”);
  }

  // main: application initialization code goes here
  public static void main (String[] args)
  {
     Frame theFrame = new HelloGUI ();
     theFrame.setTitle (“Hello World!”);
     theFrame.setSize (250, 100);
     f = new Font (“Helvetica”, Font.BOLD, 24);
     theFrame.show ();
   }
}

The biggest practical difference between applets running on a Web page and standalone applications is that the application can generally be “trusted” and allowed to do more: open files, establish TCP/IP connections with arbitrary computers, and so on. Applets, on the other hand, are restricted from doing anything that might compromise the security of the host computer. The restricted environment that Java-enabled browsers set up for applets to run in is called a “sandbox.”

• To learn more about the sandbox, see “Executable Content and Security” on p. 1142.

Compiling and Running the Standalone Application

You can compile and run the standalone application from the command line:

javac HelloGUI.java
java HelloGUI

If you were to run this application in AppletViewer, the output will look like what you saw earlier in this chapter as Figure 37.5.


NOTE:  If you look carefully at HelloGUI, you will see that a new method is there that was not in the applet version: drawCenteredString (). This code illustrates how you can use getSize() if you ever need to find out the height and width of your “Component” (here, a Frame). drawCenteredString() also introduces FontMetrics objects and shows how you can use them.



Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.