// 定义一个随机数
Random r = new Random();
// 定义变量,提供给图片上写数据的x轴的坐标
int x = 15;
int y = 30;
for (int i = 1; i <= 4; i++) {
// 修改画笔的颜色
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
// 设置字体的样式
g.setFont(new Font("宋体", Font.ITALIC, 30));
int index = data.length();
char ch = data.charAt(r.nextInt(index));
g.drawString(ch + "", x, y);
x += 20;
}
// 设置干扰线
for (int i = 1; i <= 6; i++) {
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width),r.nextInt(height));
}
// 需要把这个图片保存到指定的文件中(通过网络发给浏览器)
ImageIO.write(bufi, "JPG", new FileOutputStream("e:/1.jpg"));
}
}