public java.awt.Dimension getPreferredSize(DrawContext dc) {
    java.awt.Dimension imageSize = this.getImageSize(dc);
    java.awt.Dimension insetSize = null;

    // Optionally set the annotation's inset size to the image size.
    if (this.isFitSizeToImage()) {
      insetSize = imageSize;
    }

    // Fallback to the superclass preferred size.
    if (insetSize == null) {
      insetSize = super.getPreferredSize(dc);

      // Optionally set the annotation's aspect ratio to that of the image. We'll use the superclass
      // width, and
      // override it's height to match the image's aspect ration.
      if (this.isUseImageAspectRatio() && imageSize != null) {
        double aspect = imageSize.getHeight() / imageSize.getWidth();
        insetSize =
            new java.awt.Dimension(insetSize.width, (int) Math.round(aspect * insetSize.width));
      }
    }

    java.awt.Insets insets = this.getAttributes().getInsets();
    return new java.awt.Dimension(
        insetSize.width + (insets.left + insets.right),
        insetSize.height + (insets.top + insets.bottom));
  }