Make a simple picture verification code in Java

Source: Internet
Author: User

Java implementation of simple verification code function

Package project;

Import Java.awt.Color;

Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import Java.awt.image.BufferedImage;

Import Javax.swing.Icon;
Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JTextField;

@SuppressWarnings ("Serial")
public class Denglu extends JFrame {

Private JLabel name, pass, card, Imagecard;
Private JTextField Nametext, Passtext, Cardtext;
Private JButton login;
private int width = n, height = 30;
Private String str = "";
Public Denglu () {
Settitle ("login form");
SetFont (New Font ("", Font.Bold, 24));
SetLayout (null);//Custom layout

Responsible for generating verification code picture
Icon icon = new ImageIcon (getcardimage (width, height));
name = new JLabel ("account");
pass = new JLabel ("password");
Card = new JLabel ("Verification Code");
Imagecard = new JLabel (icon);

Nametext = new JTextField ();
Passtext = new JTextField ();
Cardtext = new JTextField ();

Login = new JButton ("login");

Name.setbounds (80, 20, 60, 30);
Pass.setbounds (80, 60, 60, 30);
Card.setbounds (80, 100, 60, 30);
Imagecard.setbounds (+, +, width, height);

Nametext.setbounds (150, 20, 200, 30);
Passtext.setbounds (150, 60, 200, 30);
Cardtext.setbounds (150, 100, 80, 30);

Login.setbounds (120, 160, 220, 30);

Add (name);
Add (pass);
Add (card);
Add (Imagecard);

Add (Nametext);
Add (Passtext);
Add (Cardtext);

Add (login);

Registering events
Addmouselistener (New Changecard ());

SetBounds (0, 0, 450, 260);
Setlocationrelativeto (NULL);
Setdefaultcloseoperation (Jframe.exit_on_close);
SetVisible (TRUE);
}

Public Image getcardimage (int width, int height) {
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);

Graphics g = image.getgraphics ();
int red1 = (int) (Math.random () * 256);
int green1 = (int) (Math.random () * 256);
int blue1 = (int) (Math.random () * 256);
Color C1 = new Color (red1, green1, blue1);

G.setcolor (C1);
G.fillrect (0, 0, width, height);


Generate text
int red2 = (int) (Math.random () * 256);
int green2 = (int) (Math.random () * 256);
int blue2 = (int) (Math.random () * 256);
Color C2 = new Color (red2, green2, blue2);
str = "";
for (int i = 0; i < 4; i++) {
char ch = (char) ((int) (Math.random () *26+65));
Str+=ch;
}


Adding interference points
for (int i = 0; i <; i++) {
int red11 = (int) (Math.random () *256);
int green11 = (int) (Math.random () *256);
int blue11 = (int) (Math.random () *256);

Color Color3 = new Color (red11, green11, BLUE11);
G.setcolor (COLOR3);
int x1 = (int) (Math.random () *width);
int y1 = (int) (Math.random () *height);
int x2 = (int) (Math.random () *width);
int y2 = (int) (Math.random () *height);
G.drawline (x1, y1, x2, y2);


}



G.setcolor (C2);
int x = (int) (Math.random () * (WIDTH/3));
int y = (int) (Math.random () * (HEIGHT/2) +10);
G.setfont (New Font ("", Font.Bold, 24));
g.DrawString (str, x, y);

return image;
}

Listener class: Executes when mouse clicks, that is, once again randomly generates a verification code when the verification code is clicked
Class Changecard implements mouselistener{

@Override
public void mouseclicked (MouseEvent e) {
Icon icon = new ImageIcon (getcardimage (width, height));
Imagecard.seticon (icon);
}

@Override
public void mousepressed (MouseEvent e) {
TODO auto-generated Method Stub

}

@Override
public void mousereleased (MouseEvent e) {
TODO auto-generated Method Stub

}

@Override
public void mouseentered (MouseEvent e) {
TODO auto-generated Method Stub

}

@Override

public void mouseexited (MouseEvent e) {
TODO auto-generated Method Stub

}

}

public static void Main (string[] args) {
New Denglu ();
}
}

Make a simple picture verification code in Java

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.