Color startingColor, Color endingColor) throws IOException {
BufferedImage bufferedImage;
Graphics2D graphics;
PNGEncodeParam param;
PNGImageEncoder captchaPNGImage;
// Create the CAPTCHA Image.
bufferedImage = new BufferedImage(CAPTCHAConstants.DEFAULT_CAPTCHA_WIDTH, CAPTCHAConstants.DEFAULT_CAPTCHA_HEIGHT,
BufferedImage.TYPE_BYTE_INDEXED);
// Setup the graphics object.
graphics = bufferedImage.createGraphics();
applyCurrentGradientPaint(graphics, bufferedImage.getWidth(),
bufferedImage.getHeight(), startingColor, endingColor);
graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage
.getHeight());
graphics.setColor(Color.black);
// Draw text on the CAPTCHA image.
drawTextOnImage(graphics, captchaText);
// Apply noise on the CAPTCHA image.
applyNoiseOnImage(graphics, bufferedImage.getWidth(), bufferedImage
.getHeight(), startingColor, endingColor);
// Draw the image border.
drawBorders(graphics, bufferedImage.getWidth(), bufferedImage
.getHeight());
// Set the reponse content type to jpeg.
response.setContentType("image/jpg");
param = PNGEncodeParam.getDefaultEncodeParam(bufferedImage);
captchaPNGImage = new PNGImageEncoder(response.getOutputStream(), param);
captchaPNGImage.encode(bufferedImage);
}