Java Graphics Draw drawPaintedShape(Graphics2D graphics, Shape shape, Paint paint, Rectangle2D paintBounds, Stroke stroke)

Here you can find the source of drawPaintedShape(Graphics2D graphics, Shape shape, Paint paint, Rectangle2D paintBounds, Stroke stroke)

Description

Draws a filled Shape with the specified Paint object.

License

Open Source License

Parameter

Parameter Description
graphics Graphics to be painted into.
shape Shape to be filled.
paint Paint to be used.
paintBounds Optional bounds describing the painted area.
stroke Stroke to be used for outlines.

Declaration

public static void drawPaintedShape(Graphics2D graphics, Shape shape, Paint paint, Rectangle2D paintBounds,
        Stroke stroke) 

Method Source Code

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

import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;

import java.awt.geom.AffineTransform;

import java.awt.geom.Rectangle2D;

public class Main {
    /**/*w w  w  . ja  va2s  .c o m*/
     * Draws a filled Shape with the specified Paint object.
     *
     * @param graphics Graphics to be painted into.
     * @param shape Shape to be filled.
     * @param paint Paint to be used.
     * @param paintBounds Optional bounds describing the painted area.
     * @param stroke Stroke to be used for outlines.
     */
    public static void drawPaintedShape(Graphics2D graphics, Shape shape, Paint paint, Rectangle2D paintBounds,
            Stroke stroke) {
        if (shape == null) {
            return;
        }
        if (stroke == null) {
            stroke = graphics.getStroke();
        }
        shape = stroke.createStrokedShape(shape);
        fillPaintedShape(graphics, shape, paint, paintBounds);
    }

    /**
     * Fills a Shape with the specified Paint object.
     *
     * @param graphics Graphics to be painted into.
     * @param shape Shape to be filled.
     * @param paint Paint to be used.
     * @param paintBounds Optional bounds describing the painted area.
     */
    public static void fillPaintedShape(Graphics2D graphics, Shape shape, Paint paint, Rectangle2D paintBounds) {
        if (shape == null) {
            return;
        }
        if (paintBounds == null) {
            paintBounds = shape.getBounds2D();
        }
        AffineTransform txOrig = graphics.getTransform();
        graphics.translate(paintBounds.getX(), paintBounds.getY());
        graphics.scale(paintBounds.getWidth(), paintBounds.getHeight());
        Paint paintOld = null;
        if (paint != null) {
            paintOld = graphics.getPaint();
            graphics.setPaint(paint);
        }
        AffineTransform tx = AffineTransform.getScaleInstance(1.0 / paintBounds.getWidth(),
                1.0 / paintBounds.getHeight());
        tx.translate(-paintBounds.getX(), -paintBounds.getY());
        graphics.fill(tx.createTransformedShape(shape));
        if (paintOld != null) {
            graphics.setPaint(paintOld);
        }
        graphics.setTransform(txOrig);
    }
}

Related

  1. drawLoweredBezel(Graphics g, int x, int y, int w, int h, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
  2. drawMarker(Graphics2D g, int markerWidth, int markerHeight, Point location)
  3. drawMask(Graphics2D g2, Shape mask)
  4. drawMovingRect(int x, int y, int width, int height, Graphics g, int seed)
  5. drawOptimizedLine(Graphics g, int x1, int y1, int x2, int y2)
  6. drawPowerScaleLabel(Graphics g, int base, int power, int x, int y, boolean yAxisP)
  7. drawProgressBar(Graphics2D g, final int x, final int y, final int width, final int height, final Color main, final Color progress, final int alpha, final int percentage)
  8. drawRainbow(Graphics2D g2, int x, int y, int Width, int Height, int Mode)
  9. drawRequiredMarker(Graphics2D g2, int x, int y, int iconSize)