Prev NEXT

How Java Works

Your First Program

Your first program will be short and sweet. It is going to create a drawing area and draw a diagonal line across it. To create this program you will need to:

  1. Open Notepad and type in (or cut and paste) the program
  2. Save the program
  3. Compile the program with the Java compiler to create a Java applet
  4. Fix any problems
  5. Create an HTML web page to "hold" the Java Applet you created
  6. Run the Java applet

Here is the program we will use for this demonstration:

Advertisement

import java.awt.Graphics;

public class FirstApplet extends java.applet.Applet
{

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

Step 1 - Type in the program

Create a new directory to hold your program. Open up Notepad (or any other text editor that can create TXT files). Type or cut and paste the program into the Notepad window. This is important: When you type the program in, case matters. That means that you must type the uppercase and lowercase characters exactly as they appear in the program. Review the programmer's creed above. If you do not type it EXACTLY as shown, it is not going to work.

Step 2 - Save the file

Save the file to the filename FirstApplet.java in the directory that you created in step 1. Case matters in the filename. Make sure the 'F' and 'A' are uppercase and all other characters are lowercase, as shown.

Step 3 - Compile the program

Open an MS-DOS window. Change directory ("cd") to the directory containing FirstApplet.java. Type:

javac FirstApplet.java

Case matters! Either it will work, in which case nothing will be printed to the window, or there will be errors. If there are no errors, a file named FirstApplet.class will be created in the directory right next to FirstApplet.java.

(Make sure that the file is saved to the name FirstApplet.java and not FirstApplet.java.txt. This is most easily done by typing dir in the MS-DOS window and looking at the file name. If it has a .txt extension, remove it by renaming the file. Or run the Windows Explorer and select Options in the View menu. Make sure that the "Hide MD-DOS File Extensions for file types that are registered" box is NOT checked, and then look at the filename with the explorer. Change it if necessary.)

Step 4 - Fix any problems

If there are errors, fix them. Compare your program to the program above and get them to match exactly. Keep recompiling until you see no errors. If javac seems to not be working, look back at the previous section and fix your installation.

Step 5 - Create an HTML Page

Create an HTML page to hold the applet. Open another Notepad window. Type into it the following:

<html>
<body>
<applet code=FirstApplet.class width=200 height=200>
</applet>
</body>
</html>

Save this file in the same directory with the name applet.htm.

[If you have never worked with HTML before, please read How a Web Page Works. The applet tag is how you access a Java applet from a web page.]

Step 6 - Run the Applet

In your MS-DOS window, type:

appletviewer applet.htm

You should see a diagonal line running from the upper left corner to the lower right corner.

Pull the applet viewer a little bigger to see the whole line. You should also be able to load the HTML page into any modern browser like Netscape Navigator or Microsoft Internet Explorer and see approximately the same thing.

You have successfully created your first program!!!