1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| public class CodeUtil { private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; private static final int SIZE = 4; private static final int LINES = 5; private static final int WIDTH = 80; private static final int HEIGHT = 40; private static final int FONT_SIZE = 30;
public static Object[] createImage() { StringBuffer sb = new StringBuffer(); BufferedImage image = new BufferedImage( WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics graphic = image.getGraphics(); graphic.setColor(Color.LIGHT_GRAY); graphic.fillRect(0, 0, WIDTH, HEIGHT); Random ran = new Random(); for (int i = 0; i <SIZE; i++) { int n = ran.nextInt(chars.length); graphic.setColor(getRandomColor()); graphic.setFont(new Font( null, Font.BOLD + Font.ITALIC, FONT_SIZE)); graphic.drawString( chars[n] + "", i * WIDTH / SIZE, HEIGHT*2/3); sb.append(chars[n]); } for (int i = 0; i < LINES; i++) { graphic.setColor(getRandomColor()); graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT)); } return new Object[]{sb.toString(), image}; }
public static Color getRandomColor() { Random ran = new Random(); Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256)); return color; }
|