posts - 0,  comments - 0,  trackbacks - 0

一、创建个图片生成器类名为ImageCreator

/**
 * 模板方法模式应用:图片生成器
 */
public class ImageCreator {
 //图片宽度
 private int width = 200;
 
 //图片高度
 private int height = 80;
 
 //外框颜色
 private Color rectColor;
 
 //背景色
 private Color bgColor;
  
 public void generateImage(String text,ByteArrayOutputStream output)throws IOException{
  
  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  
  if(rectColor == null) rectColor = new Color(0, 0, 0);
  if(bgColor == null) bgColor = new Color(240, 251, 200);
  
  //获取画布
  Graphics2D g = (Graphics2D)image.getGraphics();
  
  //画长方形
  g.setColor(bgColor);
  g.fillRect(0, 0, width, height);
  
  //外框
  g.setColor(rectColor);
  g.drawRect(0, 0, width-1, height-1);
  
  //画字符串
  g.setColor(new Color(0, 0, 0));//字体颜色
  Font font = new Font("宋体", Font.PLAIN, 15);
  g.setFont(font);
  FontMetrics fm = g.getFontMetrics(font);
  int fontHeight = fm.getHeight(); //字符的高度
  int offsetLeft = 0;
  int rowIndex = 1;
  for(int i=0;i<text.length();i++){
   char c = text.charAt(i);
   int charWidth = fm.charWidth(c); //字符的宽度
   
   //另起一行
   if(Character.isISOControl(c) || offsetLeft >= (width-charWidth)){
    rowIndex++;
    offsetLeft = 0;
   }
   
   g.drawString(String.valueOf(c), offsetLeft, rowIndex * fontHeight);
   offsetLeft += charWidth;
  }
  
  //执行
  g.dispose();
  
  //输出图片结果
  ImageIO.write(image, "JPEG", output);
 }
}


二、创建一个ImageHashMap类

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ImageHashMap {
 
  public Map<Integer,byte[]> createMap()throws IOException
  {
  
        Map<Integer,byte[]> map=new HashMap<Integer,byte[]>();
     ByteArrayOutputStream op =new ByteArrayOutputStream();//创建该类实例时,程序内部会创建一个byte类型数组的缓冲区,
     //然后利用BtyeArrayOutputStream和ByteArrayInputStream实例向数组中写入或读出Byte型数据。
        //ByteArrayOutputStream把所有的变量收集到一起,然后一次性把数据发送出去,捕获内存缓冲区的数据 转换成字节数组。
     //ByteArrayInputStream 将字节数组转化成输入流
  
         for(int i=1;i<=5;i++)
         {
          ImageCreator a=new ImageCreator();
          a.generateImage(""+i, op);
          byte[] bt=op.toByteArray();
          map.put(i, bt);        
         }
         return map;
 }
}

三、在测试类中代码测试

/* 任务:利用迭代器显示字节流
   * 
     1,使用方法iterator()要求容器返回一个Iterator.第一次调用Iterator的next()方法时,它返回序列的第一个元素;
        2,使用next()获得序列中的下一个元素;
        3,使用hasNext()检查序列中是否还有元素;
        4,使用remove()将迭代器新近返回的元素删除.
        5,HashMap中使用enterySet()返回此映射所包含的映射关系的 Set 视图 ;也可以说该方法返回一个Map.Entry实例化后的对象集
        6,使用Map.Entry类 同一时间内取得map中的key键和相应的值。
*/        
  ImageHashMap imap=new ImageHashMap();
  Map map = new HashMap();
  Iterator iter=map.entrySet().iterator();//获取map映射来的set视图,返回迭代器
  
        while(iter.hasNext())
        {
        Map.Entry entry=(Map.Entry)iter.next();
        System.out.print(entry.getKey()+" ");
        System.out.println(entry.getValue());
         }

posted on 2011-02-13 13:22 moke44 阅读(127) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

留言簿

文章档案

搜索

  •  

最新评论