<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 * Steve Revilak
 * cs680                Fall 2001
 * Assignment 4
 * File: ~srevilak/cs680/p4/Pocket.java
 *
 * $Id: Pocket.java,v 1.8 2001/11/26 00:53:56 srevilak Exp $
 */

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

/**
 * A pocket on the pooltable.
 *
 * A pocket knows where it is located, and how to draw itself.  A
 * pocket also knows how to tell if a ball has rolled into it, and how
 * to emit a little flash when that does happen.
 *
 * @author Steve Revilak
 * @version $Id: Pocket.java,v 1.8 2001/11/26 00:53:56 srevilak Exp $
 */
public class Pocket {

  /**
   * The x coordinate of the vertex of this pocket.
   */
  private int x;

  /**
   * The y coordinate of the vertex of this pocket.
   */
  private int y;

  /**
   * The radius of a pocket (in pixels).
   */
  private static final int POCKETRADIUS = 9;

  /**
   * The color of a pocket
   */
  private static Color POCKETCOLOR = new Color(0x33, 0x00, 0x33);

  /**
   * Color to flash when a ball rolls into the pocket.
   */
  private Color flashColor;

  /**
   * Used for flashing -- when a ball rolls into the pocket, this
   * value is initialized to a non-negative value, and decremented
   * each time the ball is redrawn.  draw() uses this value to
   * determine what color to make itself.
   */
  private int flashCounter;

  /**
   * Initial value for flashCounter when a ball rolls into the pocket.
   */
  private static int INIT_FLASHCOUNT = 16;

  /**
   * Construct a new pocket, whose location is at (x, y).
   */
  public Pocket(int x, int y) {
    this.x = x;
    this.y = y;
    this.flashColor = null;
    this.flashCounter = 0;
  }

  /**
   * Ask the pocket to draw itself on the given graphics object.
   */
  public void draw(Graphics g) {
    int r = Pocket.getRadius();
    Color drawColor = Pocket.POCKETCOLOR;
    if (flashCounter &gt; 0) {
      flashCounter--;
      if (flashCounter % 2 != 0) {
	drawColor = flashColor;
      }
    }
    g.setColor(drawColor);
    g.fillOval(x - r, y - r, 2 * r, 2 * r);
  }
  

  /**
   * Ask the pocket what it's radius is.
   *
   * @return the radius of the pocket
   */
  static int getRadius() {
    return POCKETRADIUS;
  }

  /**
   * Return a point containing the coordinates of the vertex of this
   * Pocket.
   *
   * @return A point representing the vertex of the pocket
   */
  public Point getVertex() {
    return new Point(x, y);
  }

  /**
   * Test to see if the given ball fell in the pocket.
   * We do this by checking the distance between the balls vertex and
   * the center of the pocket.
   * If that distance &lt;= the radius of the pocket, the balls center of
   * gravity is over the hole, and it falls in.
   */
  public boolean isBallInPocket(Ball b) {
    Point pocketLoc = getVertex();
    Point ballLoc   = b.getVertex();
    double distance = MotionUtils.distance(pocketLoc, ballLoc);
    if (distance &lt;= Pocket.getRadius()) {
      return true;
    }
    return false;
  }

  /**
   * Inform the pocket that a ball has fallen into it.
   *
   * @param b the ball that fell into the pocket
   */
  public void pocketBall(Ball b) {
    this.flashColor = b.getColor();
    this.flashCounter = INIT_FLASHCOUNT;
    return;
  }

  /**
   * Ask the pocket if it is flashing.
   *
   * @return true if the pocket is flashing, false otherwise.
   */
  public boolean isFlashing() {
    if (flashCounter &gt; 0) {
      return true;
    }
    return false;
  }

}
</pre></body></html>