Example usage for java.awt.image RenderedImage getWidth

List of usage examples for java.awt.image RenderedImage getWidth

Introduction

In this page you can find the example usage for java.awt.image RenderedImage getWidth.

Prototype

int getWidth();

Source Link

Document

Returns the width of the RenderedImage.

Usage

From source file:pcgen.gui2.tools.Utility.java

/**
 * Adjust the crop rectangle to fit within the image it is cropping. Also
 * ensure the area is square./*from w w  w  .j  a  va  2 s .  c  o m*/
 *
 * @param image    The image being cropped
 * @param cropRect The rectangle defining the cropping area. This may be updated.
 */
public static void adjustRectToFitImage(RenderedImage image, Rectangle cropRect) {
    // Make sure the rectangle is not too big
    if (cropRect.width > image.getWidth()) {
        cropRect.width = image.getWidth();
    }
    if (cropRect.height > image.getHeight()) {
        cropRect.height = image.getHeight();
    }

    // Make it square
    int dimension = Math.min(cropRect.width, cropRect.height);
    cropRect.setSize(dimension, dimension);

    // Now adjust the origin point so the box is within the image 
    if ((cropRect.x + cropRect.width) > image.getWidth()) {
        cropRect.x = image.getWidth() - cropRect.width;
    }
    if ((cropRect.y + cropRect.height) > image.getHeight()) {
        cropRect.y = image.getHeight() - cropRect.height;
    }
}