Aqua Phoenix
     >>  Research >>  OOP  
 

Navigator
   
 
       
   

7. Java Framework

7.1 Summary

  • Using the Java Framework of classes, functions, and methods (overview)
  • Compiling and running a Java Applet
  • Debugging errors when compiling

7.2 Contents

7.2.1 Using the Java Framework of classes, functions, and methods (overview)

The source code to Java Programs is placed in text files that are named directly after the class (see below). For now, we are only concerned with one file, as opposed to multiple files. You can use the framework below as a starting point for all of your Java Applet programs for now. Let's look at a motivating example:

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

public class myApplet extends Applet implements ActionListener {


  public void init() {

  }


  public void actionPerformed(ActionEvent e) {

  }

}
This is what a bare-bone Applet looks like. The first few lines that begin with "import" are used to tell the program where to find libraries. These libraries contain very basic Java objects that we will be using in our programs. For example, buttons or textfields are so basic that we don't want to program them anew every time we develop a Java Applet. For that reasons they have been placed in libraries, so that they may be used right out of the box.

The next thing we'll look at is the line that begins with public class. This line defines the name of the applet as being myApplet, and designates it as being a Java Applet as opposed to some other type of application by extends Applet. In a later lecture, we'll learn more about what extends means. For management reasons, the textfile in which the above code resides must be named after the class name, i.e. myApplet.java. Capitalization is important, that is, it makes a difference whether you define a class named myapplet or myApplet. If you took the above code and changed the name to something else, be sure that the filename reflects that change.

The very first function in this file is named init(). Nevermind the word "public" in front of it - we will be discussing its purpose later in the course. In fact, this particular function should always be preceded by the name "public". This very function is essential for any applet, and must be present in all. It does not matter whether it's the first or last function that appears in the file, as long as it is there. The function is used to set-up the applet and designate a certain layout, as we will see later. As you may remember from our previous discussion on functions, this function does not return anything (void) and it does not take any arguments (()). For the first few applets that you will be using, this function is already set-up - the contents is discussed later in the course.

Finally the second and last function by the name of actionPerformed is necessary to capture Mouse Clicks on Buttons and other such interactive objects. Again, this function is necessary for the types of applets we are going to write.

7.2.2 Compiling and running a Java Applet

Before running a Java Applet, it needs to be compiled into a binary, human-unreadable format. Source code itself cannot be run. We will not be discussing the reasons for this. We shall take it as granted. In order to compile our Java Applet, we will go through the following step in a DOS or Unix prompt. We will use the greater than sign (>) to denote the prompt, and we will assume that we have changed into the directory, in which our Java files are stored.

> javac myApplet.java
Depending on whether the code contains errors or not, you will see error messages, or simply the prompt on the next line. If you receive error messages, you must debug your program, i.e. look for errors, correct them, and re-compile as in the above step.

Once your program compiles without errors, you can run it. As you may have noticed, applets appear a lot on web pages, as opposed to anywhere else. In order to run an applet, you will need to simulate a web environment by creating an HTML file with the APPLET tag. Even if you have no idea what any of this means, you can do so quickly and painlessly by using the following template:

<APPLET CODE="myApplet" WIDTH=800 HEIGHT=600>
</APPLET>

CODE refers to the classname (or filename) of your applet. Do not include the extension .class or .java. WIDTH and HEIGHT refer to the size of the applet. Most objects, like buttons and textfields, have the property of adjusting themselves to the size of the applet. Thus, you need not worry about the initial sizing of the applet when you start laying out components.

The above two lines should be stored in a typical HTML textfile, e.g. myApplet.html. Unlike the file for the Java code, the name of this file needs not be the same; you may as well have called it appletpie.html. Moreover, you need to create this file only once, that is the first time you would like to view the applet.

As soon as you have a compiled program and the HTML file, you can view your applet as follows:

> appletviewer myApplet.html
This command will open the applet in a separate window. While you are viewing the applet, you should observe the shell window, from where you started the applet. If any errors occur, they will show up very elaborately in that window.

7.2.3 Debugging errors when compiling