Laboratory 2: Your First Applet

February 7, 8, 2006

Bring your Java, Java, Java text to the laboratory.

Objectives

The objectives of this laboratory are
  1. to create a personal web page containing a Java applet,
  2. to introduce the principles of writing a Java applet,
  3. to give you some practice with String and Graphics objects,
If you have not, first read §2.1-2.3 of Java, Java, Java.

Problem statement

Design and implement your own version of the HelloWorldGraphic applet and post it on your personal web page. To see an example, click on this sample page.

Preparation

Step 1. Setting Up Your lab2 Directory

After logging in to your account, create a new directory named lab2 in your cpsc115 directory. Click on each of the following links and download the following files into your lab2 directory.

  • HelloWorldGraphic.java (From page 72 of Java, Java, Java)
  • samplepage.html (A model for your first web page.)

    For the samplepage.html file you will have to use the Browser's View Source option in order to see the source code. Save the source code in a file with the extension html.

    Step 2. Run the appletviewer Program

    Open a Terminal application and use the cd command to change into your lab2 directory. To run an applet we use the appletview program, which is like stripped-down browser that displays Java applets without displaying the rest of the web page that contains the applet:

        appletviewer samplepage.html
    
    Note that the argument to appletviewer is the html file, not the Java applet. If you look at the sourcecode for samplepage.html you will see that it contains an applet tag, which specifies the Java applet:
    <applet code="Logo.class" width=300 height=200>
    </applet>
    

    In this case, the applet draws the logo that is displayed by appletviewer.

    Implementation

    Step 3. Designing your graphic

    Using the graph paper supplied, design a logo-like graphic using circles, ovals, rectangles, squares, colors, and strings. The graphic should contain at least three colors, three different shapes, and a string. In designing the graphic, you will have to figure out the coordinates for each of the shapes that it uses and their dimensions. In addition to the shapes illustrated in HelloWorldGraphic.java, you can consult the online documentation of the Graphics class to find other shapes you might try. Here's the link:

    Java Library Classes

    Have you graphic checked by the instructor or TA. Here's a sample:



    Step 4. Implementing in Java.

    Now develop an algorithm in Java to draw your graphic. Your algorithm will consist of a sequence of statements, similar to the statements in the paint() method of the HelloWorldGraphic class:

        public void paint(Graphics g) 
        {   g.setColor(Color.cyan);              // Set color
            g.fillRect(25, 25, 140, 40);         // Fill rectangle
            g.setColor(Color.blue);              // Set color
            g.drawRect(25, 25, 140, 40);         // Outline rectangle
            g.setColor(Color.black);             // Set color
            g.drawString("Hello World", 50, 50); // Display string
            g.setColor(Color.yellow);          
            g.fillOval(25, 75, 140, 40);         // Fill oval
            g.setColor(Color.red);
            g.drawOval(25, 75, 140, 40);         // Outline oval
            g.setColor(Color.black);
            g.drawString("Welcome to Java", 50, 100);
        } // paint()
    

    Use methods of the Graphics class to draw the shapes, colors, and strings contained in your logo. Create a file named Logo.java. The best way to do this is to copy (or save as) the HelloWorldGraphic.java file. You will need to modify the class definition and the paint() method to produce your Logo.

    Remember to use stepwise refinement as you are developing your program. After each Java statement or two, Repeatedly compile your program and run the appletviewer to check for errors. The commands you will use are the following:

        javac Logo.java 
        appletviewer samplepage.html
    

    Have your work checked by the TA or instructor.

    Creating and Publishing Your Web Page

    Step 5. Editing Your Web Page.

    Edit samplepage.html, putting your own name on it and saving it as mypage.html. To see how your page looks, open it with your browser.

    Step 6. Publishing Your Web Page.

    Web pages on the CPSC system must be stored in a directory named public_html. Any pages stored in public_html and its subdirectories can be accessed by a web browser. Create a directory of that name in your home directory.

    Copy the Logo.class and the mypage.html files into public_html. Rename the html file index.html. This is the name of the default web page that the Web server will load if you don't specify a specific web page to load.

    To see how your page looks, open it with your browser. The full URL for your page will be:

      http://www.cs.trincoll.edu/~youraccount/index.html
    

    Your page should also appear if you use the following URL:

      http://www.cs.trincoll.edu/~youraccount/
    

    Note that public_html does NOT occur in the URL.

    Hand In.

    Upon completion of your laboratory exercises, have your work checked by the laboratory instructor or TA. Print out and hand in copies of your Logo.java and index.html files. Don't forget to document your Java program with comments.

    You're done. Great work!