Generate Color Verification Code

Source: Internet
Author: User
Tags comments set background window
Verification code This article will teach you how to use JavaBean step-by-step to achieve the entire process of producing color verification code, and provide source code.


FileName: Image.java
=====================================
/*
* Created on 2004-8-4
*
* To change the template for this generated file go
* Window>preferences>java>code Generation>code and Comments
*/
Package myclass.test;

Import java.awt.*;
Import java.awt.image.*;
Import java.util.*;

/**
* @author
*
* To change the template for this generated type comment go
* Window>preferences>java>code Generation>code and Comments
*/
public class Image {

Public String srand= "";

Public color getrandcolor (int fc,int BC) {//random color for a given range
Random Random = new Random ();
if (fc>255) fc=255;
if (bc>255) bc=255;
int R=fc+random.nextint (BC-FC);
int G=fc+random.nextint (BC-FC);
int B=fc+random.nextint (BC-FC);
return new Color (R,G,B);
}
Public BufferedImage Creatimage () {

Create an image in memory
int width=60, height=20;
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);

Get Graphics context
Graphics g = image.getgraphics ();

Generate Random Class
Random Random = new Random ();

Set Background color
G.setcolor (Getrandcolor (200,250));
G.fillrect (0, 0, width, height);

Set font
G.setfont (New Font ("Times New Roman", font.plain,18));



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.