Example usage for java.awt.font TextLayout TextLayout

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

Introduction

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

Prototype

public TextLayout(String string, Map<? extends Attribute, ?> attributes, FontRenderContext frc) 

Source Link

Document

Constructs a TextLayout from a String and an attribute set.

Usage

From source file:Main.java

/**
 * Returns the bounds of the given string.
 *
 * @param text the string to measure//www . java  2  s . co m
 * @return the bounds of the given string
 */
public static Rectangle2D getDefaultStringSize(String text) {
    Font font = UIManager.getFont("Label.font");
    FontRenderContext frc = new FontRenderContext(null, true, false);
    TextLayout layout = new TextLayout(text, font, frc);

    return layout.getBounds();
}

From source file:ShadowText.java

public void paint(Graphics g) {
    String text = "Hello World";
    int x = 10;/*  w w w  .  j a  v  a  2s.c  o  m*/
    int y = 100;

    Font font = new Font("Georgia", Font.ITALIC, 50);
    Graphics2D g1 = (Graphics2D) g;

    TextLayout textLayout = new TextLayout(text, font, g1.getFontRenderContext());
    g1.setPaint(new Color(150, 150, 150));
    textLayout.draw(g1, x + 3, y + 3);

    g1.setPaint(Color.BLACK);
    textLayout.draw(g1, x, y);
}

From source file:Main.java

/**
 * Creates and returns image from the given text.
 * @param text input text/*from w  w w. j ava 2  s.c  om*/
 * @param font text font
 * @return image with input text
 */
public static BufferedImage createImageFromText(String text, Font font) {
    //You may want to change these setting, or make them parameters
    boolean isAntiAliased = true;
    boolean usesFractionalMetrics = false;
    FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D bounds = layout.getBounds();
    int w = (int) Math.ceil(bounds.getWidth());
    int h = (int) Math.ceil(bounds.getHeight()) + 2;
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); //for example;
    Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.BLACK);
    g.setFont(font);
    Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
    Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
            : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();

    return image;
}

From source file:TextLayoutOne.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, 32);

    TextLayout textLayout = new TextLayout(s, font, g2.getFontRenderContext());
    textLayout.draw(g2, 40, 80);//from ww w. ja va  2 s.co m
}

From source file:Main.java

License:asdf

public Shape getTextShape(Graphics2D g2d, String str, Font font) {
    FontRenderContext frc = g2d.getFontRenderContext();
    TextLayout tl = new TextLayout(str, font, frc);
    return tl.getOutline(null);
}

From source file:Main.java

public static BufferedImage createRotatedTextImage(String text, int angle, Font ft) {
    Graphics2D g2d = null;//  w w  w  . j  a va2s  . c om
    try {
        if (text == null || text.trim().length() == 0) {
            return null;
        }

        BufferedImage stringImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);

        g2d = (Graphics2D) stringImage.getGraphics();
        g2d.setFont(ft);

        FontMetrics fm = g2d.getFontMetrics();
        Rectangle2D bounds = fm.getStringBounds(text, g2d);

        TextLayout tl = new TextLayout(text, ft, g2d.getFontRenderContext());

        g2d.dispose();
        g2d = null;

        return createRotatedImage(tl, (int) bounds.getWidth(), (int) bounds.getHeight(), angle);
    } catch (Exception e) {
        e.printStackTrace();

        if (g2d != null) {
            g2d.dispose();
        }
    }

    return null;
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2D;/*from  w w  w . ja  v a  2 s  . c o  m*/
    g2D = (Graphics2D) g;
    FontRenderContext frc = g2D.getFontRenderContext();
    Font font1 = new Font("Courier", Font.BOLD, 24);
    String str1 = new String("Java");
    TextLayout tl = new TextLayout(str1, font1, frc);
    g2D.setColor(Color.gray);
    tl.draw(g2D, 70, 150);
}

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 {//from  w w  w .j av  a 2 s  . co  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: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, 32);

    TextLayout textLayout = new TextLayout(s, font, g2.getFontRenderContext());
    textLayout.draw(g2, 40, 80);/* w w w .ja v  a2s  . c  om*/
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2D;//  w  w w .ja v  a  2  s  .  c o  m
    g2D = (Graphics2D) g;

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

    g2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    FontRenderContext frc = g2D.getFontRenderContext();
    Font font1 = new Font("Courier", Font.BOLD, 24);
    String str1 = new String("Java");
    TextLayout tl = new TextLayout(str1, font1, frc);
    g2D.setColor(Color.gray);
    tl.draw(g2D, 50, 150);
}