Thursday, 12 July 2007

Java Fundamentals (part 6)

Dissection of the “Hello World” Application - continued



Exercise 1 - 2: The “Hello World” Applet


Create a text file named HelloWorld.java with the code shown below:


import java.applet.Applet;


import java.awt.Graphics;


public class HelloWorld extends Applet {


    public void paint(Graphics g) {


            g.drawString("Hello world!", 50, 25);


    }


}

Compile the source file with the Java compiler and as before it should create a HelloWorld.class file in the same directory.


Note: Java compiler in the JDK will compile both applications and applets.


An applet is not executed in the same way as an application. You must embed an applet in a web page before it can be run. You can then execute it either in a Java-enabled web browser such as Explorer and Firefox, or you can execute it using the appletviewer provided by JDK.


This means that you should at least know some basic HTML to accomplish this. In this course we are only going to cover enough HTML so we can run over applets. If you would like to learn more advanced HTML then you should get a proper HTML book.


Basic HTML


When you define a web page as an HTML document, it is stored in a file with the extension .html. A HTML document consists a number of elements, and each element is identified by tags. The document will begin with <HTML> and end with </HTML>, these are tags, and each element in an HTML document will be enclosed between a similar pair of tags between angle brackets. Here is an example of an HTML document consisting of a title and some other text:


          <HTML>


          <HEAD>


          <TITLE>My
First Web Page</TITLE>


          </HEAD>


          <BODY>


          You can write
anything you want.


          </BODY>


          </HTML>


The text enclosed by the <TITLE> element tags will be displayed as the window title when the page is being viewed. Other element tags can appear within the <BODY> element, and they include tags for headings, lists, tables, links to other pages and Java applets. You can find a comprehensive list of available HTML tags in any HTML book.


For many element tag pairs, you can specify an element attribute in the starting tag which defines additional or qualifying data about the element. This is how a Java applet is identified in an <APPLET> tag.


Now we know enough to put our HelloWorld applet on a web page so lets continue where we left it.
Using a text editor, create a file named Hello.html in the same directory that contains

HelloWorld.class
. This HTML file should contain the following text:


          <HTML>


          <HEAD>


          <TITLE> A
Simple Program </TITLE>


          </HEAD>


          <BODY>

Here is the output of my program:


          style='background:silver;mso-highlight:silver'><APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>


          </APPLET>


          </BODY>


          </HTML>

Lets look at the shaded line above, the applet to be executed is HelloWorld.class. The file containing the bytecodes for the applet is specified as an attribute in the <APPLET> tag. The other two attributes define the width and height of the region on the screen that will be used by the applet when it executes. These are compulsory.


To run the applet, you need to load the HTML file into an application that can run Java applets. This application might be a Java-compatible browser or another Java applet viewing program, such as the Applet Viewer provided in the JDK. You can invoke the JDK appletviewer from the DOS command prompt as follows:


          appletviewer Hello.html

No comments: