設計:用Swing中Graphics繪製圖片,圖片使用隨機背景色,隨機字符串;繪製字符串隨機位置,隨機大小,隨機顏色;背景中加入若干隨機位置和顏色的混亂線、混亂點。
效果:
X
源代碼: package com; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.p_w_picpath.BufferedImage; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class AuthCode extends JDialog implements ActionListener { private JPanel p_w_picpathPanel, buttonPanel;r /> private JTextField field; private JButton okButton, changeButton; private Random random; private BufferedImage p_w_picpath; private String[] s; public AuthCode() { init(); setTitle('驗證碼'); Toolkit kit = Toolkit.getDefaultToolkit(); Dimension s = kit.getScreenSize(); int screenWidth = s.width; int screenHeight = s.height; setBounds(screenWidth / 3, screenHeight / 3, 300, 200); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); } public void init() { p_w_picpathPanel = new JPanel(); field = new JTextField(); buttonPanel = new JPanel(); okButton = new JButton('肯定'); changeButton = new JButton('更換'); setImage(); p_w_picpathPanel.add(new JLabel(new ImageIcon(p_w_picpath))); buttonPanel.add(changeButton); buttonPanel.add(okButton); add(p_w_picpathPanel, BorderLayout.NORTH); add(field, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); okButton.addActionListener(this); changeButton.addActionListener(this); } public static void main(String[] args) { new AuthCode().setVisible(true); } public void setImage() { // 建立內存圖像 int width = 200, height = 90; p_w_picpath = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = p_w_picpath.getGraphics(); // 取得隨機數對象 random = new Random(); // 填充矩形區域 g.setColor(new Color(random.nextInt(255), random.nextInt(255), random .nextInt(255))); g.fillRect(0, 0, width, height); // 畫矩形邊框 g.setColor(new Color(255, 255, 255)); g.drawRect(0, 0, width - 1, height - 1); // 畫1000個混亂點 for (int i = 0; i < 1000; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = x; int yl = y; g.drawLine(x, y, xl, yl); g.setColor(new Color(20 + random.nextInt(200), 20 + random .nextInt(200), 20 + random.nextInt(200))); } // 畫100條混亂線 for (int i = 0; i < 100; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(100); int yl = random.nextInt(100); g.drawLine(x, y, x + xl, y + yl); g.setColor(new Color(20 + random.nextInt(200), 20 + random .nextInt(200), 20 + random.nextInt(200))); } // 隨機獲取四個字母或數字型字符 s = new String[4]; s[0] = setRndomString()[random.nextInt(62)]; s[1] = setRndomString()[random.nextInt(62)]; s[2] = setRndomString()[random.nextInt(62)]; s[3] = setRndomString()[random.nextInt(62)]; // 隨機生成四個字符大小和位置,並將字符畫到圖片上 g.setFont(new Font('Times New Roman', Font.PLAIN, 30 + random .nextInt(20))); g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); g.drawString(s[0], 20 + random.nextInt(15), 40 + random.nextInt(30)); g.setFont(new Font('Times New Roman', Font.PLAIN, 30 + random .nextInt(20))); g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); g.drawString(s[1], 50 + random.nextInt(15), 40 + random.nextInt(30)); g.setFont(new Font('Times New Roman', Font.PLAIN, 30 + random .nextInt(20))); g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); g.drawString(s[2], 90 + random.nextInt(15), 40 + random.nextInt(30)); g.setFont(new Font('Times New Roman', Font.PLAIN, 30 + random .nextInt(20))); g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); g.drawString(s[3], 130 + random.nextInt(15), 40 + random.nextInt(30)); g.dispose(); } public String[] setRndomString() { String[] s = new String[62]; for (int i = 0; i < 10; i++) { s[i] = Integer.toString(i); } for (int i = 10; i < 36; i++) { s[i] = Character.toString((char) ('a' + i - 10)); } for (int i = 36; i < 62; i++) { s[i] = Character.toString((char) ('A' + i - 36)); } return s; } public void actionPerformed(ActionEvent e) { if (e.getSource() == okButton) { if (field.getText().toLowerCase().equals( s[0].toLowerCase() + s[1].toLowerCase() + s[2].toLowerCase() + s[3].toLowerCase())) { JOptionPane.showMessageDialog(this, '輸入正確,登錄成功!'); } else { JOptionPane.showMessageDialog(this, '輸入錯誤,登錄失敗!'); } } else { setImage(); p_w_picpathPanel.removeAll(); p_w_picpathPanel.add(new JLabel(new ImageIcon(p_w_picpath))); p_w_picpathPanel.updateUI(); } } }