Java Image Load getImage(String text, boolean clockwise)

Here you can find the source of getImage(String text, boolean clockwise)

Description

get Image

License

Academic Free License

Declaration

public static BufferedImage getImage(String text, boolean clockwise) 

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();//from   w w w  .  j a v a 2s .  c om
        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. getImage(String imageName, String path)
  2. getImage(String path)
  3. getImage(String path)
  4. getImage(String url)
  5. getImage(URL url)
  6. getImageFromClassResource(Class cls, String resource)
  7. getImageFromFile(String fileName)