import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class StringToImage {
/**
* 字符串转成图片
*
* @param str
* @param i
* @return
*/
public Image StrToImg(String str, int i){
JLabel label = new JLabel(str);
label.setSize(26, 16);
// label.setFont(new Font("MONOSPACED", Font.BOLD,12));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setForeground(Color.decode("#eeeeee"));
label.setOpaque(true);
if (i == 1) {
label.setBackground(Color.decode("#7F0F08"));
} else if (i == -1) {
label.setBackground(Color.decode("#06662C"));
} else if (i == 0) {
label.setBackground(Color.decode("#999999"));
}
BufferedImage bfImage = getImage(label);
return bfImage;
}
private BufferedImage getImage(JComponent c){
Rectangle region = c.getBounds();
BufferedImage image = new BufferedImage(region.width, region.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.translate(-region.x, -region.y);
g2d.setColor(c.getBackground() );
g2d.fillRect(region.x, region.y, region.width, region.height);
c.paint(g2d);
g2d.dispose();
return image;
}
}