Boxball.javaTopMagnetGame DesignMagnet Design

Magnet Design

 * Name:  Joe Cool
 * Lab:   Lab 2 (magnets)
 * Purpose:  Magnet models a physical magnet with a north and south
 *           pole.  A magnet can move and it can interact with other
 *           magnets through attraction and repulsion.
public class Magnet {
    /* The dimensions of the magnet. */
    private final static double MAGNET_WIDTH = 150;
    private final static double MAGNET_HEIGHT = 50;

    /* Distance from pole to magnet perimeter */
    private static final double POLE_DISTANCE = MAGNET_HEIGHT / 2;

    /* The rectangle representing the outline of the magnet */
    private FramedRect box;

    /* The poles of the magnet */
    private Pole northPole, southPole;
    // Creates a new magnet
    // Parameters:
    //    point - the upper left corner of the magnet
    //    canvas - the canvas that the magnet will be displayed on
    public Magnet (Location point, DrawingCanvas canvas) {
        // Draw the rectangle
        // Create the north and south poles offset by POLE_DISTANCE
        // within the rectangle

    // Return the upper left corner of the magnet.
    public Location getLocation() {
        // return the location of the box;

    // Move the magnet relative to its current position.
    // Parameters:
    //    xoff - the x offset in the movement
    //    yoff - the y offset in the movement
    public void move(double xoff, double yoff) {
        // Move the rectangle and the two poles by the same offset.

    // Move the magnet so that its upper left corner is at the given
    // point.
    // Parameters:
    //   point - the new upper left corner for the magnet
    public void moveTo( Location point) {
        // Calculate an offset from the point to the current box
        // location.  Call the move method using the calculated
        // offset.

    // Returns true if point is within the bounds of the magnet.
    // Parameters:
    //   point - the point to check for containment
    public boolean contains (Location point) {
        // Return whether the box contains point.

    // Swaps the north and south poles of the magnet.
    public void flip ()  {
        // Remember the x coordinate where the north and south poles
        // currently are in local variables.  Compute the distance
        // between those x coordinates.  Move the poles that distance
        // in the x coordinate and 0 in the y distance.  

    // Return the north pole of the magnet
    public Pole getNorth () {
        return northPole;
    // Return the south pole of the magnet
    public Pole getSouth()  {
        return southPole;

    // Cause another magnet to move if it is close enough to cause
    // a magnet reaction.
    // Parameter:  other - the magnet to check for a reaction
    public void interact (Magnet other)  {
        // Check for attraction between the opposite poles of this
        // magnet and the other magnet.
        // Check for repulsion between similar poles of this
        // magnet and the other magnet.

Boxball.javaTopMagnetGame DesignMagnet Design