Example usage for java.awt.font FontRenderContext FontRenderContext

List of usage examples for java.awt.font FontRenderContext FontRenderContext

Introduction

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

Prototype

public FontRenderContext(AffineTransform tx, Object aaHint, Object fmHint) 

Source Link

Document

Constructs a FontRenderContext object from an optional AffineTransform and two Object values that determine if the newly constructed object has anti-aliasing or fractional metrics.

Usage

From source file:LineBreakMeasurerDemo.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    attribCharIterator = attribString.getIterator();
    FontRenderContext frc = new FontRenderContext(null, false, false);
    LineBreakMeasurer lbm = new LineBreakMeasurer(attribCharIterator, frc);
    int x = 10, y = 20;
    int w = getWidth();
    float wrappingWidth = w - 15;

    while (lbm.getPosition() < text.length()) {
        TextLayout layout = lbm.nextLayout(wrappingWidth);
        y += layout.getAscent();//from   w  w  w.j  a v a  2 s  .  c  o m
        layout.draw(g2, x, y);
        y += layout.getDescent() + layout.getLeading();
    }
}

From source file:FontRenderContextRenderingHints.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    int w = getSize().width;
    int h = getSize().height;

    RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2D.setRenderingHints(qualityHints);

    AffineTransform at = new AffineTransform();
    at.setToTranslation(-300, -400);/*  www .  j  a v  a 2 s. c o m*/
    at.shear(-0.5, 0.0);
    FontRenderContext frc = new FontRenderContext(at, false, false);
    TextLayout tl = new TextLayout("World!", font, frc);
    Shape outline = tl.getOutline(null);
    g2D.setColor(Color.blue);

    BasicStroke wideStroke = new BasicStroke(2.0f);
    g2D.setStroke(wideStroke);
    g2D.draw(outline);

}

From source file:MainClass.java

public void paint(Graphics g) {
    Dimension size = getSize();/* w w w  .  ja  v  a 2 s . c o  m*/

    String s = "To java2s.com or not to java2s.com, that is a question";

    Hashtable map = new Hashtable();
    map.put(TextAttribute.SIZE, new Float(32.0f));

    AttributedString as = new AttributedString(s, map);

    map = new Hashtable();
    map.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);

    as.addAttributes(map, 33, 52);

    AttributedCharacterIterator aci = as.getIterator();

    int startIndex = aci.getBeginIndex();
    int endIndex = aci.getEndIndex();

    LineBreakMeasurer measurer;
    measurer = new LineBreakMeasurer(aci, new FontRenderContext(null, false, false));
    measurer.setPosition(startIndex);

    float wrappingWidth = (float) size.width;

    float Y = 0.0f;

    while (measurer.getPosition() < endIndex) {
        TextLayout layout = measurer.nextLayout(wrappingWidth);

        Y += layout.getAscent();

        float X = 0.0f;

        switch (justify) {
        case LEFT:
            if (layout.isLeftToRight())
                X = 0.0f;
            else
                X = wrappingWidth - layout.getAdvance();
            break;

        case RIGHT:
            if (layout.isLeftToRight())
                X = wrappingWidth - layout.getVisibleAdvance();
            else
                X = wrappingWidth;
            break;

        case CENTER:
            if (layout.isLeftToRight())
                X = (wrappingWidth - layout.getVisibleAdvance()) / 2;
            else
                X = (wrappingWidth + layout.getAdvance()) / 2 - layout.getAdvance();
            break;

        case EQUALITY:
            layout = layout.getJustifiedLayout(wrappingWidth);
        }

        layout.draw((Graphics2D) g, X, Y);

        Y += layout.getDescent() + layout.getLeading();
    }
}

From source file:com.rapidminer.gui.graphs.ExtendedVertexShapeTransformer.java

@Override
public Shape transform(V object) {
    if (graphCreator.isLeaf(object)) {
        // leaf/*  w w w . ja  v  a 2 s  .com*/
        String text = graphCreator.getVertexName(object);
        Rectangle2D stringBounds = GraphViewer.VERTEX_BOLD_FONT.getStringBounds(text,
                new FontRenderContext(null, false, false));
        float width = (float) stringBounds.getWidth();
        float height = (float) stringBounds.getHeight();
        int minWidth = graphCreator.getMinLeafWidth();
        int minHeight = graphCreator.getMinLeafHeight();
        width = Math.max(width, minWidth);
        height = Math.max(height, minHeight);
        return new Rectangle2D.Float(-width / 2.0f - 6.0f, -height / 2.0f - 2.0f, width + 8.0f, height + 4.0f);
    } else {
        // inner nodes
        String text = graphCreator.getVertexName(object);
        Rectangle2D stringBounds = GraphViewer.VERTEX_PLAIN_FONT.getStringBounds(text,
                new FontRenderContext(null, false, false));
        float width = (float) stringBounds.getWidth();
        float height = (float) stringBounds.getHeight();
        RoundRectangle2D.Float shape = new RoundRectangle2D.Float(-width / 2.0f - 6.0f, -height / 2.0f - 4.0f,
                width + 10.0f, height + 8.0f, 10.0f, 10.0f);
        return shape;
    }
}

