Example usage for java.awt.font GlyphVector getGlyphOutline

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

Introduction

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

Prototype

public abstract Shape getGlyphOutline(int glyphIndex);

Source Link

Document

Returns a Shape whose interior corresponds to the visual representation of the specified glyph within this GlyphVector .

Usage

From source file:Paints.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    // Paint the entire background using a GradientPaint.
    // The background color varies diagonally from deep red to pale blue
    g.setPaint(new GradientPaint(0, 0, new Color(150, 0, 0), WIDTH, HEIGHT, new Color(200, 200, 255)));
    g.fillRect(0, 0, WIDTH, HEIGHT); // fill the background

    // Use a different GradientPaint to draw a box.
    // This one alternates between deep opaque green and transparent green.
    // Note: the 4th arg to Color() constructor specifies color opacity
    g.setPaint(new GradientPaint(0, 0, new Color(0, 150, 0), 20, 20, new Color(0, 150, 0, 0), true));
    g.setStroke(new BasicStroke(15)); // use wide lines
    g.drawRect(25, 25, WIDTH - 50, HEIGHT - 50); // draw the box

    // The glyphs of fonts can be used as Shape objects, which enables
    // us to use Java2D techniques with letters Just as we would with
    // any other shape. Here we get some letter shapes to draw.
    Font font = new Font("Serif", Font.BOLD, 10); // a basic font
    Font bigfont = // a scaled up version
            font.deriveFont(AffineTransform.getScaleInstance(30.0, 30.0));
    GlyphVector gv = bigfont.createGlyphVector(g.getFontRenderContext(), "JAV");
    Shape jshape = gv.getGlyphOutline(0); // Shape of letter J
    Shape ashape = gv.getGlyphOutline(1); // Shape of letter A
    Shape vshape = gv.getGlyphOutline(2); // Shape of letter V

    // We're going to outline the letters with a 5-pixel wide line
    g.setStroke(new BasicStroke(5.0f));

    // We're going to fake shadows for the letters using the
    // following Paint and AffineTransform objects
    Paint shadowPaint = new Color(0, 0, 0, 100); // Translucent black
    AffineTransform shadowTransform = AffineTransform.getShearInstance(-1.0, 0.0); // Shear to the right
    shadowTransform.scale(1.0, 0.5); // Scale height by 1/2

    // Move to the baseline of our first letter
    g.translate(65, 270);//  w w w . j a va  2 s.c  om

    // Draw the shadow of the J shape
    g.setPaint(shadowPaint);
    g.translate(15, 20); // Compensate for the descender of the J
    // transform the J into the shape of its shadow, and fill it
    g.fill(shadowTransform.createTransformedShape(jshape));
    g.translate(-15, -20); // Undo the translation above

    // Now fill the J shape with a solid (and opaque) color
    g.setPaint(Color.blue); // Fill with solid, opaque blue
    g.fill(jshape); // Fill the shape
    g.setPaint(Color.black); // Switch to solid black
    g.draw(jshape); // And draw the outline of the J

    // Now draw the A shadow
    g.translate(75, 0); // Move to the right
    g.setPaint(shadowPaint); // Set shadow color
    g.fill(shadowTransform.createTransformedShape(ashape)); // draw shadow

    // Draw the A shape using a solid transparent color
    g.setPaint(new Color(0, 255, 0, 125)); // Transparent green as paint
    g.fill(ashape); // Fill the shape
    g.setPaint(Color.black); // Switch to solid back
    g.draw(ashape); // Draw the outline

    // Move to the right and draw the shadow of the letter V
    g.translate(175, 0);
    g.setPaint(shadowPaint);
    g.fill(shadowTransform.createTransformedShape(vshape));

    // We're going to fill the next letter using a TexturePaint, which
    // repeatedly tiles an image. The first step is to obtain the image.
    // We could load it from an image file, but here we create it
    // ourselves by drawing a into an off-screen image. Note that we use
    // a GradientPaint to fill the off-screen image, so the fill pattern
    // combines features of both Paint classes.
    BufferedImage tile = // Create an image
            new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D tg = tile.createGraphics(); // Get its Graphics for drawing
    tg.setColor(Color.pink);
    tg.fillRect(0, 0, 50, 50); // Fill tile background with pink
    tg.setPaint(new GradientPaint(40, 0, Color.green, // diagonal gradient
            0, 40, Color.gray)); // green to gray
    tg.fillOval(5, 5, 40, 40); // Draw a circle with this gradient

    // Use this new tile to create a TexturePaint and fill the letter V
    g.setPaint(new TexturePaint(tile, new Rectangle(0, 0, 50, 50)));
    g.fill(vshape); // Fill letter shape
    g.setPaint(Color.black); // Switch to solid black
    g.draw(vshape); // Draw outline of letter

    // Move to the right and draw the shadow of the final A
    g.translate(160, 0);
    g.setPaint(shadowPaint);
    g.fill(shadowTransform.createTransformedShape(ashape));

    g.fill(ashape); // Fill letter A
    g.setPaint(Color.black); // Revert to solid black
    g.draw(ashape); // Draw the outline of the A
}

