Java Swing Font Metrics getImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg, Color fontColor)

Here you can find the source of getImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg, Color fontColor)

Description

get Image

License

Academic Free License

Declaration

public static BufferedImage getImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg,
            Color fontColor) 

Method Source Code


//package com.java2s;
//License from project: Academic Free License 

import java.awt.Color;

import java.awt.Font;
import java.awt.FontMetrics;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import javax.swing.JLabel;

import javax.swing.SwingUtilities;

public class Main {
    public static BufferedImage getImage(String text, boolean clockwise) {
        JLabel lbl = new JLabel();
        Font f = lbl.getFont();/* w  w w .  j  a va2 s  .  com*/
        f = f.deriveFont(Font.BOLD);
        FontMetrics fm = lbl.getFontMetrics(f);
        Color bg = new Color(1f, 1f, 1f, 0.0f);
        Color fontColor = lbl.getForeground();

        return getImage(text, clockwise, f, fm, bg, fontColor);
    }

    public static BufferedImage getImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg,
            Color fontColor) {
        BufferedImage bi = null;

        int width = SwingUtilities.computeStringWidth(fm, text);
        int height = fm.getHeight();

        bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setColor(bg);
        g2.fillRect(0, 0, width, height);

        g2.setFont(font);
        g2.setColor(fontColor);

        g2.drawString(text, 0, fm.getLeading() + fm.getAscent());

        return bi;
    }
}

Related

  1. getFontMetrics(JComponent c, Graphics g)
  2. getFontMetrics(JComponent c, Graphics g)
  3. getFontMetrics(JComponent c, Graphics g, Font f)
  4. getFontMetrics(JComponent c, Graphics g, Font font)
  5. getFRC(JComponent c, FontMetrics fm)
  6. getMethodGetFontMetrics()
  7. getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
  8. getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset)
  9. getTextBounds(FontMetrics fm, String s)