From source file:LineBreakSample.java

public LineBreakSample() {
    AttributedCharacterIterator paragraph = vanGogh.getIterator();
    paragraphStart = paragraph.getBeginIndex();
    paragraphEnd = paragraph.getEndIndex();

    // Create a new LineBreakMeasurer from the paragraph.
    lineMeasurer = new LineBreakMeasurer(paragraph, new FontRenderContext(null, false, false));
}

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 .j av  a2s .  co  m
    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:Utils.java

/** 
 * Renders a paragraph of text (line breaks ignored) to an image (created and returned). 
 *
 * @param font The font to use/*from   w  w  w.java  2 s .c o m*/
 * @param textColor The color of the text
 * @param text The message
 * @param width The width the text should be limited to
 * @return An image with the text rendered into it
 */
public static BufferedImage renderTextToImage(Font font, Color textColor, String text, int width) {
    Hashtable map = new Hashtable();
    map.put(TextAttribute.FONT, font);
    AttributedString attributedString = new AttributedString(text, map);
    AttributedCharacterIterator paragraph = attributedString.getIterator();

    FontRenderContext frc = new FontRenderContext(null, false, false);
    int paragraphStart = paragraph.getBeginIndex();
    int paragraphEnd = paragraph.getEndIndex();
    LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc);

    float drawPosY = 0;

    //First time around, just determine the height
    while (lineMeasurer.getPosition() < paragraphEnd) {
        TextLayout layout = lineMeasurer.nextLayout(width);

        // Move it down
        drawPosY += layout.getAscent() + layout.getDescent() + layout.getLeading();
    }

    BufferedImage image = createCompatibleImage(width, (int) drawPosY);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    drawPosY = 0;
    lineMeasurer.setPosition(paragraphStart);
    while (lineMeasurer.getPosition() < paragraphEnd) {
        TextLayout layout = lineMeasurer.nextLayout(width);

        // Move y-coordinate by the ascent of the layout.
        drawPosY += layout.getAscent();

        /* Compute pen x position.  If the paragraph is
           right-to-left, we want to align the TextLayouts
           to the right edge of the panel.
         */
        float drawPosX;
        if (layout.isLeftToRight()) {
            drawPosX = 0;
        } else {
            drawPosX = width - layout.getAdvance();
        }

        // Draw the TextLayout at (drawPosX, drawPosY).
        layout.draw(graphics, drawPosX, drawPosY);

        // Move y-coordinate in preparation for next layout.
        drawPosY += layout.getDescent() + layout.getLeading();
    }

    graphics.dispose();
    return image;
}

From source file:CheckFonts.java

/**
 * ?  ??  dialog base unit'.//w ww  . jav  a  2  s  . c  o  m
 * 
 * ? :   - ?  ????  ,   - ??  ????
 *  .
 */
protected static Dimension getDimension(ResourceDialog dialog, String text) throws Exception {
    int fontPixelsSize = (int) Math.round(dialog.pointsize * 96.0 / 72);

    Font f = getDialogFont(dialog).deriveFont(dialog.italic != 0 ? Font.ITALIC : 0, fontPixelsSize);

    // BufferedImage img = new BufferedImage(1024, 512, BufferedImage.TYPE_INT_RGB);
    // Graphics gr = img.getGraphics();
    // FontMetrics fm = gr.getFontMetrics(f);
    // r = fm.stringWidth(text);

    FontRenderContext frc = new FontRenderContext(null, false, false);
    Rectangle2D strDialogUnits = f.getStringBounds("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", frc);
    // ? ???   - http://support.microsoft.com/kb/125681
    double fullWidth = strDialogUnits.getWidth();
    double fullHeight = strDialogUnits.getHeight();
    long avgWidth = Math.round(Math.ceil(fullWidth) / 26 + 1) / 2;
    long avgHeight = Math.round(Math.ceil(fullHeight));
    double dbuX = avgWidth / 4.0;
    double dbuY = avgHeight / 8.0;

    Rectangle2D strRect = f.getStringBounds(text.replace("&", ""), frc);
    int w = (int) Math.ceil(strRect.getWidth() / dbuX);

    int h = (int) Math.ceil(strRect.getHeight() / dbuY);

    return new Dimension(w, h);
}

From source file:AttributesApp.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    attribCharIterator = attribString.getIterator();

    FontRenderContext frc = new FontRenderContext(null, false, false);
    TextLayout layout = new TextLayout(attribCharIterator, frc);

    layout.draw(g2, 20, 100);/* w  ww  . ja  v  a  2 s . com*/
}

From source file:desmoj.extensions.grafic.util.Plotter.java

/**
* Constructor to set the path of output directory and the size of created image.
* Default are onScreen = false, locale = Locale.getDefault, timeZone = TimeZone.getdefault 
* @param path//from   ww  w.j a  v  a 2s  . c o  m
* @param size
 */
public Plotter(String path, Dimension size) {
    this.paintPanel = new PaintPanel(path, size);
    this.onScreen = false;
    this.locale = Locale.getDefault();
    this.timeZone = TimeZone.getDefault();
    this.begin = null;
    this.end = null;
    this.frc = new FontRenderContext(new AffineTransform(), true, true);
}