Java Draw Image drawImage(Image image, Dimension windowSize, Graphics graphics)

Here you can find the source of drawImage(Image image, Dimension windowSize, Graphics graphics)

Description

draw Image

License

Open Source License

Declaration

public static double[] drawImage(Image image, Dimension windowSize, Graphics graphics) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

public class Main {
    public static double[] drawImage(Image image, Dimension windowSize, Graphics graphics) {
        double size[] = getResize(image, windowSize);
        graphics.drawImage(image, 0, 0, (int) size[0], (int) size[1], null);
        return size;
    }/*from   w ww. jav  a  2  s.c  o m*/

    /**
     * @param image the source image
     * @param windowSize maximum space to fit the image
     * @return {width, height, used scale}
     */
    public static double[] getResize(Image image, Dimension windowSize) {
        double w = image.getWidth(null);
        double h = image.getHeight(null);
        double f = 1;
        double fw = w / windowSize.getWidth();
        double fh = h / windowSize.getHeight();
        if (fw > 1 || fh > 1) {
            if (fh > fw) {
                w /= fh;
                h = windowSize.getHeight();
                f = fh;
            } else {
                w = windowSize.getWidth();
                h /= fw;
                f = fw;
            }
        }
        return new double[] { w, h, 1 / f };
    }
}

Related

  1. drawImage(final Graphics2D graphics, final Image image, final Rectangle rectangle)
  2. drawImage(Graphics g, BufferedImage im, Rectangle dst, Rectangle src)
  3. drawImage(Graphics graphics, Component comp, Image image)
  4. drawImage(Graphics2D g, BufferedImage image, int x, int y)
  5. drawImage(Graphics2D g2d, BufferedImage image, Rectangle dispArea)
  6. drawImage(Image image, Graphics g, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2)
  7. drawImage(Image img, double x, double y, Graphics2D graphics)
  8. drawImageClip(Graphics g, BufferedImage image, ImageObserver observer)
  9. drawImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink)