用Java编写随机验证码生成器

用Java编写随机验证码生成器

很多GUI程序都需要在组件上绘制图形,在java.awt包中提供了一个专门的Graphics类,它相当于画布,提供各种绘制方法。
随机验证码生成器用了其中的几种方法
①setColor( )用于指定上下文颜色,方法中接收一个Color类型的参数,如Color.WHITE等。

声明

public abstract void setColor(Color c);
/**
  * Sets the paint mode of this graphics context to overwrite the
  * destination with this graphics context's current color.
  * This sets the logical pixel operation function to the paint or
  * overwrite mode.  All subsequent rendering operations will
  * overwrite the destination with the current color.
  */

②setFont( )方法用于指定上下文字体,方法中接收一个Font类型的参数,Font类第一个参数是String类型,表示字体名称,如“黑体”;第二个参数表示int类型,表示字体样式,如Font.PLAINT;第三个参数也是int类型,表示字体大小。

声明

public abstract void setPaintMode();
/**
  * Sets the paint mode of this graphics context to alternate between
  * this graphics context's current color and the new specified color.
  * This specifies that logical pixel operations are performed in the
  * XOR mode, which alternates pixels between the current color and
  * a specified XOR color.
  * 

* When drawing operations are performed, pixels which are the * current color are changed to the specified color, and vice versa. *

* Pixels that are of colors other than those two colors are changed * in an unpredictable but reversible manner; if the same figure is * drawn twice, then all pixels are restored to their original values. * @param c1 the XOR alternation color */

③drawRect( )方法和drawOval( )方法用于绘制矩形和椭圆形的边框,fillRect( )和fillOval( )方法用于使用当前颜色填充绘制完成的矩形和椭圆形。

声明

public abstract void fillRect(int x, int y, int width, int height);
/**
  * Draws the outline of the specified rectangle.
  * The left and right edges of the rectangle are at
  * x and x + width.
  * The top and bottom edges are at
  * y and y + height.
  * The rectangle is drawn using the graphics context's current color.
  * @param         x   the x coordinate
  *                         of the rectangle to be drawn.
  * @param         y   the y coordinate
  *                         of the rectangle to be drawn.
  * @param         width   the width of the rectangle to be drawn.
  * @param         height   the height of the rectangle to be drawn.
  * @see          java.awt.Graphics#fillRect
  * @see          java.awt.Graphics#clearRect
  */

public void drawRect(int x, int y, int width, int height) {
        if ((width < 0) || (height < 0)) {
            return;
        }

	if (height == 0 || width == 0) {
            drawLine(x, y, x + width, y + height);
        } else {
            drawLine(x, y, x + width - 1, y);
            drawLine(x + width, y, x + width, y + height - 1);
            drawLine(x + width, y + height, x + 1, y + height);
            drawLine(x, y + height, x, y + 1);
        }
 }

public abstract void drawOval(int x, int y, int width, int height);
/**
  * Fills an oval bounded by the specified rectangle with the
  * current color.
  * @param       x the x coordinate of the upper left corner
  *                     of the oval to be filled.
  * @param       y the y coordinate of the upper left corner
  *                     of the oval to be filled.
  * @param       width the width of the oval to be filled.
  * @param       height the height of the oval to be filled.
  * @see         java.awt.Graphics#drawOval
  */

④drawString( )方法用于绘制一段文本,第一个参数为绘制文本的内容,第二个,第三个参数表示绘制的左下角坐标x,y;

声明

public abstract void drawString(String str, int x, int y);
/**
  * Renders the text of the specified iterator applying its attributes
  * in accordance with the specification of the
  * {@link java.awt.font.TextAttribute TextAttribute} class.
  * 

* The baseline of the leftmost character is at position * (xy) in this graphics context's coordinate system. * @param iterator the iterator whose text is to be drawn * @param x the x coordinate. * @param y the y coordinate. * @throws NullPointerException if iterator is * null. * @see java.awt.Graphics#drawBytes * @see java.awt.Graphics#drawChars */

最后贴上完整代码

package GUI;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class AWT绘图 {
	public static void main(String[] args){
		final Frame frame = new Frame("验证码");
		final Panel panel = new Mypanel();
		frame.add(panel);
		frame.setSize(200,100);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		MypanelListener ml = new MypanelListener();
		frame.addWindowListener(ml);
	}
}
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;
  			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 = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFG"+"HIJKLMNPQRSTUVWXYZ").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);
 	}
}
class MypanelListener implements WindowListener{
  	public void windowClosing(WindowEvent e){
		Window window = e.getWindow();
 		window.setVisible(false);
   		window.dispose();  
  	}
  	public void windowActivated(WindowEvent e){
   
  	}
	public void windowClosed(WindowEvent e){
   
  	}
	public void windowDeactivated(WindowEvent e){
   
	}
	public void windowDeiconified(WindowEvent e){
   
	}
  	public void windowIconified(WindowEvent e){
   
  	}
 	public void windowOpened(WindowEvent e){
   
  	}
}

先填充一个浅灰色的矩形,再填充一个黑色的矩形边框,然后随机在矩形中绘制100个椭圆作为验证码干扰,最后生成随机数。

效果图
验证码

你可能感兴趣的:(Java)