Java Draw Image drawTransparentImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink, double transparency)

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

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
transparency 1.0 is opaque 0.0 is transparent

Declaration

public static void drawTransparentImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink,
        double transparency) 

Method Source Code

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

import java.awt.*;

import java.awt.geom.Ellipse2D;

public class Main {
    /**//from  w ww  . j a v a  2  s .c  o  m
     * 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
     * @param transparency 1.0 is opaque 0.0 is transparent
     */
    public static void drawTransparentImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink,
            double transparency) {
        Composite oldComposite = gr.getComposite();
        gr.setComposite(oldComposite);
        Composite newComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) transparency);
        gr.setComposite(newComposite);
        drawImageInOval(gr, img, r, noShrink);
        gr.setComposite(oldComposite);
    }

    /**
     * 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 drawImageInOval(Graphics2D gr, Image img, Rectangle r, boolean noShrink) {
        Shape oldclip = gr.getClip();
        Ellipse2D clip = new Ellipse2D.Double(r.getX(), r.getY(), r.getWidth(), r.getHeight());
        gr.clip(clip);
        if (noShrink) {
            int imgwidth = img.getWidth(null);
            int imgheight = img.getHeight(null);
            if (imgwidth > r.width && imgheight > r.height)
                gr.drawImage(img, r.x, r.y, imgwidth, imgheight, null);
            else
                gr.drawImage(img, r.x, r.y, r.width, r.height, null);
        } else {
            gr.drawImage(img, r.x, r.y, r.width, r.height, null);
        }
        gr.clip(oldclip);
    }
}

Related

  1. drawScaledImage(Graphics graphics, BufferedImage image, int x, int y, int w, int h, int left, int right)
  2. drawScaledImage(Image image, Component canvas, Graphics g)
  3. drawSquare(BufferedImage image, int x, int y, int size, int pixel)
  4. drawStretchedImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer)
  5. drawTiledImage(Rectangle2D bounds, Graphics2D g2, Image image, ImageObserver observer, int imageWidth, int imageHeight, int tilePadding)
  6. drawVerticalBar(final BufferedImage image, final double max, final double min)
  7. drawVerticalLineAt(int x, int dottedLine, Color color, BufferedImage displayMap)
  8. drawVolatileImage(Graphics2D g, VolatileImage volatileImage, int x, int y, Image orig)