CS21 Week14 In-Class Exercises

Create a week14 subdirectory in your cs21 subdirectory, and from your week14 subdirectory, copy over my week14 files:

% cd  
% cd cs21
% mkdir week14
% cd week14
% pwd 
% cp ~newhall/public/cs21/week14/* .   

  1. Applets are java GUI programs that can be added to any webpage. In the source file add the lines:
    import javax.swing.JApplet;
    import java.awt.*;
    import java.util.*;
    
    Then, instead of adding a main method, you will implement a class that is derived from JApplet (extends JApplet) and implements a method named paint. The paint method is where your code to "draw" the gui is located:
    public class MyCoolApplet extends JApplet {
    
        public void paint (Graphics page) {
    
    	// invoke methods on the Graphics object to draw something
    	page.drawRect(50, 50, 40, 40);
    
        }
    }
    

  2. We can use methods of the Graphics class to draw to the Graphics object's coordinate system:
                             x
     (0,0) *------------------------>  x-axis (positive values)
           |                 |
           |                 |
           |                 |
         y |-----------------* (x, y)
           |
           |
           \/  y-axis  (positive values)
    

    Here are some Graphics class methods:

    setColor(Color c) 
        Sets this graphics context's current color to the specified color.  (Color class)
    
    drawLine(int x1, int y1, int x2, int y2) 
         Draws a line, using the current color, between the points (x1, y1) and 
         (x2, y2) in this graphics context's coordinate system.
    
    drawRect(int x, int y, int width, int height)
         Draws the outline of the specified rectangle with (x,y) upper lft corner 
    
    drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) 
         Draws the outline of a elliptical arc covering the specified rectangle.
         The top left corner of the bounding rectangle is at (x,y)
    
    fillRect(int x, int y, int width, int height) 
         Fills the specified rectangle.
    
    fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) 
         Fills a circular or elliptical arc covering the specified rectangle.
    
    fillOval(int x, int y, int width, int height) 
         Fills an oval bounded by the specified rectangle w/current color
    

  3. The applet needs to be attached to a webpage. Create a .html document that attaches it. For example, here is the contents of a file named mycoolapp.html:
    <html>
    <body>
    
    <h1>a title at the top of my webpage, using h1 size font</h1>
    
    <applet code="MyCoolApplet.class" width=350 height=175></applet>
    
    Here is just some text that would appear after my applet on the webpage.
    
    </body>
    </html>
    

  4. To run the applet, compile the java code, then either load the .html file in a webbrowser, or run the applet viewer. The appletviewer only displays the applet and not any other content on the webpage, the webbrowser caches copies of .class files that it loads, and as a result may not display the latest version of your code. In general, you should do your java programming development using appletveiwer, and once your applet is complete test the final layout of your webpage with the applet using the webbrowser. Here is how to run both:
    % appletviewer mycoolapp.html
    
    OR
    
    % firebird &
    # then choose File->Open File and select mycoolapp.html to open
    

  5. If you want to try out more features of Java's GUI support, check out the Swing Tutorial