LAB 13  (Comp 170) - AWT continued

In this lab, we continue our exploration of AWT begun in Lab 12.  We wish to develop familiarity with drawing figures which include lines, circles, ovals, rectangles, and arcs.  This will require basic geometric knowledge of the Cartesian plane.  Recall that the origin is at the upper left-hand corner of our window.

I   Our first example illustrates the use of several built-in methods in the Graphics class of AWT.  Begin by running this program to see what happens.  (You will need only one file, since there is only one class, Faces.)  Next, your task is to improve upon the "happy" face, and also create a "sad" face.  You may use the drawLine, drawOval, drawArc, and drawRect methods described in API.  This may take some time and experimentation to develop familiarity with the geometry of these methods.  For example, drawArc has six parameters.  The API description is as follows:

public abstract void drawArc(int x,  int y, int width, int height, int startAngle, int arcAngle)

Draws the outline of a circular or elliptical arc covering the specified rectangle.

The resulting arc begins at startAngle and extends for arcAngle degrees, using the current color. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive value indicates a counter-clockwise rotation while a negative value indicates a clockwise rotation.

The center of the arc is the center of the rectangle whose origin is (x, y) and whose size is specified by the width and height arguments.

The resulting arc covers an area width + 1 pixels wide by height + 1 pixels tall.

Parameters:

x - the x coordinate of the upper-left corner of the arc to be drawn.

y - the y coordinate of the upper-left corner of the arc to be drawn.

width - the width of the arc to be drawn.

height - the height of the arc to be drawn.

startAngle - the beginning angle.

arcAngle - the angular extent of the arc, relative to the start angle.
 
 

import java.awt.*;
import java.awt.event.*;

public class Faces extends Frame implements ActionListener{

    private int whichDrawing;  // whichDrawing plays the role of a flag
                                // assuming values 0, 1, or 2

    public static void main(String[ ] args) {
        Frame f = new Faces();
        f.resize(300, 300);
        f.show();
    }
     public void actionPerformed(ActionEvent e){
        String c = e.getActionCommand();
        if (c.equals("happy")){
            whichDrawing = 1;
            repaint();
        }
        else if (c.equals("sad")){
            whichDrawing = 2;
            repaint();
        }
     }
    public void paint(Graphics g) {
        if (whichDrawing ==0){
            g.setFont(new Font("Helvetica", Font.BOLD, 18));
            g.drawString( "Comedy or Tragedy?", 80, 80);
        }
        else if (whichDrawing==1){  // draw happy face
                // your task is to improve upon this face!
            g.setColor(Color.red);
            g.fillOval(100,30,100, 100);   //  face
            g.setColor(Color.blue);
            g.drawArc(130,75,50,25,0,-180);  // mouth
            g.setColor(Color.blue);
            g.fillOval(130,45, 10,10);   //  left eye
            g.setColor(Color.darkGray);
            g.drawOval(150,45, 18,13);   // right eye
            g.fillOval(154,49, 9,7);
            g.drawLine(145,85, 155,80);  // nose
            g.drawLine(145,85, 153, 85);
            g.setColor(Color.black);
            g.drawLine(150,130, 150,250);   // body
        }
        else if (whichDrawing==2){
                // your task is to draw an appropriately sad face here
            g.setColor(Color.magenta);
            g.fillOval(0,0,100, 100);
        }
    }

    public Faces( ) {
        whichDrawing = 0;
        setSize(300, 300);
        setBackground(Color.green);
        setTitle("La Joie ou La Tristesse");
        setLayout(new BorderLayout());

        Panel ButtonPanel = new Panel();

        Button happy = new Button("happy");
            happy.addActionListener(this);
            ButtonPanel.add(happy);

        Button sad = new Button("sad");
            sad.addActionListener(this);
            ButtonPanel.add(sad);

        add(ButtonPanel, "South");
    }

    public boolean handleEvent(Event e) {
    if (e.id==Event.WINDOW_DESTROY)
    System.exit(0);
    return super.handleEvent(e);
    }

}
 

II  Next, by using the drawLine method, create stick-figure bodies for each of the "heads" above.

III  If time permits, create a third button, and allow the user to select from among three emotions of your choice (for example, anger, melancholy, happiness).

IV  Next, we explore a recursive geometric design.  Run this program and notice how the start button results in calling the recursive method drawRings with a different base case each time it is clicked.  Initially the stopSize is set at 100 pixels.  When the start button is clicked, the stopSize is changed to 50 pixels; next time to 25 pixels, etc.

import java.awt.*;
import java.awt.event.*;

public class RecDrawing extends Frame implements ActionListener{
    int stopSize = 100;
    public static void main(String[] args) {
        Frame f = new RecDrawing();
        f.resize(400, 400);
        f.show();
    }
    public void actionPerformed(ActionEvent e){
         String c = e.getActionCommand();
         if (c.equals("start")){
             stopSize = stopSize/2;  // here we change
                // the base case of the recursive method
                // drawRings
             repaint();
         }
    }
    public void paint(Graphics g) {
         g.setColor(Color.white);
         g.fillRect(0, 0, 300, 300);
         g.setColor(Color.black);
         drawRings(10, 30, 200, 200, g);
    }
    public void drawRings(int x, int y, int w, int h, Graphics g){
        // this is a recursive method which ends when h or w is <= stopSize.
         if ((h > stopSize)&&(w > stopSize)){
                if (x%2==0){
                    g.setColor(Color.red);
                }
                else{
                     g.setColor(Color.green);
                }
                g.drawOval(x, y, w, h);
                drawRings(x, y+h/2,w/2, h/2, g);
                drawRings(x+w/2, y,w/2, h/2, g);
                drawRings(x+w/2, y+h/2,w/2, h/2, g);
                drawRings(x, y,w/2, h/2, g);
         }
     }

    public RecDrawing( ) {
         setSize(300, 300);
         setBackground(Color.cyan);
         setTitle("Recursive Drawing Circles");
         setLayout(new BorderLayout());

         Panel ButtonPanel = new Panel();

         Button start = new Button("start");
         start.addActionListener(this);
         ButtonPanel.add(start);
         add(ButtonPanel, "South");
     }

    public boolean handleEvent(Event e) {
        if (e.id==Event.WINDOW_DESTROY)
            System.exit(0);
        return super.handleEvent(e);
    }
}

The art of drawing is of more real importance to the human race than that of writing, because people can hardly draw anything without being of some use both to themselves and others, and can hardly write anything without wasting their own time and that of others.

- John Ruskin:  Modern Painters, 1856


Course Home Page          Department Home Page        Loyola Home Page