Session 4: Graphics

Program Output

In software engineering actors are users or systems that a program interacts with. Actors are represented in diagrams as stick figures.

In this session you and your partner will create an actor shape that can be displayed in a graphics window. Users can specify the size and position of the actor shape.

Here is what your program output should look like:

Your ActorShape class will be very similar to the MouseShape class developed at the bottom of:

http://www.cs.sjsu.edu/faculty/pearce/cs46a/graphics.htm

What to submit

Send the complete declaration of ActorShape in an email to cs46a_grader@yahoo.com. The subject of the email should be "session 4". Be sure to include your partner's name in the email.

Includes

Here are some include statements you will need:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

Actor Viewer

Here is a standard viewer class you will need:

public class ActorViewer {
   public static final int FRAME_WIDTH = 300;
   public static final int FRAME_HEIGHT = 400;
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("Frame Viewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ActorComponent component = new ActorComponent();
      frame.add(component);
      frame.setVisible(true);
   }
}

Actor Component

Here is the component class you will use:

public class ActorComponent extends JComponent {
   private ActorShape actor1;
   private ActorShape actor2;
   private ActorShape actor3;
   public ActorComponent() {
      actor1 = new ActorShape(10, 10, 100);
      actor2 = new ActorShape(110, 10, 50);
      actor3 = new ActorShape(160, 10, 200);
   }
   public void paintComponent(Graphics gc) {
      Graphics2D gc2d = (Graphics2D)gc;
      actor1.draw(gc2d);
      actor2.draw(gc2d);
      actor3.draw(gc2d);
   }
}

ActorShape

Here is the start of the shape class that you must complete:

public class ActorShape {

   private int xc, yc, width;
   private Ellipse2D.Double face;
   private Color faceColor;
   private Line2D.Double arms, leftLeg, rightLeg, torso;

   public ActorShape(int x, int y, int w) {
       // initialize all fields here
   }
   public void draw(Graphics2D gc) {
      Color oldColor = gc.getColor();
      // gc draws face, torso, arms, and legs
      gc.setColor(oldColor);
   }
}

Note that the bounding box of an actor is a square:

Before you begin, use a piece of graph paper to plot the positions of the body parts relative to xc, yc, and width.