一、笔记
1、验证码
1.1Graphics
1.1.1有两种矩形:普通型和圆角型。
画普通矩形有两个方法:
drawRect(int x,int y,int width,int height):画线框围起来的矩形。其中参数x和y指定左上角的位置,参数width和height是矩形的宽和高。
fillRect(int x,int y,int width,int height):是用预定的颜色填充一个矩形,得到一个着色的矩形块。
以下代码是画矩形的例子:
g.drawRect(80,100,40,25);//画线框
g.setColor(Color.yellow);g.fillRect(20,70,20,30);//画着色块
1.1.2参考:https://www.cnblogs.com/donghb/p/7637990.html
1.2、StringBuffer
1.2.1
1.3、Random类
1.3.1、nextInt方法
public int nextInt(int n);//该方法的作用是生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值,包含0而不包含n。
1.4、BufferedImage类
1.4.1、BufferedImage 子类描述具有可访问图像数据缓冲区的 Image ,继承自Image实现其方法,主要的作用就是将图片加载到内存对其进行操作;
1.4.2、BufferedImage 用法:
BufferedImage img = new BufferedImage(20, 30, BufferedImage.TYPE_INT_RGB);
try {
//通过ImageIO类读取文件
img = ImageIO.read(new File("D:/1.jpg"));
//获取图片信息
System.out.println(img.getHeight()+" "+img.getWidth()+" ");
OutputStream out = new FileOutputStream(new File("D:/2.jpg"));
ImageIO.write(img, "jpg", out); //将图片写入指定文件
} catch (Exception e) {
e.printStackTrace();
}
1.4.3、BufferedImage 和Graphics结合画图用法
int imageWidth = 100;
int imageHeight = 100;
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);//声明图片类型
Graphics graphics = image.getGraphics();//获取图片
int fontSize = 50;
Font font = new Font("楷体", Font.PLAIN, fontSize);
graphics.setFont(font);
graphics.setColor(new Color(246, 96, 0));
graphics.fillRect(0, 0, imageWidth, imageHeight);
graphics.setColor(new Color(255, 255, 255));
int strWidth = graphics.getFontMetrics().stringWidth("好");
System.out.println(strWidth);
graphics.drawString("好", fontSize - (strWidth / 2), fontSize + 30);//将文字写在图片上
try {
ImageIO.write(image, "jpg", new File("D:\\a.jpg"));//将图片输出到本地文件;也可以用输出流OutputStream输出
} catch (Exception e) {
e.printStackTrace();
}
graphics.dispose();//关闭图片
1.4.4、将文字写在图片上
Graphics.drawString(String str,int x,int y);
把X、Y理解成一个矩形的左上角的坐标;相当于从X、Y开始的一个矩形区域,这个矩形的大小就是根据字体大小和文字长度的大小;
参考:
https://www.jianshu.com/p/009914797af2