`
xuxiaolei
  • 浏览: 148859 次
  • 性别: Icon_minigender_1
  • 来自: 彩虹之巅
社区版块
存档分类

使用JAVA生成图片

阅读更多
java 代码
  1.   
  2. import java.awt.*;   
  3. import java.awt.image.*;   
  4. import java.io.*;   
  5. import javax.imageio.*;   
  6. import java.awt.font.*;   
  7. import java.awt.geom.*;   
  8.   
  9. public class CreateImage    
  10. {   
  11.     public static void main(String[] args) throws Exception   
  12.     {   
  13.         int width = 100;   
  14.         int height = 100;   
  15.         String s = "你好";   
  16.            
  17.         File file = new File("c:/image.jpg");   
  18.            
  19.         Font font = new Font("Serif", Font.BOLD, 10);   
  20.         BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   
  21.         Graphics2D g2 = (Graphics2D)bi.getGraphics();   
  22.         g2.setBackground(Color.WHITE);   
  23.         g2.clearRect(00, width, height);   
  24.         g2.setPaint(Color.RED);   
  25.            
  26.         FontRenderContext context = g2.getFontRenderContext();   
  27.         Rectangle2D bounds = font.getStringBounds(s, context);   
  28.         double x = (width - bounds.getWidth()) / 2;   
  29.         double y = (height - bounds.getHeight()) / 2;   
  30.         double ascent = -bounds.getY();   
  31.         double baseY = y + ascent;   
  32.            
  33.         g2.drawString(s, (int)x, (int)baseY);   
  34.            
  35.         ImageIO.write(bi, "jpg", file);   
  36.     }   
  37. }   
分享到:
评论
8 楼 qq970836452 2013-09-06  
[u]
引用
引用
引用
引用
[u][i][i][b][b][/url][/img][/img][/img][/img][/url][/url][/url][/url][/img][/img][/url][/img][/url][/url][/flash][/flash][/flash][/b][/b][/i][/i][/u]
[/u][/i][/b][/u]
7 楼 jordan_micle 2012-02-14  
那个font对象好像都没有应用到图片当中啊
6 楼 madbird 2009-05-14  
感谢,你的例子让我对解决我现在的问题有了新的想法
5 楼 xuxiaolei 2009-04-16  
to zoutuo
不太明白你说的什么意思,你说的是不是类似于饼图柱图的?那可能是用jfreechart生成的。
4 楼 zoutuo 2009-04-16  
请问投票那种怎么画出来的啊?
3 楼 whq 2008-12-09  
很好,很强大
2 楼 xiaofeizm55333 2007-11-30  
正好用到,非常感谢
1 楼 lintomny 2007-10-10  
谢谢,不错的例子。简单直接!

相关推荐

Global site tag (gtag.js) - Google Analytics