Example usage for java.awt.font TextAttribute FONT

List of usage examples for java.awt.font TextAttribute FONT

Introduction

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

Prototype

TextAttribute FONT

To view the source code for java.awt.font TextAttribute FONT.

Click Source Link

Document

Attribute key used to provide the font to use to render text.

Usage

From source file:Main.java

public static int paintMultilineText(Graphics2D g2d, String text, int textX, int textWidth, int textY,
        int maxTextLineCount) {
    FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, false);
    int fa = g2d.getFontMetrics().getAscent();

    if (text.length() == 0)
        return textY;

    int currOffset = 0;
    AttributedString attributedDescription = new AttributedString(text);
    attributedDescription.addAttribute(TextAttribute.FONT, g2d.getFont());
    LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attributedDescription.getIterator(), frc);
    int lineCount = 0;
    while (true) {
        TextLayout tl = lineBreakMeasurer.nextLayout(textWidth);
        if (tl == null)
            break;

        int charCount = tl.getCharacterCount();
        String line = text.substring(currOffset, currOffset + charCount);

        g2d.drawString(line, textX, textY);

        textY += fa;/* w w w  .  j  a v a2s  . c om*/
        currOffset += charCount;

        lineCount++;
        if ((maxTextLineCount > 0) && (lineCount == maxTextLineCount))
            break;
    }

    // textY += fh;

    return textY;
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AttributedString astr = new AttributedString("aString");
    astr.addAttribute(TextAttribute.FONT, new Font("", 1, 30), 1, 2);
    astr.addAttribute(TextAttribute.BACKGROUND, Color.red, 2, 3);

    TextLayout tl = new TextLayout(astr.getIterator(), g2d.getFontRenderContext());
    tl.draw(g2d, 10, 20);/*from   www  .  j ava2s. com*/
}

From source file:BasicShapes.java

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

    int x = 10, y = 10, start = 2, end = 4;

    AttributedString astr = new AttributedString("aString");
    astr.addAttribute(TextAttribute.FONT, new Font("", 1, 1), start, end);
    astr.addAttribute(TextAttribute.BACKGROUND, Color.red, start, end);

    // Draw mixed-style text
    TextLayout tl = new TextLayout(astr.getIterator(), g2d.getFontRenderContext());
    tl.draw(g2d, x, y);/* w w  w .  j a  v a2 s  .c  o m*/

}

From source file:Main.java

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

    Font font = new Font("Serif", Font.PLAIN, 40);

    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.FONT, font);
    g2d.drawString(as1.getIterator(), 15, 60);
}

From source file:IteratorUnderStrike.java

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

    String s = "\"www.java2s.com\" is great.";

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font plainFont = new Font("Times New Roman", Font.PLAIN, 24);

    AttributedString as = new AttributedString(s);
    as.addAttribute(TextAttribute.FONT, plainFont);
    as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 1, 15);
    as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 18, 25);

    g2.drawString(as.getIterator(), 24, 70);
}

From source file:Main.java

public void paint(Graphics g) {
    String input = "this is a test.this is a test.this is a test.this is a test.";

    AttributedString attributedString = new AttributedString(input);
    attributedString.addAttribute(TextAttribute.FONT, (Font) UIManager.get("Label.font"));
    Color color = (Color) UIManager.get("Label.foreground");

    attributedString.addAttribute(TextAttribute.FOREGROUND, color);

    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    int width = getSize().width;
    int x = 10;/* w  w  w.j av a2s  .co m*/
    int y = 30;

    AttributedCharacterIterator characterIterator = attributedString.getIterator();
    FontRenderContext fontRenderContext = g2d.getFontRenderContext();
    LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, fontRenderContext);
    while (measurer.getPosition() < characterIterator.getEndIndex()) {
        TextLayout textLayout = measurer.nextLayout(width);
        y += textLayout.getAscent();
        textLayout.draw(g2d, x, y);
        y += textLayout.getDescent() + textLayout.getLeading();
    }
}

From source file:Main.java

private static TextLayout createTextLayout(JComponent c, String s, Font f, FontRenderContext frc) {
    Object shaper = (c == null ? null : c.getClientProperty(TextAttribute.NUMERIC_SHAPING));
    if (shaper == null) {
        return new TextLayout(s, f, frc);
    } else {//  ww w .ja  v a 2  s .  c  o m
        Map<TextAttribute, Object> a = new HashMap<TextAttribute, Object>();
        a.put(TextAttribute.FONT, f);
        a.put(TextAttribute.NUMERIC_SHAPING, shaper);
        return new TextLayout(s, a, frc);
    }
}

From source file:IteratorTest.java

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

    String s = "Java Source and Support";
    Dimension d = getSize();/*from w w w.jav  a2 s. co  m*/

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font serifFont = new Font("Serif", Font.PLAIN, 48);
    Font sansSerifFont = new Font("Monospaced", Font.PLAIN, 48);

    AttributedString as = new AttributedString(s);
    as.addAttribute(TextAttribute.FONT, serifFont);
    as.addAttribute(TextAttribute.FONT, sansSerifFont, 2, 5);
    as.addAttribute(TextAttribute.FOREGROUND, Color.red, 5, 6);
    as.addAttribute(TextAttribute.FOREGROUND, Color.red, 16, 17);

    g2.drawString(as.getIterator(), 40, 80);
}

From source file:TextLayoutLineBreakerMeasurer.java

public void paint(Graphics g) {
    Graphics2D graphics2D = (Graphics2D) g;
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font font = new Font("LucidaSans", Font.PLAIN, 14);
    AttributedString messageAS = new AttributedString(m);
    messageAS.addAttribute(TextAttribute.FONT, font);
    AttributedCharacterIterator messageIterator = messageAS.getIterator();
    FontRenderContext messageFRC = graphics2D.getFontRenderContext();
    LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);

    Insets insets = getInsets();//from   ww w.jav  a 2  s  .  c o  m
    float wrappingWidth = getSize().width - insets.left - insets.right;
    float x = insets.left;
    float y = insets.top;

    while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
        TextLayout textLayout = messageLBM.nextLayout(wrappingWidth);
        y += textLayout.getAscent();
        textLayout.draw(graphics2D, x, y);
        y += textLayout.getDescent() + textLayout.getLeading();
        x = insets.left;
    }
}

From source file:Main.java

public Main() {
    setSize(350, 400);/*ww  w  . jav a 2  s  . com*/
    attribString.addAttribute(TextAttribute.FOREGROUND, Color.blue, 0, text.length());
    attribString.addAttribute(TextAttribute.FONT, new Font("sanserif", Font.ITALIC, 20), 0, text.length());
}