/** * Returns the preferred size of this Thumbnail. The preferred size will be calculated in a way * that maintains the source Figure's aspect ratio. * * @param wHint The width hint * @param hHint The height hint * @return The preferred size */ public Dimension getPreferredSize(int wHint, int hHint) { if (prefSize == null) return adjustToAspectRatio(getBounds().getSize(), false); Dimension preferredSize = adjustToAspectRatio(prefSize.getCopy(), true); if (maxSize == null) return preferredSize; Dimension maximumSize = adjustToAspectRatio(maxSize.getCopy(), true); if (preferredSize.contains(maximumSize)) return maximumSize; else return preferredSize; }
/** * Returns the scaled Image of the source Figure. If the Image needs to be updated, the * ThumbnailUpdater will notified. * * @return The thumbnail image */ protected Image getThumbnailImage() { Dimension oldSize = targetSize; targetSize = getPreferredSize(); targetSize.expand(new Dimension(getInsets().getWidth(), getInsets().getHeight()).negate()); setScales( targetSize.width / (float) getSourceRectangle().width, targetSize.height / (float) getSourceRectangle().height); if ((isDirty()) && !updater.isRunning()) updater.start(); else if (oldSize != null && !targetSize.equals(oldSize)) { revalidate(); updater.restart(); } return thumbnailImage; }
private Dimension adjustToAspectRatio(Dimension size, boolean adjustToMaxDimension) { Dimension sourceSize = getSourceRectangle().getSize(); Dimension borderSize = new Dimension(getInsets().getWidth(), getInsets().getHeight()); size.expand(borderSize.getNegated()); int width, height; if (adjustToMaxDimension) { width = Math.max( size.width, (int) (size.height * sourceSize.width / (float) sourceSize.height + 0.5)); height = Math.max( size.height, (int) (size.width * sourceSize.height / (float) sourceSize.width + 0.5)); } else { width = Math.min( size.width, (int) (size.height * sourceSize.width / (float) sourceSize.height + 0.5)); height = Math.min( size.height, (int) (size.width * sourceSize.height / (float) sourceSize.width + 0.5)); } size.width = width; size.height = height; return size.expand(borderSize); }