Example usage for java.awt Graphics2D getFontRenderContext

List of usage examples for java.awt Graphics2D getFontRenderContext

Introduction

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

Prototype


public abstract FontRenderContext getFontRenderContext();

Source Link

Document

Get the rendering context of the Font within this Graphics2D context.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");

    final BufferedImage originalImage = ImageIO.read(url);
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();
    final BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = textImage.createGraphics();

    FontRenderContext frc = g.getFontRenderContext();
    Font font = new Font("Arial", Font.BOLD, 50);
    GlyphVector gv = font.createGlyphVector(frc, "java2s.com");

    int xOff = 0;
    int yOff = 50;

    Shape shape = gv.getOutline(xOff, yOff);
    g.setClip(shape);//ww w.j a va  2 s  .  co m
    g.drawImage(originalImage, 0, 0, null);

    g.setStroke(new BasicStroke(2f));
    g.setColor(Color.BLACK);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.draw(shape);
    g.dispose();

    ImageIO.write(textImage, "png", new File("cat-text.png"));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(textImage)));
        }
    });
}

From source file:Main.java

public static void drawCenteredShadowText(Graphics2D g2, String s, int x, int y, Color shadowColor) {
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
    int leftX = (int) (x - (float) bounds.getWidth() / 2);
    drawShadowText(g2, s, leftX, y, shadowColor, 1);
}

From source file:Utils.java

public static Shape generateShapeFromText(Font font, String string) {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = img.createGraphics();

    try {//  w ww.  j a v  a  2  s. c  o  m
        GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string);
        Shape shape = vect.getOutline(0f, (float) -vect.getVisualBounds().getY());

        return shape;
    } finally {
        g2.dispose();
    }
}

From source file:Main.java

public static BufferedImage createRotatedTextImage(String text, int angle, Font ft) {
    Graphics2D g2d = null;
    try {// w ww. j  av  a 2  s.  com
        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: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  w w  .ja  va2 s . com
    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

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 void paint(Graphics g) {
    Graphics2D g2D;
    g2D = (Graphics2D) g;/*from   w w  w .  j a va2  s  . c  om*/
    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:HorizontallyCenteredText.java

protected void paintHorizontallyCenteredText(Graphics2D g2, String s, float centerX, float baselineY) {
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
    float width = (float) bounds.getWidth();
    g2.drawString(s, centerX - width / 2, baselineY);
}

From source file:ShadowText.java

public void paint(Graphics g) {
    String text = "Hello World";
    int x = 10;/*from   ww  w  . j  a  v a2  s .  c om*/
    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:MainClass.java

private void getLayouts(Graphics g) {
    layouts = new ArrayList();

    Graphics2D g2d = (Graphics2D) g;
    FontRenderContext frc = g2d.getFontRenderContext();

    AttributedString attrStr = new AttributedString(text);
    attrStr.addAttribute(TextAttribute.FONT, font, 0, text.length());
    LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
    float wrappingWidth;

    wrappingWidth = getSize().width - 15;

    while (measurer.getPosition() < text.length()) {
        TextLayout layout = measurer.nextLayout(wrappingWidth);
        layouts.add(layout);/*from  ww  w . j av  a  2 s  .  co m*/
    }
}