日期:2014-05-20 浏览次数:20863 次
public static void generate(String text,Font font,File imagefile) throws IOException {
// 3*2 + 10*2 + 30*6 = 206 + 字间距 < 300 ?
BufferedImage image = new BufferedImage(300,FULL_BOARD_HEIGHT,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)image.getGraphics();
int text_width = g2d.getFontMetrics(font).stringWidth(text);
// 牌子宽度
int width = BORDER_WIDTH * 2 + BOARD_SPACE*2 + text_width;
// 透明
image = g2d.getDeviceConfiguration().createCompatibleImage(width,FULL_BOARD_HEIGHT,Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setFont(font);
RoundRectangle2D.Double outer = new RoundRectangle2D.Double(0,0,width,BOARD_HEIGHT+BORDER_WIDTH*2,BOARD_ARC_WIDTH,BOARD_ARC_WIDTH);
Color board_color = new Color(110,110,110);
g2d.setPaint(board_color);
g2d.fill(outer);
RoundRectangle2D.Double board = new RoundRectangle2D.Double(BORDER_WIDTH,BORDER_WIDTH,width-BORDER_WIDTH*2,BOARD_HEIGHT,BOARD_ARC_WIDTH,BOARD_ARC_WIDTH);
g2d.setPaint(new Color(206,6,0));
g2d.fill(board);
g2d.setPaint(Color.WHITE);
g2d.drawString(text,11,40);
Ellipse2D.Double shadow = new Ellipse2D.Double(width/2-8,88,16,7);
g2d.setPaint(Color.BLACK);
g2d.fill(shadow);
// 柱子 宽度 9