All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.atomtool.captcha.background.GradientBackgroundFactory Maven / Gradle / Ivy

The newest version!
package cn.atomtool.captcha.background;

import java.awt.*;
import java.awt.image.BufferedImage;

public class GradientBackgroundFactory implements BackgroundFactory {
    public enum Direction {
        Horizontal,
        Vertical,
        TopLeftBottomRight,
        BottomLeftTopRight
    }

    private Color startColor;
    private Color endColor;
    private Direction direction;

    public GradientBackgroundFactory() {
        this(new Color(192, 192, 0), new Color(192, 128, 128), Direction.Horizontal);
    }

    public GradientBackgroundFactory(Color startColor, Color endColor, Direction direction) {
        this.startColor = startColor;
        this.endColor = endColor;
        this.direction = direction;
    }

    public void fillBackground(BufferedImage image) {

        float x1, y1, x2, y2;

        switch (direction) {
            default:
            case Horizontal:
                x1 = 0;
                y1 = 0;
                x2 = image.getWidth();
                y2 = 0;
                break;
            case Vertical:
                x1 = 0;
                y1 = 0;
                x2 = 0;
                y2 = image.getHeight();
                break;
            case BottomLeftTopRight:
                x1 = 0;
                y1 = image.getHeight();
                x2 = image.getWidth();
                y2 = 0;
                break;
            case TopLeftBottomRight:
                x1 = 0;
                y1 = 0;
                x2 = image.getWidth();
                y2 = image.getHeight();
                break;
        }

        GradientPaint gp = new GradientPaint(x1, y1, startColor, x2, y2, endColor);

        Graphics2D g = image.createGraphics();
        g.setPaint(gp);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy