/*
 * Pixel.java    2.0 97/06/07
 * Copyright (c) 1997, B. F. Caviness
 *
 * This software may be copied for any non-commercial educational use
 * as long as it contains this copyright notice.
 *
 * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED.  THE AUTHOR
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY THE COPYEE AS A
 * RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */


package sg;

/**
 * The <code>class Pixel</code> defines an object representing
 * the coordinates of a pixel for a simple graphics device.
 * In a pixel coordinate system the x-coordinate increases as the pixel
 * moves horzionally to the right, and the y-coordinate increases as the
 * pixel moves vertically down.
 *
 * @version	2.0, 7 June 1997
 * @author	Bob Caviness
 */

public class Pixel{
    /**
     * The x coordinate
     */
    int x;

    /**
     * The y coordinate
     */
    int y;

    /**
     * Constructs and initializes a Pixel initialized with (0, 0).
     */
    public Pixel(){
        x = y = 0; 
    }

    /**
     * Constructs and initializes a Pixel with the same location as
     * the specified Pixel.
     *
     * @param p a pixel
     *
     * @exception PixelException Throws exception if either coordinate
     * of the pixel is negative.
     */
    public Pixel(Pixel p) 
        throws PixelException
    {
        if ( p.x >= 0 && p.y >= 0 ) {
            x = p.x; y = p.y;
        }
        else
            throw new PixelException( p.x, p.y );
    }

    /**
     * Constructs and initializes a Pixel from the specified x and y
     * coordinates.
     *
     * @param x the x coordinate
     * @param y the y coordinate
     *
     * @exception PixelException  Throws exception if either coordinate,
     * x or y, is negative.
     */
    public Pixel(int x, int y) 
        throws PixelException
    {
        if ( x >= 0 && y >= 0 ) {
            this.x = x;
            this.y = y;
        }
        else
            throw new PixelException( x, y );
    }

    /**
     * Moves the pixel to the specified location.
     * @param x  the x coordinate of the new location
     * @param y  the y coordinate of the new location
     * @return void
     */
    public void move(int x, int y) {
        this.x = x;
        this.y = y;
    }

    /**
     * Translates the pixel.
     * @param x translate the x coordinate by this value
     * @param y translate the y coordinate by this value
     * @return void
     */
    public void translate(int x, int y) {
        this.x += x;
        this.y += y;
    }

    /**
     * Checks whether two pixels are equal.
     * @param p check to see if object is equal to p.
     * @return true if the coordinates of the two pixels are equal; false
     * otherwise.
     */
    public boolean equals(Pixel p) {
        return ( p.x == x && p.y == y );
    }

    /**
     * Returns the String representation of the object Pixel.
     * @return String
     */
    public String toString() {
        return getClass().getName() + "[x=" + x + ",y=" + y + "]";
    }

}
