Lab 12 Creating your own applet + More Java graphics


Objectives:

The goal of this lab is to practice writing your own Java programs from the beginning to the end. You will learn how to:
In this lab you will write your own Java program from scratch. There is nothing to download, because you will create the Java project yourself. You also need to figure out Java classes that you need to write and their methods. You might find it helpful to look at your previous projects, especially those from the past couple of weeks, to use them as samples.

The assignment for this lab is to create a project that produces an applet that looks something like this:

How to create an applet in CodeWarrior

  1. Start CodeWarrior by selecting it from the Apple menu.
  2. Choose New... from the File menu. A window will pop up, as shown below. Select "Java Stationary" as the kind of your project, and type in a name with the extension .mcp. Then click OK.
    Note that the extension .mcp is required for a project name. Otherwise the file will not be recognized as a project by Fetch.

  3. A new window will pop up giving you choice between 3 versions of Java. Click on JDK1.1 and select Java Applet. Then click OK.

  4. As the result, a new folder will be created on your Desktop which has the same name as the project, only without the .mcp extension. We call this folder a project folder. The project is created with three standard files: TrivialApplet.html, TrivialAppletDebug.html, and TrivialApplet.java. Remove these files from your project by dragging them from the project window to the trash. Note that deleting these files from the project does not automatically delete them from the project folder. If you want to delete them from the folder, double-click on the folder to open it, and then drag the files into the trash.
  5. In the CodeWarrior choose New Text File from the File menu. A new untitled file will open. You need to create two files: the HTML file that specifies which class to run and the JAVA file. Your HTML file can have any name and (for this project) it will look like this:
    
    <TITLE> Rainy Day Applet </TITLE>
    
    <applet codebase = "Java Classes" code = "RainyDay.class" width = 400, height = 400>
    </applet>
    
    Type this code into the new file and save it in the project folder. You can give it any name with the extension .html.
  6. Create another new file called RainyDay.java and save it in the project folder. The name of this file must be the same as the name of the class that it defines (only with the .java extension), and in our case the class is RainyDay.class, as specified in the HTML file above.
  7. Now it remains to add the two new files to the project. To do this, open the project folder and drag the file RainyDay.java into the project window in the folder Sources. A window will pop up asking you to confirm that you are adding the files to the project's targets.
  8. The same way add the HTML file that you have created, also to Sources. In the end the project window should look like this:

    Now your project is created.
Now, when you have created your project, you need to write some JAVA code in the file RainyDay.java. Recall that we want the applet to look something like this:

Happy programming!