// Class: PersonDisplay
//
// Author: Joel Booth
// based on the PatientDisplay class written by Alyce Brady
//
//
// Created on 9 November 2004
//
// License Information:
// This class is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation.
//
// This class is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import edu.kzoo.grid.GridObject;
import edu.kzoo.grid.display.ScaledDisplay;
/**
* @author Joel Booth
* @version 9 November 2004
**/
public class PersonDisplay extends ScaledDisplay
{
private static final double HEAD_DIAMETER = .20;
private Shape head;
private Shape body;
private Shape arms;
private Shape leftLeg;
private Shape rightLeg;
/** Constructs an object that knows how to draw a patient.
**/
public PersonDisplay()
{
buildPaths(HEAD_DIAMETER);
}
/** Sets up the paths used for the patient body, head, arms, and legs.
* The various parameters should be specified assuming the patient
* will occupy a cell of size (1, 1).
* @param headDiameter circumference of the head
**/
protected void buildPaths(double headDiameter)
{
// Build a set of paths for a patient standing in a unit-length cell.
// We will scale as needed later.
// The head is a circle (ellipse, actually) of the given diameter.
head = new Ellipse2D.Double(-headDiameter/2, -3 * headDiameter/2,
headDiameter, headDiameter);
// The patient body starts below the head.
double bodyLength = 3 * headDiameter;
double combinedArmLength = 2 * bodyLength / 3;
body = new Line2D.Double(0, -bodyLength/6, 0, bodyLength/6);
arms = new Line2D.Double(-combinedArmLength/2, 0,
combinedArmLength/2, 0);
leftLeg = new Line2D.Double(0, bodyLength/6,
-bodyLength/4, bodyLength/2);
rightLeg = new Line2D.Double(0, bodyLength/6,
bodyLength/4, bodyLength/2);
}
/** Draws the given object as a stick figure; the object must be of
* the Patient class.
* (Precondition: obj
is a Patient
object.)
* @param p patient we want to draw
* @param comp component on which to draw
* @param g2 drawing surface
**/
public void draw(GridObject p, Component comp, Graphics2D g2)
{
Person person = (Person) p;
// Set the color based on the person's body color
Color color = person.getBodyColor();
g2.setPaint(color);
// Draw head
g2.draw(head);
g2.fill(head);
// Stroke stick figure in slightly darker color.
g2.setPaint(color.darker());
g2.draw(body);
g2.draw(arms);
g2.draw(leftLeg);
g2.draw(rightLeg);
}
}