/*
//  FileName: GraphOval.java
//  
//  Vijay Konkimalla
//  07 Dec 95
//
//  Objective: 
//
//  $Author: konki $
//  $Log: GraphOval.java,v $
# Revision 1.3  1996/04/01  21:40:38  konki
# Added capability to display handles plus verify the selected handles
#
# Revision 1.2  1996/03/11  21:02:12  konki
# *** empty log message ***
#
# Revision 1.1  1996/02/18  23:10:21  konki
# Initial revision
#
# Revision 1.1  1996/02/18  23:10:21  konki
# Initial revision
#
//  $Id: GraphOval.java,v 1.3 1996/04/01 21:40:38 konki Exp $
*/

package wb;

//import  GraphUnit;

import  java.awt.*;
import  java.io.*;

/**
 * Oval/Ellipse in its native form
 * Mapping (xmin, ymin) -->  (top left) of the rectangle that encloses the oval
 *         (xmax, ymax) -->  (width, height) of the enclosing rectangle
 */
public class GraphOval extends GraphUnit
{
    /**
     * Base Constructor provided for sub-classing, with only the Type 
     * set to OVALS (as defined in interface GTypes)
     * @see GTypes
     */
    GraphOval()
    {
        type = OVALS;
        select = false;
        hashkey = hashCode();
    }

    /**
     * Creates an Instance of Partial Oval with its starting point
     * the top left set along with the Color and type
     * @param x1 the x coordinate of the starting point
     * @param y1 the y coordinate of the starting point
     * @param arg the color of this rectangle.      
     * @see #addnext
     * @see GTypes
     */
    public GraphOval( int x1, int y1, Color arg )
    {
        xmin = x1;
        ymin = y1;
        type = OVALS;
        color = arg;
        select = false;
        hashkey = hashCode();
    }
    
    /**
     * Creates a Oval that is complete in all aspects of its definition
     * although the enclosing rectangle arguments are start and end points of the 
     * user interaction, it appropriately identifies the left top and width 
     * and height
     * @param x1 the x coordinate of the starting point
     * @param y1 the y coordinate of the starting point
     * @param x2 the x coordinate of the end point
     * @param y2 the y coordinate of the end point
     * @param arg the color of this oval.      
     */
    public GraphOval( int x1, int y1, int x2, int y2, Color arg )
    {
        xmin = x1 < x2 ? x1 : x2;
        ymin = y1 < y2 ? y1 : y2;
        xmax = (x2-x1) < 0 ? (x1-x2) : (x2-x1);
        ymax = (y2-y1) < 0 ? (y1-y2) : (y2-y1);
        color = arg;
        type = OVALS;
        select = false;
        hashkey = hashCode();
    }

    /**
     * Creates an instance of the GraphOval by reading the data from the 
     * Input Stream. It then fills up the member variables of the class
     * @param din DataInputStream the stream from which the data is read
     * @exception IOException if an I/O error occurs
     * @see ByteUnPackInputStream
     * @see #packetize
     */
    public GraphOval( DataInputStream din ) throws IOException
    {
        basedepacketize( din ); 
        buis = null;
        select = false;
    }

    /**
     * Add next assumes that the input is referred to as the final point of 
     * user's description of the enclosing rectangle's diagonal. Hence, it 
     * corrects the data in xmin, ymin  such that they map to top left 
     * and xmax, ymax to width and height respectively.
     * @param x int x-coordinate 
     * @param y int y-coordinate
     */
    public void addnext( int x, int y)
    {
        xmin = xmin < x ? xmin : x;
        ymin = ymin < y ? ymin : y;
        xmax = (x-xmin) < 0 ? (xmin-x) : (x-xmin);  // width of Oval
        ymax = (y-ymin) < 0 ? (ymin-y) : (y-ymin);  // height of Oval
    }
    
