Example #1
0
 public Rectangle getRectangle(final IModelSpaceCanvas canvas) {
   Rectangle rect = _image.getBounds();
   int w = rect.x;
   int h = rect.y;
   int numPoints = getPointCount();
   if (numPoints == 1) {
     ImageAnchor anchor = getAnchorType();
     Point2D.Double m = new Point2D.Double(getPoint(0).getX(), getPoint(0).getY());
     Point2D.Double p = new Point2D.Double(0, 0);
     canvas.transformModelToPixel(getModelSpace(), m.getX(), m.getY(), p);
     if (anchor.equals(ImageAnchor.Center)) {
       rect = new Rectangle((int) (p.x - w / 2), (int) (p.y - h / 2), w, h);
     } else if (anchor.equals(ImageAnchor.UpperLeft)) {
       rect = new Rectangle((int) p.x, (int) p.y, w, h);
     } else if (anchor.equals(ImageAnchor.UpperRight)) {
       rect = new Rectangle((int) (p.x - w), (int) p.y, w, h);
     } else if (anchor.equals(ImageAnchor.LowerLeft)) {
       rect = new Rectangle((int) p.x, (int) (p.y - h), w, h);
     } else if (anchor.equals(ImageAnchor.LowerRight)) {
       rect = new Rectangle((int) (p.x - w), (int) (p.y - h), w, h);
     } else {
       throw new RuntimeException("Invalid image anchor: " + anchor);
     }
   } else if (numPoints >= 2) {
     int xmin = Integer.MAX_VALUE;
     int xmax = -Integer.MAX_VALUE;
     int ymin = Integer.MAX_VALUE;
     int ymax = -Integer.MAX_VALUE;
     for (int i = 0; i < numPoints; i++) {
       Point2D.Double m = new Point2D.Double(getPoint(i).getX(), getPoint(i).getY());
       Point2D.Double p = new Point2D.Double(0, 0);
       canvas.transformModelToPixel(getModelSpace(), m.getX(), m.getY(), p);
       xmin = Math.min(xmin, (int) p.getX());
       xmax = Math.max(xmax, (int) p.getX());
       ymin = Math.min(ymin, (int) p.getY());
       ymax = Math.max(ymax, (int) p.getY());
     }
     w = xmax - xmin + 1;
     h = ymax - ymin + 1;
     rect = new Rectangle(xmin, ymin, w, h);
   } else {
     throw new RuntimeException("Invalid image definition.");
   }
   return rect;
 }