The Essential JViews Framework > Graphic Objects > Geometric Properties > The boundingBox Method

The bounding box defines the smallest rectangle encompassing the graphic object. It is returned by the following method:

public IlvRect boundingBox(IlvTransformer t)  

The IlvTransformer parameter is the 2D transformation matrix used to draw the object in a particular drawing port (see transformer). This transformation may correspond to a zoom, a rotation, or a translation of the graphic object in the destination drawing port. The method must then return the rectangle that contains the graphic object when it is drawn using the specified transformation.

images/boundingbox.gif

Figure 4.2 The Bounding Box of a Graphic Object

The following example defines the shape of a graphic object with the drawrect field. In order to return the bounding box of the object, the boundingBox method simply applies the transformer to the rectangle:

class MyRectangle extends IlvGraphic
{
   // The geometric rectangle that defines the object.
   final IlvRect drawrect = new IlvRect();
 
   //constructor
   public MyRectangle(float x, float y, float width, float height)
   {
    drawrect.reshape(x, y, width, height);
   }
 
   // The bounding box method.
   public IlvRect boundingBox(IlvTransformer t)
   {
      //Copies the original rectangle to avoid its modification
      IlvRect rect = new IlvRect(drawrect);
      if (t != null)
        t.apply(rect);
      return rect;
   }
}

The method boundingBox is a very important method. Since it is called very frequently, it must be written in a highly optimized way.

Note
For the MyRectangle class to compile correctly you need to overload the draw, copy and applyTransform methods. For an example of how this is done, see Creating a New Graphic Object Class.