    /**
     * renders the Oval to the graphics using the current color (this) 
     * @param g Graphics to which it is rendered.
     */
    public void paint( Graphics g )
    {
        g.setColor(color);
        g.setPaintMode();
        g.drawOval(xmin, ymin, xmax, ymax);
        if (select) {
            g.setColor(Color.lightGray);	
            g.fillRect( (xmin-3 < 0) ? xmin : xmin-3, 
                        (ymin-3 < 0) ? ymin : ymin-3, 6, 6);
            g.fillRect( (xmin-3 < 0) ? xmin : xmin-3, 
                        (ymax+ymin-3 < 0) ? ymax+ymin : ymax+ymin-3, 6, 6);
            g.fillRect( (xmax+xmin-3 < 0) ? xmax+xmin : xmax+xmin-3, 
                        (ymin-3 < 0) ? ymin : ymin-3, 6, 6);
            g.fillRect( (xmax+xmin-3 < 0) ? xmax+xmin : xmax+xmin-3, 
                        (ymax+ymin-3 < 0) ? ymax+ymin : ymax+ymin-3, 6, 6);
        }
    }
    
    /**
     * renders the Oval to the graphics using the background color (this) 
     * in XOR mode.
     * @param g Graphics to which it is rendered.
     * @param arg COlor of the background
     */
    public void erase( Graphics g, Color arg )
    {
        g.setColor(color);
        g.setXORMode(arg);
        g.drawOval(xmin, ymin, xmax, ymax);
        if (select) {
            g.setColor(Color.lightGray);	
            g.setXORMode(arg);
            g.fillRect( (xmin-3 < 0) ? xmin : xmin-3, 
                        (ymin-3 < 0) ? ymin : ymin-3, 6, 6);
            g.fillRect( (xmin-3 < 0) ? xmin : xmin-3, 
                        (ymax+ymin-3 < 0) ? ymax+ymin : ymax+ymin-3, 6, 6);
            g.fillRect( (xmax+xmin-3 < 0) ? xmax+xmin : xmax+xmin-3, 
                        (ymin-3 < 0) ? ymin : ymin-3, 6, 6);
            g.fillRect( (xmax+xmin-3 < 0) ? xmax+xmin : xmax+xmin-3, 
                        (ymax+ymin-3 < 0) ? ymax+ymin : ymax+ymin-3, 6, 6);
        }
    }

    /**
     * method to verify if the passed point is enclosed in the handles 
     * of this Object.
     * @param x x-coordinate of the point
     * @param y y-coordinate of the point
     * @return boolean true iff the point is enclosed in the handles
     */
    public boolean isInside( int x, int y)
    {
        if( ( x >= (xmin-3 < 0 ? xmin : xmin-3) ) && ( x <= xmin+3 ) 
            && ( y >= (ymin-3 < 0 ? ymin : ymin-3) ) && ( y <= ymin+3 ) ) {
            return true;
        }
        if( ( x >= (xmax+xmin-3 < 0 ? xmax+xmin : xmax+xmin-3) ) 
            && ( x <= xmax+xmin+3 ) 
            && ( y >= (ymin-3 < 0 ? ymin : ymin-3) ) && ( y <= ymin+3 ) ) {
            return true;
        }
        if( ( x >= (xmax+xmin-3 < 0 ? xmax+xmin : xmax+xmin-3) ) 
            && ( x <= xmax+xmin+3 ) 
            && ( y >= (ymax+ymin-3 < 0 ? ymax+ymin : ymax+ymin-3) ) 
            && ( y <= ymax+ymin+3 ) ) {
            return true;
        }
        return( ( x >= (xmin-3 < 0 ? xmin : xmin-3) ) 
            && ( x <= xmin+3 ) 
            && ( y >= (ymax+ymin-3 < 0 ? ymax+ymin : ymax+ymin-3) ) 
            && ( y <= ymax+ymin+3 ) ); 
    }

}

/*
 * End of File: GraphOval.java
 */