Example usage for java.awt TexturePaint getImage

List of usage examples for java.awt TexturePaint getImage

Introduction

In this page you can find the example usage for java.awt TexturePaint getImage.

Prototype

public BufferedImage getImage() 

Source Link

Document

Returns the BufferedImage texture used to fill the shapes.

Usage

From source file:Main.java

public void paint(Graphics g) {
    BufferedImage bim;//ww w . j ava 2s.  c  o m

    TexturePaint tp;

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
    System.out.println(tp.getImage());

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}

From source file:org.eclipse.birt.chart.device.svg.SVGGraphics2D.java

/**
 * @param paint/*from  w  w w  . j  ava2  s .c o  m*/
 *            The paint to set.
 */
@Override
public void setPaint(Paint paint) {
    if (paint instanceof GradientPaint) {
        SVGGradientPaint gp = new SVGGradientPaint((GradientPaint) paint);
        int index = paints.indexOf(gp);
        if (index == -1) {
            paints.add(gp);
            definitions.appendChild(createGradientPaint(gp, false));
            definitions.appendChild(createGradientPaint(gp, true));
        } else {
            gp = paints.get(index);
        }
        this.paint = gp;
    } else if (paint instanceof TexturePaint) {
        TexturePaint tp = (TexturePaint) paint;
        if (!textures.contains(tp)) {
            textures.add(tp);
            Element elemImg = createEmbeddeImage(tp.getImage());
            if (elemImg != null) {
                definitions.appendChild(elemImg);
                String imgId = elemImg.getAttribute("id"); //$NON-NLS-1$
                definitions.appendChild(createTexturePaint(tp, imgId, false));
                definitions.appendChild(createTexturePaint(tp, imgId, true));
            }
        }
        this.paint = tp;
    } else
        this.paint = paint;
}

From source file:org.eclipse.birt.chart.device.svg.SVGGraphics2D.java

protected Element createTexturePaint(TexturePaint paint, String imgId, boolean highlight) {
    Element elem = dom.createElement("pattern"); //$NON-NLS-1$
    if (highlight)
        elem.setAttribute("id", getTextureId(paint) + "h"); //$NON-NLS-1$ //$NON-NLS-2$
    else//from www .  j a  va 2 s  .co  m
        elem.setAttribute("id", getTextureId(paint)); //$NON-NLS-1$

    BufferedImage img = paint.getImage();
    int width = img.getWidth();
    int height = img.getHeight();
    elem.setAttribute("patternUnits", "userSpaceOnUse"); //$NON-NLS-1$//$NON-NLS-2$
    elem.setAttribute("width", Integer.toString(width)); //$NON-NLS-1$
    elem.setAttribute("height", Integer.toString(height)); //$NON-NLS-1$

    Element elemUse = dom.createElement("use"); //$NON-NLS-1$
    elemUse.setAttribute("x", "0"); //$NON-NLS-1$//$NON-NLS-2$
    elemUse.setAttribute("y", "0"); //$NON-NLS-1$//$NON-NLS-2$
    elemUse.setAttribute("xlink:href", "#" + imgId); //$NON-NLS-1$//$NON-NLS-2$

    elem.appendChild(elemUse);
    return elem;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

private void setPaint(final boolean invert, final double xoffset, final double yoffset, final boolean fill) {
    if (paint instanceof Color) {
        final Color color = (Color) paint;
        final int alpha = color.getAlpha();
        if (fill) {
            if (alpha != currentFillGState) {
                currentFillGState = alpha;
                PdfGState gs = fillGState[alpha];
                if (gs == null) {
                    gs = new PdfGState();
                    gs.setFillOpacity(alpha / 255.00f);
                    fillGState[alpha] = gs;
                }/*from   www  .j a  v a  2 s .co  m*/
                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 / 255.0f);
                    strokeGState[alpha] = gs;
                }
                cb.setGState(gs);
            }
            cb.setColorStroke(color);
        }
    } else if (paint instanceof GradientPaint) {
        final GradientPaint gp = (GradientPaint) paint;
        final Point2D p1 = gp.getPoint1();
        transform.transform(p1, p1);
        final Point2D p2 = gp.getPoint2();
        transform.transform(p2, p2);
        final Color c1 = gp.getColor1();
        final Color c2 = gp.getColor2();
        final PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), (float) p1.getX(),
                normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), c1, c2);
        final PdfShadingPattern pat = new PdfShadingPattern(shading);
        if (fill) {
            cb.setShadingFill(pat);
        } else {
            cb.setShadingStroke(pat);
        }
    } else if (paint instanceof TexturePaint) {
        try {
            final TexturePaint tp = (TexturePaint) paint;
            final BufferedImage img = tp.getImage();
            final Rectangle2D rect = tp.getAnchorRect();
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight());
            final AffineTransform inverse = this.normalizeMatrix();
            inverse.translate(rect.getX(), rect.getY());
            inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight());
            final 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 {
            int type = BufferedImage.TYPE_4BYTE_ABGR;
            if (paint.getTransparency() == Transparency.OPAQUE) {
                type = BufferedImage.TYPE_3BYTE_BGR;
            }
            final BufferedImage img = new BufferedImage((int) width, (int) height, type);
            final Graphics2D g = (Graphics2D) img.getGraphics();
            g.transform(transform);
            final 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) {
                final AffineTransform tx = new AffineTransform();
                tx.scale(1, -1);
                tx.translate(-xoffset, -yoffset);
                g.drawImage(img, tx, null);
            }
            g.dispose();
            // g = null;
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final 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);
            }
        }
    }
}