Code for Class sg.Pixel with an
Emphasis on Exception Handling






public class Pixel{

    int x;
    int y;

    public Pixel(){
        x = y = 0; 
    }

    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 );
    }

    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 + "]";
    }

}