View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   package com.octo.captcha.component.image.backgroundgenerator;
8   
9   import java.awt.*;
10  import java.awt.geom.Arc2D;
11  import java.awt.image.BufferedImage;
12  
13  /***
14   * <p>Black ellipses drawn on a white background</p>
15   *
16   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
17   * @version 1.0
18   */
19  public class EllipseBackgroundGenerator extends AbstractBackgroundGenerator {
20  
21      public EllipseBackgroundGenerator(Integer width, Integer height) {
22          super(width, height);
23      }
24  
25      /***
26       * Generates a backround image on wich text will be paste. Implementations must take into account the imageHeigt and
27       * imageWidth.
28       *
29       * @return the background image
30       */
31      public BufferedImage getBackground() {
32          BufferedImage bimgTP = new BufferedImage(getImageWidth(),
33                  getImageHeight(), BufferedImage.TYPE_INT_RGB);
34          Graphics2D g2d = bimgTP.createGraphics();
35          // g2d.setColor(Color.white);
36          //g2d.fillRect(0, 0, getImageWidth(), getImageHeight());
37          BasicStroke bs = new BasicStroke(2.0f, BasicStroke.CAP_BUTT,
38                  BasicStroke.JOIN_MITER, 2.0f, new float[]{2.0f, 2.0f}, 0.0f);
39          g2d.setStroke(bs);
40          AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
41                  0.75f);
42          g2d.setComposite(ac);
43  
44          g2d.translate(getImageWidth() * -1.0, 0.0);
45          double delta = 5.0;
46          double xt;
47          double ts = 0.0;
48          for (xt = 0.0; xt < (2.0 * getImageWidth()); xt += delta) {
49              Arc2D arc = new Arc2D.Double(0, 0,
50                      getImageWidth(), getImageHeight(), 0.0, 360.0, Arc2D.OPEN);
51              g2d.draw(arc);
52              g2d.translate(delta, 0.0);
53              ts += delta;
54          }
55          return bimgTP;
56      }
57  }