// This is a `comment'. You can tell because the
// lines start with `//'. These lines are all
// ignored by the Java compiler, so you can write
// whatever you like. At the very least you should
// start with a comment saying what the program
// does
// Note that this program has far more comments than
// real program statements; this would probably not
// be the case in most real programs.
// Hello1.java
// A program that displays a message
// Kevin Boone, May 1999
// The first real (i.e., non-comment) line of the program is `import'
// This tells the compiler what classes to use apart
// from any defined in this program. In this case
// I tell the compiler to consider using any of the
// classes that are part of the `package' java.awt.
// and the class `Applet'
// I have to do that because the program uses the
// Class `Applet' to do most of the work, and this
// class is defined in the java.applet package and
// the java.awt package.
// Most Java programs will start with an `import'
// statement
import java.applet.Applet;
import java.awt.*;
// So now down to work. Define a class. All java programs
// have at least one class. In this case we are writing an
// applet, so the new class is a type of applet. `extends'
// means, essentially, `is a type of'.
// Note that Java rules stipulate that a `public' class
// must be defined in a file of the same
// name, i.e., Hello1 must be defined in `Hello1.java'
public class Hello1 extends Applet
// The open brace below denotes that all the statements that
// follow are part of the class Hello1, until the matching
// closing brace at the end of the program
{
// Now we define an operation called `paint'. Providing this
// operation ensures that when the program starts up,
// something useful will happen.
// If we do not provide a `paint' operation, then the program
// will compile and run correctly, but it won't display
// anything at all.
// The concept of an `operation' will be covered in more detail
// later in the course
public void paint (Graphics g)
{
// Now the program text is `indented', this is,
// all the lines start a few spaces from the
// left margin. Doing this makes it easy for the
// (human) reader to identify all the lines that
// are part of `paint'. The computer does not
// care about this, but people will find the
// program easier to understand
// `drawString' is an operation in the class called
// Graphics. Its job is simply to display text in
// an area of the screen. In this case the text is
// positioned 20 pixels across, and 20 pixels down
g.drawString ("Welcome to INT4120", 20, 20);
}
// This final brace denotes the end of the class `Hello1' and,
// in this case, the end of the program
}
©1994-2003 Kevin Boone, all rights reserved