1
2
3
4
5
6
7 package com.octo.captcha.component.image.backgroundgenerator;
8
9 import com.octo.captcha.CaptchaException;
10 import com.octo.captcha.component.image.color.ColorGenerator;
11 import com.octo.captcha.component.image.color.SingleColorGenerator;
12
13 import java.awt.*;
14 import java.awt.image.BufferedImage;
15
16 /***
17 * <p/>
18 * Gradient background. Use the constructor to specify colors. Default color are Black and Gray </p>
19 *
20 * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue </a>
21 * @version 1.0
22 */
23 public class GradientBackgroundGenerator extends AbstractBackgroundGenerator {
24
25 ColorGenerator firstColor = null;
26
27 ColorGenerator secondColor = null;
28
29 public GradientBackgroundGenerator(Integer width, Integer height, Color firstColor,
30 Color secondColor) {
31 super(width, height);
32 if (firstColor == null || secondColor == null) {
33 throw new CaptchaException("Color is null");
34 }
35 this.firstColor = new SingleColorGenerator(firstColor);
36 this.secondColor = new SingleColorGenerator(secondColor);
37
38 }
39
40 public GradientBackgroundGenerator(Integer width, Integer height, ColorGenerator firstColorGenerator,
41 ColorGenerator secondColorGenerator) {
42 super(width, height);
43 if (firstColorGenerator == null || secondColorGenerator == null) {
44 throw new CaptchaException("ColorGenerator is null");
45 }
46 this.firstColor = firstColorGenerator;
47 this.secondColor = secondColorGenerator;
48 }
49
50 /***
51 * Generates a backround image on wich text will be paste. Implementations must take into account the imageHeigt and
52 * imageWidth.
53 *
54 * @return the background image
55 */
56 public BufferedImage getBackground() {
57 BufferedImage bi = new BufferedImage(getImageWidth(), getImageHeight(),
58 BufferedImage.TYPE_INT_RGB);
59 Graphics2D pie = (Graphics2D) bi.getGraphics();
60 pie.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
61 GradientPaint gp = new GradientPaint(0, getImageHeight(), firstColor.getNextColor(), getImageWidth(), 0,
62 secondColor.getNextColor());
63 pie.setPaint(gp);
64 pie.fillRect(0, 0, getImageWidth(), getImageHeight());
65 pie.dispose();
66 return bi;
67 }
68 }