Example usage for java.awt.font GlyphVector getOutline

List of usage examples for java.awt.font GlyphVector getOutline

Introduction

In this page you can find the example usage for java.awt.font GlyphVector getOutline.

Prototype

public abstract Shape getOutline(float x, float y);

Source Link

Document

Returns a Shape whose interior corresponds to the visual representation of this GlyphVector when rendered at x, y.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");

    final BufferedImage originalImage = ImageIO.read(url);
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();
    final BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = textImage.createGraphics();

    FontRenderContext frc = g.getFontRenderContext();
    Font font = new Font("Arial", Font.BOLD, 50);
    GlyphVector gv = font.createGlyphVector(frc, "java2s.com");

    int xOff = 0;
    int yOff = 50;

    Shape shape = gv.getOutline(xOff, yOff);
    g.setClip(shape);/*www  .  j a  v a2 s  . c  o m*/
    g.drawImage(originalImage, 0, 0, null);

    g.setStroke(new BasicStroke(2f));
    g.setColor(Color.BLACK);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.draw(shape);
    g.dispose();

    ImageIO.write(textImage, "png", new File("cat-text.png"));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(textImage)));
        }
    });
}

From source file:Utils.java

public static Shape generateShapeFromText(Font font, String string) {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = img.createGraphics();

    try {//from  ww  w .  j a va  2s  .  co  m
        GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string);
        Shape shape = vect.getOutline(0f, (float) -vect.getVisualBounds().getY());

        return shape;
    } finally {
        g2.dispose();
    }
}

From source file:Bouncer.java

protected void setClip(Graphics2D g2) {
    if (mClip == false)
        return;/*from  w w w.  ja  v  a  2 s.  c  o  m*/
    if (mClipShape == null) {
        Dimension d = getSize();
        FontRenderContext frc = g2.getFontRenderContext();
        Font font = new Font("Serif", Font.PLAIN, 144);
        String s = "Java Source and Support!";
        GlyphVector gv = font.createGlyphVector(frc, s);
        Rectangle2D bounds = font.getStringBounds(s, frc);
        mClipShape = gv.getOutline((d.width - (float) bounds.getWidth()) / 2,
                (d.height + (float) bounds.getHeight()) / 2);
    }
    g2.clip(mClipShape);
}

From source file:org.jfree.experimental.swt.SWTGraphics2D.java

/**
 * Draws the specified glyph vector at the location <code>(x, y)</code>.
 * //  w w  w  . j a v  a  2s. c  o  m
 * @param g  the glyph vector (<code>null</code> not permitted).
 * @param x  the x-coordinate.
 * @param y  the y-coordinate.
 */
public void drawGlyphVector(GlyphVector g, float x, float y) {
    fill(g.getOutline(x, y));
}

From source file:DefaultGraphics2D.java

/**
 * Renders the text of the specified {@link GlyphVector} using the
 * <code>Graphics2D</code> context's rendering attributes. The rendering
 * attributes applied include the <code>Clip</code>, <code>Transform</code>,
 * <code>Paint</code>, and <code>Composite</code> attributes. The
 * <code>GlyphVector</code> specifies individual glyphs from a {@link Font}.
 * The <code>GlyphVector</code> can also contain the glyph positions. This
 * is the fastest way to render a set of characters to the screen.
 * /*  w w  w .  ja v  a 2s. co  m*/
 * @param g
 *          the <code>GlyphVector</code> to be rendered
 * @param x
 *          the x position in user space where the glyphs should be rendered
 * @param y
 *          the y position in user space where the glyphs should be rendered
 * 
 * @see java.awt.Font#createGlyphVector(FontRenderContext, char[])
 * @see java.awt.font.GlyphVector
 * @see #setPaint
 * @see java.awt.Graphics#setColor
 * @see #setTransform
 * @see #setComposite
 * @see #setClip(Shape)
 */
public void drawGlyphVector(GlyphVector g, float x, float y) {
    Shape glyphOutline = g.getOutline(x, y);
    fill(glyphOutline);
}

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

/**
 * @see Graphics2D#drawGlyphVector(GlyphVector, float, float)
 *//*  w  w  w.j  av a 2 s.  co  m*/
@Override
public void drawGlyphVector(final GlyphVector g, final float x, final float y) {
    final Shape s = g.getOutline(x, y);
    fill(s);
}