The Essential JViews Framework > Graphic Objects > Creating a New Graphic Object Class > Testing for a Point Inside an Object

When drawing a ILOG JViews graphic objects you often need to validate the presence of a point inside the object. The ShadowEllipse example implements the contains method. It returns true if the specified point is located within the main ellipse. All the coordinates are specified relative to the coordinate system of the view.

 /**
   * Tests whether a point lies within the shape of the object.
   * This method will be called when you click on the object.
   * @param p The point where user clicks in the object's coordinate system.
   * @param tp Same point as 'p' but transformed by transformer 't'
   * @param t The transformer used to draw the object.
   */
  public boolean contains(IlvPoint p, IlvPoint tp,
                          IlvTransformer t)
  {
    // We want to allow the user to click on the main ellipse
    // but not on the shadow ellipse. 
    // This method will return true when the clicked point is
    // on the main ellipse.
    // We first compute the bounding rectangle of the main
    // ellipse in the view coordinate system, just like in the
    // method draw.
    IlvRect r = new IlvRect(drawrect);
             
    if (t != null) 
      t.apply(r);
               
    int thick = thickness;
         
    if ((r.width <= thick) || (r.height <= thick))
      thick = (int)Math.min(r.width, r.height);
 
    r.width  -= thick;
    r.height -= thick;
 
    // Then we call PointInFilledArc that will return true
    // if the point is in the ellipse. 'r' and 'tp' are both
    // in the view coordinate system.
    return IlvArcUtil.PointInFilledArc(tp, r, (float)0, (float)360);
  }