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   /*
8    * jcaptcha, the open source java framework for captcha definition and integration
9    * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
10   * See the LICENSE.txt file distributed with this package.
11   */
12  package com.octo.captcha.component.image.color;
13  
14  import com.octo.captcha.CaptchaException;
15  
16  import java.awt.*;
17  import java.security.SecureRandom;
18  import java.util.Random;
19  
20  /***
21   * A RandomListColor returns a random color have been picked from a user defined colors list.
22   *
23   * @author Benoit Doumas
24   * @author Chrsitian Blavier
25   */
26  public class RandomListColorGenerator implements ColorGenerator {
27      /***
28       * List of colors that can be selected
29       */
30      private Color[] colorsList = null;
31  
32      /***
33       * Use for random color selection
34       */
35      private Random random = new SecureRandom();
36  
37      /***
38       * Constructor that take an array of Color
39       *
40       * @param colorsList the array of color
41       */
42      public RandomListColorGenerator(Color[] colorsList) {
43          if (colorsList == null) {
44              throw new CaptchaException("Color list cannot be null");
45          }
46          for (int i = 0; i < colorsList.length; i++) {
47              if (colorsList[i] == null) {
48                  throw new CaptchaException("One or several color is null");
49              }
50          }
51          this.colorsList = colorsList;
52      }
53  
54      /***
55       * @see com.octo.captcha.component.image.color.ColorGenerator#getNextColor()
56       */
57      public Color getNextColor() {
58          int index = random.nextInt(this.colorsList.length);
59          return this.colorsList[index];
60      }
61  
62  }