Java Draw Image drawStretchedImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer)

Here you can find the source of drawStretchedImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer)

Description

draw Stretched Image

License

Open Source License

Declaration

public static void drawStretchedImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer) 

Method Source Code


//package com.java2s;

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.geom.Rectangle2D;

import java.awt.image.ImageObserver;

public class Main {
    public static void drawStretchedImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer) {
        drawStretchedImage(bounds, g2, image, observer, false);
    }//from  w  w  w  . ja  v a2 s .co m

    public static void drawStretchedImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer,
            boolean keepAspect) {
        int x = (int) bounds.getX();
        int y = (int) bounds.getY();
        int w = (int) bounds.getWidth();
        int h = (int) bounds.getHeight();
        if (keepAspect) {
            int imageWidth = image.getWidth(observer);
            int imageHeight = image.getHeight(observer);
            if ((imageWidth > 0) && (imageHeight > 0)) {
                double scaleWidth = bounds.getWidth() / imageWidth;
                double scaleHeight = bounds.getHeight() / imageHeight;
                if (scaleWidth < scaleHeight) {
                    h = (int) (scaleWidth * imageHeight);
                    // center vertically
                    y = y + (int) Math.max(0, Math.round((bounds.getHeight() - h) / 2));
                } else {
                    w = (int) (scaleHeight * imageWidth);
                    // center horizontally
                    x = x + (int) Math.max(0, Math.round((bounds.getWidth() - w) / 2));
                }
            }
        }
        // scales the image
        g2.drawImage(image, x, y, w, h, observer);
    }
}

Related

  1. drawPixel(BufferedImage img, int x, int y, int color)
  2. drawPixel(int x, int y, Color color, BufferedImage displayMap)
  3. drawScaledImage(Graphics graphics, BufferedImage image, int x, int y, int w, int h, int left, int right)
  4. drawScaledImage(Image image, Component canvas, Graphics g)
  5. drawSquare(BufferedImage image, int x, int y, int size, int pixel)
  6. drawTiledImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer, int imageWidth, int imageHeight, int tilePadding)
  7. drawTransparentImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink, double transparency)
  8. drawVerticalBar(final BufferedImage image, final double max, final double min)
  9. drawVerticalLineAt(int x, int dottedLine, Color color, BufferedImage displayMap)