Java examples for 2D Graphics:Shape
Draws a filled Shape with the specified Paint object.
//package com.java2s; 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 { /**//from w ww . ja v a 2 s. c om * 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); } }