Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.MultipleGradientPaint.*;
- import java.awt.*;
- import java.awt.font.*;
- import java.awt.geom.*;
- import java.awt.image.*;
- import java.io.FileOutputStream;
- import javax.imageio.ImageIO;
- /**
- * Example class which implement a custom Paint object
- * This will be part of Apache POI slide rendering for shapes with shape/path style gradient
- * Originally I thought this is the same as PathGradientBrush in MFC, but actually this
- * method is much trivial, as the mfc class cycles around and give each point a different
- * linear gradient to the center, whereas for powerpoint the shape outline and color gradually
- * changes to the center.
- */
- public class PathGradientPaintExample {
- public static void main(String[] args) throws Exception {
- int width = 500;
- int height = 500;
- Color greenTrans = new Color(0, 255, 0, 200);
- Color colors[] = { Color.blue, greenTrans, Color.magenta };
- float fractions[] = { 0f, 0.1f, 1f };
- PathGradientPaint pgpSquare = new PathGradientPaint(colors, fractions, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
- PathGradientPaint pgpRound = new PathGradientPaint(colors, fractions);
- BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- Graphics2D graphics = img.createGraphics();
- graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
- graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
- graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
- // the easy case, a convex shape
- Shape shape1 = getShape1(width, height, graphics);
- graphics.setPaint(pgpSquare);
- graphics.setRenderingHint(PathGradientPaint.GRADIENT_SHAPE, shape1);
- graphics.fill(shape1);
- // a club, which needs rounded lines, otherwise the lines overshoot
- Shape shape2 = getShape2(width, height, graphics);
- Rectangle rect2 = shape2.getBounds();
- graphics.setRenderingHint(PathGradientPaint.GRADIENT_SHAPE, shape2);
- graphics.setPaint(pgpRound);
- graphics.translate(10, (int)(height/2+rect2.getHeight()/2));
- graphics.translate(rect2.getCenterX(), rect2.getCenterY());
- graphics.rotate(Math.PI/2);
- graphics.scale(0.7, 1);
- graphics.translate(-rect2.getCenterX(), -rect2.getCenterY());
- graphics.fill(shape2);
- // a H figure, to show the diameter calculation error
- graphics.setTransform(new AffineTransform());
- Shape shape3 = getShape3(width, height, graphics);
- graphics.setPaint(pgpSquare);
- graphics.setRenderingHint(PathGradientPaint.GRADIENT_SHAPE, shape3);
- graphics.translate(330, 350);
- graphics.fill(shape3);
- // a smaller H, to check fewer gradient steps
- Shape shape4 = getShape3(width/3, height/3, graphics);
- graphics.setPaint(pgpSquare);
- graphics.setRenderingHint(PathGradientPaint.GRADIENT_SHAPE, shape4);
- graphics.translate(65, 0);
- graphics.fill(shape4);
- FileOutputStream out = new FileOutputStream("test.png");
- ImageIO.write(img, "png", out);
- out.close();
- }
- public static class PathGradientPaint implements Paint {
- private static class PathGradientPaintKey extends RenderingHints.Key {
- PathGradientPaintKey(int id) { super(id); }
- public boolean isCompatibleValue(Object val) { return true; }
- }
- /**
- * PathGradientPaint needs the shape to be set.
- * It will be achieved through setting it in the rendering hints
- */
- public static RenderingHints.Key GRADIENT_SHAPE = new PathGradientPaintKey(1);
- // http://asserttrue.blogspot.de/2010/01/how-to-iimplement-custom-paint-in-50.html
- protected final Color colors[];
- protected final float fractions[];
- protected final int capStyle;
- protected final int joinStyle;
- protected final int transparency;
- public PathGradientPaint(Color colors[], float fractions[]) {
- this(colors,fractions,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
- }
- public PathGradientPaint(Color colors[], float fractions[], int capStyle, int joinStyle) {
- this.colors = colors;
- this.fractions = fractions;
- this.capStyle = capStyle;
- this.joinStyle = joinStyle;
- // determine transparency
- boolean opaque = true;
- for (int i = 0; i < colors.length; i++){
- opaque = opaque && (colors[i].getAlpha() == 0xff);
- }
- this.transparency = opaque ? OPAQUE : TRANSLUCENT;
- }
- public PaintContext createContext(ColorModel cm,
- Rectangle deviceBounds,
- Rectangle2D userBounds,
- AffineTransform transform,
- RenderingHints hints) {
- return new PathGradientContext(cm, deviceBounds, userBounds, transform, hints);
- }
- public int getTransparency() {
- return transparency;
- }
- class PathGradientContext implements PaintContext {
- protected final Rectangle deviceBounds;
- protected final Rectangle2D userBounds;
- protected final AffineTransform xform;
- protected final RenderingHints hints;
- /**
- * for POI: the shape will be only known when the subclasses determines the concrete implementation
- * in the draw/-content method, so we need to postpone the setting/creation as long as possible
- **/
- protected final Shape shape;
- protected final PaintContext pCtx;
- protected final int gradientSteps;
- WritableRaster raster;
- public PathGradientContext(
- ColorModel cm
- , Rectangle deviceBounds
- , Rectangle2D userBounds
- , AffineTransform xform
- , RenderingHints hints
- ) {
- shape = (Shape)hints.get(GRADIENT_SHAPE);
- if (shape == null) {
- throw new IllegalPathStateException("PathGradientPaint needs a shape to be set via the rendering hint PathGradientPaint.GRADIANT_SHAPE.");
- }
- this.deviceBounds = deviceBounds;
- this.userBounds = userBounds;
- this.xform = xform;
- this.hints = hints;
- gradientSteps = getGradientSteps(shape);
- Point2D start = new Point2D.Double(0, 0);
- Point2D end = new Point2D.Double(gradientSteps, 0);
- LinearGradientPaint gradientPaint = new LinearGradientPaint(start, end, fractions, colors, CycleMethod.NO_CYCLE, ColorSpaceType.SRGB, new AffineTransform());
- Rectangle bounds = new Rectangle(0, 0, gradientSteps, 1);
- pCtx = gradientPaint.createContext(cm, bounds, bounds, new AffineTransform(), hints);
- }
- public void dispose() {}
- public ColorModel getColorModel() {
- return pCtx.getColorModel();
- }
- public Raster getRaster(int xOffset, int yOffset, int w, int h) {
- ColorModel cm = getColorModel();
- if (raster == null) createRaster();
- // TODO: eventually use caching here
- WritableRaster childRaster = cm.createCompatibleWritableRaster(w, h);
- Rectangle2D childRect = new Rectangle2D.Double(xOffset, yOffset, w, h);
- if (!childRect.intersects(deviceBounds)) {
- // usually doesn't happen ...
- return childRaster;
- }
- Rectangle2D destRect = new Rectangle2D.Double();
- Rectangle2D.intersect(childRect, deviceBounds, destRect);
- int dx = (int)(destRect.getX()-deviceBounds.getX());
- int dy = (int)(destRect.getY()-deviceBounds.getY());
- int dw = (int)destRect.getWidth();
- int dh = (int)destRect.getHeight();
- Object data = raster.getDataElements(dx, dy, dw, dh, null);
- dx = (int)(destRect.getX()-childRect.getX());
- dy = (int)(destRect.getY()-childRect.getY());
- childRaster.setDataElements(dx, dy, dw, dh, data);
- return childRaster;
- }
- protected int getGradientSteps(Shape shape) {
- Rectangle rect = shape.getBounds();
- int lower = 1;
- int upper = (int)(Math.max(rect.getWidth(),rect.getHeight())/2.0);
- while (lower < upper-1) {
- int mid = lower + (upper - lower) / 2;
- BasicStroke bs = new BasicStroke(mid, capStyle, joinStyle);
- Area area = new Area(bs.createStrokedShape(shape));
- if (area.isSingular()) {
- upper = mid;
- } else {
- lower = mid;
- }
- }
- return upper;
- }
- protected void createRaster() {
- ColorModel cm = getColorModel();
- raster = cm.createCompatibleWritableRaster((int)deviceBounds.getWidth(), (int)deviceBounds.getHeight());
- BufferedImage img = new BufferedImage(cm, raster, false, null);
- Graphics2D graphics = img.createGraphics();
- graphics.setRenderingHints(hints);
- graphics.translate(-deviceBounds.getX(), -deviceBounds.getY());
- graphics.transform(xform);
- Raster img2 = pCtx.getRaster(0, 0, gradientSteps, 1);
- int rgb[] = new int[cm.getNumComponents()];
- for (int i = gradientSteps-1; i>=0; i--) {
- img2.getPixel(i, 0, rgb);
- Color c = new Color(rgb[0],rgb[1],rgb[2]);
- if (rgb.length == 4) {
- // it doesn't work to use just a color with transparency ...
- graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, rgb[3]/255.0f));
- }
- graphics.setStroke(new BasicStroke(i+1, capStyle, joinStyle));
- graphics.setColor(c);
- graphics.draw(shape);
- }
- graphics.dispose();
- }
- }
- }
- static Shape getShape1(int width, int height, Graphics2D graphics) {
- int x1Points[] = {0, width, 0, width};
- int y1Points[] = {0, height, height, 0};
- GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, x1Points.length);
- polygon.moveTo(x1Points[0], y1Points[0]);
- for (int index = 1; index < x1Points.length; index++) {
- polygon.lineTo(x1Points[index], y1Points[index]);
- };
- polygon.closePath();
- return polygon;
- }
- static Shape getShape2(int width, int height, Graphics2D graphics) {
- Font f = new Font("Lucida Sans", Font.BOLD, height/2);
- FontRenderContext fontRenderContext = graphics.getFontRenderContext();
- GlyphVector glyphVector = f.createGlyphVector(fontRenderContext, "\u2663"); // club
- return glyphVector.getOutline();
- }
- static Shape getShape3(int width, int height, Graphics2D graphics) {
- Font f = new Font("Lucida Sans", Font.BOLD, height/2);
- FontRenderContext fontRenderContext = graphics.getFontRenderContext();
- GlyphVector glyphVector = f.createGlyphVector(fontRenderContext, "H");
- return glyphVector.getOutline();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement