/** * Sets the paint for this graphics context. For now, this graphics context only supports * instances of {@link Color} or {@link GradientPaint} (in the latter case there is no real * gradient support, the paint used is the {@code Color} returned by {@code getColor1()}). * * @param paint the paint ({@code null} permitted, ignored). * @see #getPaint() * @see #setColor(Color) */ @Override public void setPaint(Paint paint) { if (paint == null) { return; // to be consistent with other Graphics2D implementations } if (paint instanceof Color) { setColor((Color) paint); } else if (paint instanceof GradientPaint) { GradientPaint gp = (GradientPaint) paint; setColor(gp.getColor1()); } else { throw new RuntimeException("Can only handle 'Color' at present."); } }
/** * Transforms a <code>GradientPaint</code> instance to fit the specified * <code>target</code> shape. * * @param paint the original paint (<code>null</code> not permitted). * @param target the target shape (<code>null</code> not permitted). * * @return The transformed paint. */ public GradientPaint transform(final GradientPaint paint, final Shape target) { GradientPaint result = paint; final Rectangle2D bounds = target.getBounds2D(); if (this.type.equals(GradientPaintTransformType.VERTICAL)) { result = new GradientPaint((float) bounds.getCenterX(), (float) bounds.getMinY(), paint.getColor1(), (float) bounds.getCenterX(), (float) bounds.getMaxY(), paint.getColor2()); } else if (this.type.equals(GradientPaintTransformType.HORIZONTAL)) { result = new GradientPaint((float) bounds.getMinX(), (float) bounds.getCenterY(), paint.getColor1(), (float) bounds.getMaxX(), (float) bounds.getCenterY(), paint.getColor2()); } else if (this.type.equals( GradientPaintTransformType.CENTER_HORIZONTAL)) { result = new GradientPaint((float) bounds.getCenterX(), (float) bounds.getCenterY(), paint.getColor2(), (float) bounds.getMaxX(), (float) bounds.getCenterY(), paint.getColor1(), true); } else if (this.type.equals(GradientPaintTransformType.CENTER_VERTICAL)) { result = new GradientPaint((float) bounds.getCenterX(), (float) bounds.getMinY(), paint.getColor1(), (float) bounds.getCenterX(), (float) bounds.getCenterY(), paint.getColor2(), true); } return result; }
/** * Performs the writing of a {@link GradientPaint} object. * * @param tagName the tag name. * @param object the {@link GradientPaint} object. * @param writer the writer. * @param mPlexAttribute ??. * @param mPlexValue ??. * @throws IOException if there is an I/O error. * @throws XMLWriterException if there is a writer error. */ public void write( final String tagName, final Object object, final XMLWriter writer, final String mPlexAttribute, final String mPlexValue) throws IOException, XMLWriterException { final GradientPaint paint = (GradientPaint) object; writer.writeTag(tagName, mPlexAttribute, mPlexValue, false); writer.startBlock(); final RootXmlWriteHandler rootHandler = getRootHandler(); rootHandler.write("color1", paint.getColor1(), Color.class, writer); writer.allowLineBreak(); rootHandler.write("color2", paint.getColor2(), Color.class, writer); writer.allowLineBreak(); rootHandler.write("point1", paint.getPoint1(), Point2D.class, writer); writer.allowLineBreak(); rootHandler.write("point2", paint.getPoint2(), Point2D.class, writer); writer.endBlock(); writer.writeCloseTag(tagName); }
/** * Paints a single bar instance. * * @param g2 the graphics target. * @param renderer the renderer. * @param row the row index. * @param column the column index. * @param bar the bar * @param base indicates which side of the rectangle is the base of the bar. */ public void paintBar( Graphics2D g2, XYBarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base) { Paint itemPaint = renderer.getItemPaint(row, column); Color c0, c1; if (itemPaint instanceof Color) { c0 = (Color) itemPaint; c1 = c0.brighter(); } else if (itemPaint instanceof GradientPaint) { GradientPaint gp = (GradientPaint) itemPaint; c0 = gp.getColor1(); c1 = gp.getColor2(); } else { c0 = Color.blue; c1 = Color.blue.brighter(); } // as a special case, if the bar colour has alpha == 0, we draw // nothing. if (c0.getAlpha() == 0) { return; } if (base == RectangleEdge.TOP || base == RectangleEdge.BOTTOM) { Rectangle2D[] regions = splitVerticalBar(bar, this.g1, this.g2, this.g3); GradientPaint gp = new GradientPaint( (float) regions[0].getMinX(), 0.0f, c0, (float) regions[0].getMaxX(), 0.0f, Color.white); g2.setPaint(gp); g2.fill(regions[0]); gp = new GradientPaint( (float) regions[1].getMinX(), 0.0f, Color.white, (float) regions[1].getMaxX(), 0.0f, c0); g2.setPaint(gp); g2.fill(regions[1]); gp = new GradientPaint( (float) regions[2].getMinX(), 0.0f, c0, (float) regions[2].getMaxX(), 0.0f, c1); g2.setPaint(gp); g2.fill(regions[2]); gp = new GradientPaint( (float) regions[3].getMinX(), 0.0f, c1, (float) regions[3].getMaxX(), 0.0f, c0); g2.setPaint(gp); g2.fill(regions[3]); } else if (base == RectangleEdge.LEFT || base == RectangleEdge.RIGHT) { Rectangle2D[] regions = splitHorizontalBar(bar, this.g1, this.g2, this.g3); GradientPaint gp = new GradientPaint( 0.0f, (float) regions[0].getMinY(), c0, 0.0f, (float) regions[0].getMaxX(), Color.white); g2.setPaint(gp); g2.fill(regions[0]); gp = new GradientPaint( 0.0f, (float) regions[1].getMinY(), Color.white, 0.0f, (float) regions[1].getMaxY(), c0); g2.setPaint(gp); g2.fill(regions[1]); gp = new GradientPaint( 0.0f, (float) regions[2].getMinY(), c0, 0.0f, (float) regions[2].getMaxY(), c1); g2.setPaint(gp); g2.fill(regions[2]); gp = new GradientPaint( 0.0f, (float) regions[3].getMinY(), c1, 0.0f, (float) regions[3].getMaxY(), c0); g2.setPaint(gp); g2.fill(regions[3]); } // draw the outline... if (renderer.isDrawBarOutline() /*&& state.getBarWidth() > renderer.BAR_OUTLINE_WIDTH_THRESHOLD*/ ) { Stroke stroke = renderer.getItemOutlineStroke(row, column); Paint paint = renderer.getItemPaint(row, column); if (stroke != null && paint != null) { g2.setStroke(stroke); g2.setPaint(paint); g2.draw(bar); } } }
private void setPaint(boolean invert, double xoffset, double yoffset, boolean fill) { if (paint instanceof Color) { Color color = (Color) paint; int alpha = color.getAlpha(); if (fill) { if (alpha != currentFillGState) { currentFillGState = alpha; PdfGState gs = fillGState[alpha]; if (gs == null) { gs = new PdfGState(); gs.setFillOpacity(alpha / 255f); fillGState[alpha] = gs; } cb.setGState(gs); } cb.setColorFill(color); } else { if (alpha != currentStrokeGState) { currentStrokeGState = alpha; PdfGState gs = strokeGState[alpha]; if (gs == null) { gs = new PdfGState(); gs.setStrokeOpacity(alpha / 255f); strokeGState[alpha] = gs; } cb.setGState(gs); } cb.setColorStroke(color); } } else if (paint instanceof GradientPaint) { GradientPaint gp = (GradientPaint) paint; Point2D p1 = gp.getPoint1(); transform.transform(p1, p1); Point2D p2 = gp.getPoint2(); transform.transform(p2, p2); Color c1 = gp.getColor1(); Color c2 = gp.getColor2(); PdfShading shading = PdfShading.simpleAxial( cb.getPdfWriter(), (float) p1.getX(), normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), c1, c2); PdfShadingPattern pat = new PdfShadingPattern(shading); if (fill) cb.setShadingFill(pat); else cb.setShadingStroke(pat); } else if (paint instanceof TexturePaint) { try { TexturePaint tp = (TexturePaint) paint; BufferedImage img = tp.getImage(); Rectangle2D rect = tp.getAnchorRect(); com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null); PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight()); AffineTransform inverse = this.normalizeMatrix(); inverse.translate(rect.getX(), rect.getY()); inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight()); double[] mx = new double[6]; inverse.getMatrix(mx); pattern.setPatternMatrix( (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4], (float) mx[5]); image.setAbsolutePosition(0, 0); pattern.addImage(image); if (fill) cb.setPatternFill(pattern); else cb.setPatternStroke(pattern); } catch (Exception ex) { if (fill) cb.setColorFill(Color.gray); else cb.setColorStroke(Color.gray); } } else { try { BufferedImage img = null; int type = BufferedImage.TYPE_4BYTE_ABGR; if (paint.getTransparency() == Transparency.OPAQUE) { type = BufferedImage.TYPE_3BYTE_BGR; } img = new BufferedImage((int) width, (int) height, type); Graphics2D g = (Graphics2D) img.getGraphics(); g.transform(transform); AffineTransform inv = transform.createInverse(); Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight()); fillRect = inv.createTransformedShape(fillRect); g.setPaint(paint); g.fill(fillRect); if (invert) { AffineTransform tx = new AffineTransform(); tx.scale(1, -1); tx.translate(-xoffset, -yoffset); g.drawImage(img, tx, null); } g.dispose(); g = null; com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null); PdfPatternPainter pattern = cb.createPattern(width, height); image.setAbsolutePosition(0, 0); pattern.addImage(image); if (fill) cb.setPatternFill(pattern); else cb.setPatternStroke(pattern); } catch (Exception ex) { if (fill) cb.setColorFill(Color.gray); else cb.setColorStroke(Color.gray); } } }