//填充字符
String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//设置字体
g.setFont(new Font("宋体",Font.BOLD,30));
//缓存随机生成的字符
StringBuffer buf = new StringBuffer();
//随机获得4个字符
Random random = new Random();
for(int i = 0 ; i < 4 ; i++){
//设置随机颜色
g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
//获得一个随机字符
int index = random.nextInt(62);
//截取字符串
String str = data.substring(index, index + 1); //[)
//需要将随机的字符,写到图片中
g.drawString(str, 20 * i, 30);
//缓存
buf.append(str);
}