The Graphic Java units will concentrate in creating simple graphical designs in Java. They are not meant to teach programming in Java. However, completing these units will familiarize the student with a portion of the Java programming language. The Java programming language is a complete professional programming language and has been adopted by many universities as the primary programming language for their programming courses.


1. The Applet:
The code contained in the following text area defines an applet with the named name. (That's right, its name is name.) This applet is about as simple as an applet can be and it provides a framework from which you to work from. That is, you can use it to create all the assignments in the first part of this module. Typically, you will type up this code in a text editor such as JOE. To do this you type:

at the command line in a terminal window and then simply enter the code as shown:

All java files should have the extension .java.

2. Compiling the Applet:
Once you have typed up the java file, you can compile it like this:

If your code is correct a class file will be created. You can verify the existence of this file by entering dir at the command line. If, on the otherhand, there are mistakes in your java file, the java compiler will report their location to you and you will need to correct them and then try compiling again.

3. Viewing the Applet:
Once you have a class file you can test it by using the appletviewer like this:

The code at the bottom of the java file makes this possible. The bottom two lines are the applet tag as defined in HTML (what you would put on a web page to show the applet).

The Example: Here's a basic line-by-line analysis of the example:

Line Number(s)Explanation
1,2imports - These are references to java libraries. You don't have to worry about these. Just always include these lines in your applet.
4Class declaration - The only part you need to worry about here is the name (in this case the name IS name). The name given on this line must match the name of the file and the name listed in the applet tag at the bottom of the page.
5init method declaration - The init method is a standard method used in all applets. You should always type this line exactly as you see it here.
6setBackground - this method call is used to set the background color for your applet area. The only part you might want to change for now is the actual color named. For instance, you might substitute red, yellow, cyan, magenta, black, white, green, or gray for blue.
7Closing Curly Brace - This closes the init method. If you leave this out you will have a compiler error.
9paint method declaration - The paint method is a standard method used in all applets. You should always type this line exactly as you see it here.
10setColor - The setColor method sets the current drawing color used in the applet. You can substitute colors as described for the setBackground method.
11drawString - This method allows you to draw a string. It takes three arguments: a string variable and two integer values. The string value is what gets printed in the applet area. The integer values select the location at which the message is displayed (the first integer value designates how far to the right starting from the left side the message is displayed and the second integer value designates how far down from the top to display the message).
12, 13Curly Braces - The first closes the paint method and the second closes the class
15, 16applet tag - This is an HTML tag used to display an applet on a web page. Here it signals the appletviewer how to display the applet. You will need to make sure that the code file named matches the name of the file and class for the file in which it appears. Also you can customize the size of your applet using the width and height attributes.


STEP ONE: Get the above example to work!

STEP TWO: Add two lines of code following line 11, but before the curly braces in lines 12 and 13. The first line you add will change the current drawing color. The second line you add will print, "my name is Toby Jones" (only you will substitute your name for Toby Jones).