Example usage for java.awt Graphics2D drawGlyphVector

List of usage examples for java.awt Graphics2D drawGlyphVector

Introduction

In this page you can find the example usage for java.awt Graphics2D drawGlyphVector.

Prototype

public abstract void drawGlyphVector(GlyphVector g, float x, float y);

Source Link

Document

Renders the text of the specified GlyphVector using the Graphics2D context's rendering attributes.

Usage

From source file:Hexagon.java

public static BufferedImage drawCodePoint(char codePoint, int width, int height, Font font, Color color) {
    BufferedImage img = createImage(width, height);
    Graphics2D g2 = img.createGraphics();
    String text = "" + codePoint;
    g2.setColor(color);//w ww .  j av  a  2s  .c  o  m
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    GlyphVector gv = font.createGlyphVector(g2.getFontRenderContext(), text);
    g2.drawGlyphVector(gv, 0f, (float) gv.getGlyphMetrics(0).getBounds2D().getHeight());
    return img;
}

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";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    g2.drawGlyphVector(gv, 40, 60);
}

From source file:MainClass.java

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

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

    String s = "www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    g2.drawGlyphVector(gv, 40, 60);
}

From source file:SimpleFont.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();

    GlyphVector gv = font.createGlyphVector(frc, s);
    g2.drawGlyphVector(gv, 40, 60);
}

From source file:org.apache.fop.render.java2d.Java2DPainter.java

/** {@inheritDoc} */
public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[] dx, String text)
        throws IFException {
    g2dState.updateColor(state.getTextColor());
    FontTriplet triplet = new FontTriplet(state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
    //TODO Ignored: state.getFontVariant()
    //TODO Opportunity for font caching if font state is more heavily used
    Font font = getFontInfo().getFontInstance(triplet, state.getFontSize());
    //String fontName = font.getFontName();
    //float fontSize = state.getFontSize() / 1000f;
    g2dState.updateFont(font.getFontName(), state.getFontSize() * 1000);

    Graphics2D g2d = this.g2dState.getGraph();
    GlyphVector gv = g2d.getFont().createGlyphVector(g2d.getFontRenderContext(), text);
    Point2D cursor = new Point2D.Float(0, 0);

    int l = text.length();
    int dxl = (dx != null ? dx.length : 0);

    if (dx != null && dxl > 0 && dx[0] != 0) {
        cursor.setLocation(cursor.getX() - (dx[0] / 10f), cursor.getY());
        gv.setGlyphPosition(0, cursor);//w  w w.  j  a  v a 2 s  .  c  o m
    }
    for (int i = 0; i < l; i++) {
        char orgChar = text.charAt(i);
        float glyphAdjust = 0;
        int cw = font.getCharWidth(orgChar);

        if ((wordSpacing != 0) && CharUtilities.isAdjustableSpace(orgChar)) {
            glyphAdjust += wordSpacing;
        }
        glyphAdjust += letterSpacing;
        if (dx != null && i < dxl - 1) {
            glyphAdjust += dx[i + 1];
        }

        cursor.setLocation(cursor.getX() + cw + glyphAdjust, cursor.getY());
        gv.setGlyphPosition(i + 1, cursor);
    }
    g2d.drawGlyphVector(gv, x, y);
}

From source file:org.apache.pdfbox.pdmodel.font.PDSimpleFont.java

/**
 * This will draw a string on a canvas using the font.
 *
 * @param g2d The graphics to draw onto.
 * @param at The transformation matrix with all information for scaling and shearing of the font.
 * @param x The x coordinate to draw at.
 * @param y The y coordinate to draw at.
 * @param glyphs The GlyphVector containing the glyphs to be drawn.
 *
 *///from  w w w.jav a  2s. c om
protected void writeFont(final Graphics2D g2d, final AffineTransform at, final float x, final float y,
        final GlyphVector glyphs) {
    // check if we have a rotation
    if (!at.isIdentity()) {
        try {
            AffineTransform atInv = at.createInverse();
            // do only apply the size of the transform, rotation will be realized by rotating the graphics,
            // otherwise the hp printers will not render the font
            // apply the transformation to the graphics, which should be the same as applying the
            // transformation itself to the text
            g2d.transform(at);
            // translate the coordinates
            Point2D.Float newXy = new Point2D.Float(x, y);
            atInv.transform(new Point2D.Float(x, y), newXy);
            g2d.drawGlyphVector(glyphs, (float) newXy.getX(), (float) newXy.getY());
            // restore the original transformation
            g2d.transform(atInv);
        } catch (NoninvertibleTransformException e) {
            LOG.error("Error in " + getClass().getName() + ".writeFont", e);
        }
    } else {
        g2d.drawGlyphVector(glyphs, x, y);
    }
}