Google

IBM

Hello World applet

This example is an applet which simply displays some text in a window. Here's the source (also available as a plain ASCII file):


  /* A terribly simple Applet */



  class HelloApplet extends Applet

    method init

      resize(200, 40)



    method paint(g=Graphics)

      g.drawString("Hello world!", 50, 30)

The first line is a comment, which isn't really needed. The third line introduces the class that will form the applet (warning: the name of the class should exactly match the name of the file, including use of capitals, or Java will get confused).

The class contains two methods: the first is called when the applet is initialized, and the second when the window needs to be displayed.

To run the program, you'd create a plain text file containing the lines above, and then enter the command:


  java COM.ibm.netrexx.process.NetRexxC helloapplet

This would:

  1. Call the NetRexx translator to translate the program from NetRexx to Java
  2. Call the Java compiler (javac) to compile the program into a class file

To have the applet displayed in World Wide Web page, you then need to

  1. place the class file in the same directory as the page in which is is to appear
  2. refer to it, wherever you want it to appear on the page, with the <applet> tag:
    
      <applet code="HelloApplet.class" width=200 height=40>
    
      </applet>
    
    

    which sets the maximum size for the window in which the applet will appear.

You can then view it with any Java-enabled browser. If you are using such program to view this page, you should see the applet just below here.

If you do not have a Java-enabled browser, you can view the applet using the 'applet viewer' that comes with the Java Development Kit. To do this, change to the directory containing the World Wide Web (HTML) page that contains the reference to the applet, and issue the command


  applet helloapp.htm

where 'helloapp.htm' is the name of the page. This should start up the applet viewer, showing a window for each applet referenced from the page. [Note: the applet viewer may be called 'appletviewer' on some systems.]

If you do not see the applet in the window, or the window does not appear, close the applet (if necessary) and see if there are any messages in the applet viewer's 'weblog' file. This may be found in the 'applet' or 'weblogs' subdirectory which you should find in the Java home directory (e.g., d:\javaos2, depending on where you installed Java).

Warning: the applet viewer takes a URL (e.g., http://etc...) or a plain file name. If you want to give it the name of a file that needs a path, prefix the file specification with 'file:/'. For example


  applet file:/d:\fred\myapplet.htm

The format of the file specification will vary from platform to platform.

[ IBM | Help | Legal | Privacy ]