setColor(color c) 将文字,边框或要填充的区域为指定颜色
Font f=new Font(String name , int type , int size) setFont(Font f) Font()方法的三个参数分别表示:字体名称(如:"宋体" ), 字体样式(如:Font.PLAINT,Font.ITALIC,Font.BOLD),字体大小 setFont()方法用于接收一个Font类型的参数
drawRect(int x , int y , int width , int height) drawRect()方法用于绘制矩形边框;其中:x表示矩形左边缘的位置; y表示矩形上边缘的位置;width表示矩形的宽,height表示矩形的高
drawOval(int x , int y , int width , int height) drawOval()方法用于绘制一个圆或椭圆;其大小为参数为x,y,width,height指定的矩形的外接圆或椭圆
fillRect(int x , int y , int width , int height) 用预先指定的颜色填充矩形
fillOval(int x , int y , int width , int height) 用预先指定的颜色填充圆或椭圆
drawString(String str , int x , int y) 用预先设置好的颜色和字体来绘制文本str,文本左下角的坐标为(x,y)
重写panel组件的paint()方法,并为该方法传入一个Griphics类型的对象用来充当画笔来绘制图形,绘制一个可以验证码,让其在Frame窗体中显示
package AWT组件; import java.awt.*; import java.util.Random; public class AWT绘图 { public static void main(String[] args) { Frame f=new Frame("验证码"); cdPanel cd=new cdPanel(); //为cdPanel创建实例对象 f.add(cd); f.setLocationRelativeTo(null); f.setSize(220,130); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { Window window=arg0.getWindow(); window.dispose(); } }); } } //创建cdPanel类继承Panel类,重写Panel类的paint()方法 class cdPanel extends Panel{ //重写Panel类的paint()方法 public void paint(Graphics arg0) { int width=160; int height=60; arg0.setColor(Color.black);//预先设置矩形边框的颜色 /**绘制矩形边框 * 20是矩形左边缘到左边框的距离,10是矩形上边缘到上边框的距离 * width是矩形宽度,height是矩形高度 */ arg0.drawRect(20, 10, width, height); //绘制矩形边框 arg0.setColor(Color.LIGHT_GRAY); //预先设置填充矩形的颜色 arg0.fillRect(20, 10, width+1, height+1); //填充矩形,范围+1才能将矩形全部填完 arg0.setColor(Color.red); //预先设置椭圆边框的颜色 /** * random r=new random(); * int m=r.nextInt(123); * * 注解:定义整型变量m从0~123之间获取一个随机数 */ Random r=new Random(); for(int i=1;i<=200;i++){ int x=r.nextInt(width)+20; //+20才能确保绘制的椭圆全部落在矩形区域,因为矩形左边缘到左边框的距离为20 int y=r.nextInt(height)+10; arg0.drawOval(x, y, 2, 2); } arg0.setColor(Color.black); //设置字体颜色 Font font=new Font("宋体",Font.BOLD,50); arg0.setFont(font); //设置字体样式(3个参数分别表示:字体名称,字体样式,字体大小) /** * toCharArray()的用法:将字符串对象中的字符转换为一个字符数组 * 例如: * String str="好好 学习,天天向上!"; * char ch[]=str.toCharArray(); * System.out.println("ch[1]="+ch[1]); * System.out.println("ch[2]="+ch[2]); * 输出结果: * ch[1]=好 * ch[2]= //因为第三个字符为空,所以输出为空 * * 扩展: * charAt()的用法:提取字符串中指定的字符, * 该方法返回一个字符值,该字符位于指定索引位置,字符串中第一个字符的索引为0 * 例如: * String str="1234567890"+"abcdefghijklmnopqrstuvwxyz"; * char ch=charAt(10); * System.out.println(ch); * 输入结果: * a */ // char[] chars=("1234567890"+"abcdefghijklmnopqrstuvwxyz"+"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); String str="1234567890"+"abcdefghijklmnopqrstuvwxyz"+"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[] chars=str.toCharArray(); StringBuilder sb=new StringBuilder(); for(int i=0;i<4;i++){ int yan=r.nextInt(chars.length); //从0~chars.length之间获取一个随机数作为索引 char ch=chars[yan]; //通过索引从chars[]数组中获取一个字符 /**append()方法的作用: * 用于向StringBuilder(/StringBuffer/String)对象后面追加字符/字符串 * 例如: * StringBuffer s = new StringBuffer("Hello"); * s.append("World"); * 输出结果: * HelloWorld */ sb.append(ch); //通过变量ch在循环过程中获取的字符拼接成验证码 } /** * toString()方法用于返回对象的字符串形式 */ arg0.drawString(sb.toString(),40,60); //验证码左下角的坐标为(40,60) }