Java Draw Image drawImageClip(Graphics g, BufferedImage image, ImageObserver observer)

Here you can find the source of drawImageClip(Graphics g, BufferedImage image, ImageObserver observer)

Description

Draws the image inside the clip bounds to the given graphics object.

License

Open Source License

Declaration

public static void drawImageClip(Graphics g, BufferedImage image,
        ImageObserver observer) 

Method Source Code

//package com.java2s;

import java.awt.*;

import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

public class Main {
    /**//from w w w  .ja  v a2  s  .c  om
     * Draws the image inside the clip bounds to the given graphics object.
     */
    public static void drawImageClip(Graphics g, BufferedImage image,
            ImageObserver observer) {
        Rectangle clip = g.getClipBounds();

        if (clip != null) {
            int w = image.getWidth();
            int h = image.getHeight();

            int x = Math.max(0, Math.min(clip.x, w));
            int y = Math.max(0, Math.min(clip.y, h));

            w = Math.min(clip.width, w - x);
            h = Math.min(clip.height, h - y);

            if (w > 0 && h > 0) {
                // TODO: Support for normal images using fast subimage copies
                g.drawImage(image.getSubimage(x, y, w, h), clip.x, clip.y,
                        observer);
            }
        } else {
            g.drawImage(image, 0, 0, observer);
        }
    }
}

Related

  1. drawImage(Graphics2D g, BufferedImage image, int x, int y)
  2. drawImage(Graphics2D g2d, BufferedImage image, Rectangle dispArea)
  3. drawImage(Image image, Dimension windowSize, Graphics graphics)
  4. drawImage(Image image, Graphics g, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2)
  5. drawImage(Image img, double x, double y, Graphics2D graphics)
  6. drawImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink)
  7. drawImageInPolygon(Graphics g2d, BufferedImage img, Polygon poly, double xfactor, double yfactor)
  8. drawImageInRect(Graphics g2d, BufferedImage img, Rectangle rect, double xfactor, double yfactor)
  9. drawImageInRect(Graphics2D gr, Image img, Rectangle r, boolean noShrink)