Java Draw Image drawImageInRect(Graphics2D gr, Image img, Rectangle r, boolean noShrink)

Here you can find the source of drawImageInRect(Graphics2D gr, Image img, Rectangle r, boolean noShrink)

Description

draw an image in a rectangle

License

Apache License

Parameter

Parameter Description
gr non-null graphic context
img non-null image
r non-null rectangle
obs non-null observer

Declaration

public static void drawImageInRect(Graphics2D gr, Image img, Rectangle r, boolean noShrink) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.*;

import java.awt.geom.Rectangle2D;
import java.awt.image.*;

public class Main {
    private static ImageObserver gObserver;

    /**/* www. j av  a  2s  .  c  om*/
     * draw an image in a rectangle
     *
     * @param gr  non-null graphic context
     * @param img non-null image
     * @param r   non-null rectangle
     * @param obs non-null observer
     */
    public static void drawImageInRect(Graphics2D gr, Image img, Rectangle r, boolean noShrink) {
        gr.drawImage(img, r.x, r.y, r.width, r.height, null);
        if (noShrink) {
            int imgwidth = img.getWidth(null);
            int imgheight = img.getHeight(null);
            if (imgwidth > r.width && imgheight > r.height) {
                Shape oldclip = gr.getClip();
                Rectangle2D clip = new Rectangle2D.Double(r.getX(), r.getY(), r.getWidth(), r.getHeight());
                gr.clip(clip);
                gr.drawImage(img, r.x, r.y, imgwidth, imgheight, gObserver);
                gr.clip(oldclip);
            } else
                gr.drawImage(img, r.x, r.y, r.width, r.height, gObserver);
        } else {
            gr.drawImage(img, r.x, r.y, r.width, r.height, null);
        }

    }
}

Related

  1. drawImage(Image img, double x, double y, Graphics2D graphics)
  2. drawImageClip(Graphics g, BufferedImage image, ImageObserver observer)
  3. drawImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink)
  4. drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, double xfactor, double yfactor)
  5. drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor, double yfactor)
  6. drawImageWithClipping(Graphics g, BufferedImage img)
  7. drawInMiddle(Graphics g, Image image, String text)
  8. drawLabel(BufferedImage image, String text)
  9. drawNormalImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer, int imageWidth, int imageHeight)