import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class awtHuitu {
public static void main(String[] args) {
Frame f=new Frame("验证码");
Panel p=new Mypanel();
f.add(p);
f.setSize(400,300);
f.setLocationRelativeTo(null); //参数为null,将窗口将置于屏幕的中央
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class Mypanel extends Panel{
public void paint(Graphics g){ //一个类,相当于一个抽象的画笔
int width=160; //设置验证码边框大小
int height=40;
g.setColor(Color.LIGHT_GRAY); //设置全局颜色
g.fillRect(0, 0, width, height);//填充
g.setColor(Color.BLACK);
g.drawRect(0, 0, width-1, height-1);
Random r=new Random();
for(int i=0;i<100;i++){
int x=r.nextInt(width)-2; //产生0-width之间的随机数
int y=r.nextInt(height)-2;
g.drawOval(x,y, 2, 2); //画点阵
}
g.setFont(new Font("黑体",Font.BOLD,30)); //指定字体名称、样式和磅值大小
g.setColor(Color.BLUE);
char [] chars=("0123456789abcdefghijklmnopqrstuvwxyz"
+"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
StringBuilder sb=new StringBuilder();
for(int i=0;i<4;i++){
int pos=r.nextInt(chars.length);
char c=chars[pos];
sb.append(c+" ");
}
g.drawString(sb.toString(), 20, 30);
}
}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class awtHuitu {
public static void main(String[] args) {
Frame f=new Frame("验证码");
Panel p=new Mypanel();
f.add(p);
f.setSize(400,300);
f.setLocationRelativeTo(null); //参数为null,将窗口将置于屏幕的中央
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class Mypanel extends Panel{
public void paint(Graphics g){ //一个类,相当于一个抽象的画笔
int width=160; //设置验证码边框大小
int height=40;
g.setColor(Color.LIGHT_GRAY); //设置全局颜色
g.fillRect(0, 0, width, height);//填充
g.setColor(Color.BLACK);
g.drawRect(0, 0, width-1, height-1);
Random r=new Random();
for(int i=0;i<100;i++){
int x=r.nextInt(width)-2; //产生0-width之间的随机数
int y=r.nextInt(height)-2;
g.drawOval(x,y, 2, 2); //画点阵
}
g.setFont(new Font("黑体",Font.BOLD,30)); //指定字体名称、样式和磅值大小
g.setColor(Color.BLUE);
char [] chars=("0123456789abcdefghijklmnopqrstuvwxyz"
+"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
StringBuilder sb=new StringBuilder();
for(int i=0;i<4;i++){
int pos=r.nextInt(chars.length);
char c=chars[pos];
sb.append(c+" ");
}
g.drawString(sb.toString(), 20, 30);
}
}