UserPreferences

GettingWiredWithJavaMacChapter3


In this chapter, we will write our own simple Java program and go through the steps of entering it. In the process, we will learn some more about projects and the IDE.

1. Another Simple Java Program

The program below is a Java applet that can be used to view a specific image in the applet window. While it does not do anything fancy, it is simple enough and yet also illustrative enough of the potential fun aspects of learning programming in Java.

/*
        A simple applet that displays a picture
*/

import java.awt.*;
import java.applet.Applet;

public class ImageViewer extends Applet
{
        Image photo;

        public void init() {
                photo = getImage(getCodeBase(), "BMC.GIF");
        }

        public void paint(Graphics g) {
                g.drawImage(photo, 0, 0, this);
        }
}

We will need an image that will be displayed by this applet. I will use a picture of a computer. The picture is stored in a file called BMC.GIF. It is shown below:

http://cs.brynmawr.edu/Images/BMC.GIF

Figure 1: The image BMC.GIF

You will either need an image of your own, or you may copy this one from the following address:

http://cs.brynmawr.edu/Images/BMC.GIF

Store the image on your Zip disk and then you will be ready to implement the above program. Do the following steps:

Step 1: Find CodeWarrior IDE and launch it.

This step should be straightforward now. If you are unsure, go back to Chapter 2 and reread it first.

Step 2: Create a project.

Go ahead and start with a New Project in the IDE. We will call our project ImageViewer.mcp and store it in the same folder as before. Make sure you see the following project window:

http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_7f3380a2.png

Figure 2: Project window for ImageViewer.mcp

Step 3: Enter your Java program.

Now, we have two options here. Either we can open the TrivialApplet.java program file again and then modify the program in there, or start afresh. We will do the latter. Which means, we will first create a new program file. Go to the File menu and select the

http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_5f26bfa6.gif

A big text window, named untitled will pop up. Start entering your program here. When done, use the Save as... option in the File menu to save the file in the ImageViewer folder as ImageViewer.java

http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_m2efdeed4.png http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_26588f1e.png

Figure 3: Creating a program file and saving it in the project folder.

Next, we will also need to create an appropriate HTML file for our applet. Rather than typing the entire HTML file, we can start by using the contents of the already provided file, TrivialApplet.html. Go ahead and open it. Copy all the contents of this file and paste them into another new text file. Save the new file, with the contents as shown below, into a file called ImageViewer.html in the ImageViewer folder.

http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_m7f0a5728.png

Figure 4: Contents of the new HTML file: ImageViewer.html

Step 4: Update the project, if necessary.

This time, we will not skip this step. As you can see in your project window, it lists the TrivialApplet files in the Sources group. These files were only provided as a convenient starter. Now, we have a different set of files (ImageViewer.java and ImageViewer.html), so we have to update the project window to reflect that. First, click your mouse once on the Sources folder in the project window. Then, go to the Project menu and select the Add Files... option. This is shown on the following page:

http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_m3491cdff.gif

Figure 5: Selecting Add Files from the Project menu.

A dialog window will pop up asking you to select the file you want to add. Using the navigation bar, navigate to the ImageViewer project folder. It should list the ImageViewer files in it. First select the ImageViewer.html file, and then press Add. Another box will pop up with two options that should be checked off by default. Press OK if both boxes are checked off, if not - check them off and press OK. Repeat these two steps to further add the ImageViewer.java file.

http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_217d180d.png

Figure 6: Adding files to the project.

Next, we have to remove the TrivialApplet.java and TrivialApplet.html files from the project. Select each in the project window and drag them to the trash can. Notice that the files ImageViewer.html and ImageViewer.java are listed in the Sources folder. If, for some reason they appear elsewhere in the project window, simply select them and drag them under the Sources folder. That is likely to happen if you do not select the Sources folder before adding.

It is important to know that the picture file that you are using in this applet has to be placed in the Classes folder of your project otherwise the image will not be displayed. After copying the file into the Classes folder make sure that you repeat the two steps described above to add it to the project so that the project window displays the file under the Classes folder. Your applet is written so that it will look there for the image file. Also note that the name of the image file should be exactly the same (mind the upper/lower case letters) as specified in the applet code (BMC.GIF). At the end your project window should look like the one on the following page.

http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_7f353092.png

Figure 7: Contents of the Project window after removing TrivialApplet files

Next, select the Bring Up To Date option from the project menu as shown below.

http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_m3491cdff.gif

Figure 8: Updating the project

We are almost ready for the next step. You may go ahead and try it.

Step 5: Run the program.

Select Run from the Project menu.

Your program will be compiled and run. If there are any syntax errors, they will be pointed out at this stage. You will have to correct them before proceeding any further. Usually the compiler points out mistakes with the syntax only and if there are any conceptual or structural problems you would have to go back and find them yourself.

The output of the program is on the following page.

http://dangermouse.brynmawr.edu/cs110/manual/Chapter3_html_c112197.png

Figure 9: Output of the ImageViewer applet

2. Another way to add files

The way to add files, that we just showed you, is good if there are several files that belong to the same project. However, for most of our projects, you will have only one program file. The IDE offers an alternative way of adding files. Select the Sources folder in the project window (as before), then click the mouse in the program window (ImageViewer.java), and then use the Add Window option from the Project menu.

3. Moving on...

As you get more comfortable with the IDE, you will discover more ways of doing the same things. In addition to the menus, you can also perform the same operations using various buttons provided in the interface. Explore these slowly during the semester. Later on, we will come back to some of the more esoteric features of the IDE. Remember, it is a tool used by the pros, so there is much here to learn. But, at the same time, for a beginner, it is also an easy tool to start programming in Java.