From source file:RotatedText.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "111111111111111111111111111111";

    Font font = new Font("Courier", Font.PLAIN, 12);
    g2d.translate(20, 20);/*  w w  w . j  av a2 s . co m*/

    FontRenderContext frc = g2d.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate((double) i / (double) (length - 1) * Math.PI / 3);

        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2d.fill(transformedGlyph);
    }
}

From source file:RollingText.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "Java Source and Support.";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);/*from   w  w w.  j  av a 2 s  . c o  m*/

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);//  w ww  .ja va2s. c o m

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);//from   w  w  w.  ja  va 2s .  c  o  m

    GlyphVector gv = font.createGlyphVector(frc, s);
    System.out.println(gv.getFont());

    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);/*w w w .  j a va  2  s  . co m*/

    GlyphVector gv = font.createGlyphVector(frc, s);
    System.out.println(gv.getOutline());

    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);// www . j  av a2 s .  co m

    GlyphVector gv = font.createGlyphVector(frc, s);
    System.out.println(gv.getVisualBounds());

    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "www.java2s.com www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);//from   w w w . j a  va2 s  .  c  o m

    GlyphVector gv = font.createGlyphVector(frc, s);
    System.out.println(gv.getLogicalBounds());

    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

From source file:com.jhlabs.awt.TextStroke.java

public Shape createStrokedShape(Shape shape) {
    FontRenderContext frc = new FontRenderContext(null, true, true);
    GlyphVector glyphVector = font.createGlyphVector(frc, text);

    GeneralPath result = new GeneralPath();
    PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS);
    float points[] = new float[6];
    float moveX = 0, moveY = 0;
    float lastX = 0, lastY = 0;
    float thisX = 0, thisY = 0;
    int type = 0;
    float next = 0;
    int currentChar = 0;
    int length = glyphVector.getNumGlyphs();

    if (length == 0)
        return result;

    float factor = stretchToFit ? measurePathLength(shape) / (float) glyphVector.getLogicalBounds().getWidth()
            : 1.0f;/*  w w  w .ja  v a2  s.  c  om*/
    float height = (float) glyphVector.getLogicalBounds().getHeight();
    float nextAdvance = 0;

    while (currentChar < length && !it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            moveX = lastX = points[0];
            moveY = lastY = points[1];
            result.moveTo(moveX, moveY);
            nextAdvance = glyphVector.getGlyphMetrics(currentChar).getAdvance() * 0.5f;
            next = nextAdvance;
            break;

        case PathIterator.SEG_CLOSE:
            points[0] = moveX;
            points[1] = moveY;
            // Fall into....

        case PathIterator.SEG_LINETO:
            thisX = points[0];
            thisY = points[1];
            float dx = thisX - lastX;
            float dy = thisY - lastY;
            float distance = (float) FastMath.sqrt(dx * dx + dy * dy);
            if (distance >= next) {
                float r = 1.0f / distance;
                float angle = (float) FastMath.atan2(dy, dx);
                while (currentChar < length && distance >= next) {
                    Shape glyph = glyphVector.getGlyphOutline(currentChar);
                    Point2D p = glyphVector.getGlyphPosition(currentChar);
                    float px = (float) p.getX();
                    float py = (float) p.getY();
                    float x = lastX + next * dx * r;
                    float y = lastY + next * dy * r;
                    float advance = nextAdvance;
                    nextAdvance = currentChar < length - 1
                            ? glyphVector.getGlyphMetrics(currentChar + 1).getAdvance() * 0.5f
                            : 0;
                    t.setToTranslation(x, y);
                    t.rotate(angle);
                    t.translate(-px - advance, -py + height * factor / 2.0f);
                    result.append(t.createTransformedShape(glyph), false);
                    next += (advance + nextAdvance) * factor;
                    currentChar++;
                    if (repeat)
                        currentChar %= length;
                }
            }
            next -= distance;
            lastX = thisX;
            lastY = thisY;
            break;
        }
        it.next();
    }

    return result;
}

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

public void drawGlyphVector(GlyphVector glyph, float x, float y) {
    translate(x, y);//from   www  .  j a  v  a2  s.  com
    Element currentElement = dom.createElement("g");//$NON-NLS-1$
    Element transElement = createElement("g"); //$NON-NLS-1$
    currentElement.appendChild(transElement);
    // need to defer clipping for each glyph
    setFillColor(transElement, true);
    for (int idx = 0; idx < glyph.getNumGlyphs(); idx++) {
        Element glyphElem = createShape(glyph.getGlyphOutline(idx));

        transElement.appendChild(glyphElem);
    }
    // should add clipping to the group element that is not transformed
    if (clip != null)
        currentElement.setAttribute("clip-path", "url(#clip" + clip.hashCode() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    appendChild(currentElement);
    translate(-x, -y);
}