Understanding What Just Happened

So what just happened?

First, you wrote a piece of code for an extremely simple Java applet. An applet is a Java program that can run within a Web browser, as opposed to a Java application, which is a stand-alone program that runs on your local machine (Java applications are slightly more complicated and somewhat less popular, so we will start with applets). We compiled the applet using javac. We then created an extremely simple Web page to "hold" the applet. We ran the applet using appletviewer, but you can just as easily run it in a browser.

The program itself is about 10 lines long:

import java.awt.Graphics;

public class FirstApplet extends java.applet.Applet
{

    public void paint(Graphics g)
    {
        g.drawLine(0, 0, 200, 200);
    }
}

This is about the simplest Java applet you can create. To fully understand it you will have to learn a fair amount, particularly in the area of object oriented programming techniques. Since I am assuming that you have zero programming experience, what I would like you to do is focus your attention on just one line in this program for the moment:

g.drawLine(0, 0, 200, 200);

This is the line in this program that does the work. It draws the diagonal line. The rest of the program is scaffolding that supports that one line, and we can ignore the scaffolding for the moment. What happened here was that we told the computer to draw one line from the upper left hand corner (0,0) to the bottom right hand corner (200, 200). The computer drew it just like we told it to. That is the essence of computer programming!

(Note also that in the HTML page, we set the size of the applet's window in step 5 above to have a width of 200 and a height of 200.)

In this program, we called a method (a.k.a. function) called drawLine and we passed it four parameters (0, 0, 200, 200). The line ends in a semicolon. The semicolon acts like the period at the end of the sentence. The line begins with g., signifying that we want to call the method named drawLine on the specific object named g (which you can see one line up is of the class Graphics -- we will get into classes and methods of classes in much more detail later in this article).

A method is simply a command -- it tells the computer to do something. In this case, drawLine tells the computer to draw a line between the points specified: (0, 0) and (200, 200). You can think of the window as having its 0,0 coordinate in the upper left corner, with positive X and Y axes extending to the right and down. Each dot on the screen (each pixel) is one increment on the scale.

Try experimenting by using different numbers for the four parameters. Change a number or two, save your changes, recompile with javac and rerun after each change in appletviewer, and see what you discover.

What other functions are available besides drawLine? You find this out by looking at the documentation for the Graphics class. When you installed the Java development kit and unpacked the documentation, one of the files unloaded in the process is called java.awt.Graphics.html, and it is on your machine. This is the file that explains the Graphics class. On my machine, the exact path to this file is D:\jdk1.1.7\docs\api\java.awt.Graphics.html. On your machine the path is likely to be slightly different, but close -- it depends on exactly where you installed things. Find the file and open it. Up toward the top of this file there is a section called "Method Index." This is a list of all of the methods this class supports. The drawLine method is one of them, but you can see many others. You can draw, among other things:

  • Lines
  • Arcs
  • Ovals
  • Polygons
  • Rectangles
  • Strings
  • Characters

Read about and try experimenting with some of these different methods to discover what is possible. For example, try this code:

        g.drawLine(0, 0, 200, 200);
        g.drawRect(0, 0, 200, 200);
        g.drawLine(200, 0, 0, 200);

It will draw a box with two diagonals (be sure to pull the window big enough to see the whole thing). Try drawing other shapes. Read about and try changing the color with the setColor method. For example:

import java.awt.Graphics;
import java.awt.Color;

public class FirstApplet extends java.applet.Applet
{

    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.fillRect(0, 0, 200, 200);
        g.setColor(Color.black);
        g.drawLine(0, 0, 200, 200);
        g.drawLine(200, 0, 0, 200);
    }
}

Note the addition of the new import line in the second line of the program. The output of this program looks like this:

One thing that might be going through your head right now is, "How did he know to use Color.red rather than simply red, and how did he know to add the second import line?" You learn things like that by example. Because I just showed you an example of how to call the setColor method, you now know that whenever you want to change the color you will use Color. followed by a color name as a parameter to the setColor method, and you will add the appropriate import line at the top of the program. If you look up setColor, it has a link that will tell you about the Color class, and in it is a list of all the valid color names along with techniques for creating new (unnamed) colors. You read that information, you store it in your head and now you know how to change colors in Java. That is the essence of becoming a computer programmer -- you learn techniques and remember them for the next program you write. You learn the techniques either by reading an example (as you did here) or by reading through the documentation or by looking at example code (as in the demo directory). If you have a brain that likes exploring and learning and remembering things, then you will love programming!

In this section, you have learned how to write linear, sequential code -- blocks of code that consist of method calls starting at the top and working toward the bottom (try drawing one of the lines before you draw the red rectangle and watch what happens -- it will be covered over by the rectangle and made invisible. The order of lines in the code sequence is important). Sequential lines of code form the basic core of any computer program. Experiment with all the different drawing methods and see what you can discover.