Nice Mice
Nice Mice

Reputation: 39

How to create a colour gradient without Javafx/AWT?

so i have a little Problem and i try to solve it since hours. I have a BufferedImage and i want to change the colour in a fluent way f.e. from red to white. My main:

public static void main(String[] args) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int x = 0; x != width; x++) {
        for (int y = 0; y != height; y++) {
            image.setRGB(x, y, color12(x, y));



    try {
        ,,,

My method to change the color:

static int color12(int x, int y) {      
    int size = 100;
    if (Math.abs(width / 2 - x) < size / 2 && Math.abs(height / 2 - y) < size / 2)
        return new Color(255, 0, 0).getRGB();
    else
        return new Color(200, 200, 255).getRGB();
}

}

So i played around with the method, but i cant get it done. My best "solution" was this:

int r = 0 ;     
    int b = 0;
    int g = 0;
    for (int i = 1; i < 255; i++) 
r++; 

else return new Color(r,g,b).getRGB();

Can anyone help me?

Upvotes: 0

Views: 251

Answers (1)

d.j.brown
d.j.brown

Reputation: 1842

I'm not sure how you want the gradient (e.g. horizontal, vertical or diagonal), but here's an example using linear interpolation for a horizontal or vertical gradient.

import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class ExampleFrame extends JFrame {

    private static enum GradientOrientation {  
        HORIZONTAL, VERTICAL
    };

    private static BufferedImage createGradientImg(int width, int height, Color startColor, Color endColor, GradientOrientation o) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int pos = o.equals(GradientOrientation.HORIZONTAL) ? x : y;
                int size = o.equals(GradientOrientation.HORIZONTAL) ? width : height;
                image.setRGB(x, y, getGradientRGB(startColor, endColor, pos, size));
            }
        }
        return image;
    }

    private static int getGradientRGB(Color startColor, Color endColor, int pos, int size) {
        double perc = (double) pos / size;
        int newRed = (int) (endColor.getRed() * perc + startColor.getRed() * (1 - perc));
        int newGreen = (int) (endColor.getGreen() * perc + startColor.getGreen() * (1 - perc));
        int newBlue = (int) (endColor.getBlue() * perc + startColor.getBlue() * (1 - perc));
        return new Color(newRed, newGreen, newBlue).getRGB();
    }

    public void createAndShow() {
        BufferedImage img1 = createGradientImg(200, 100, Color.RED,
                Color.WHITE, GradientOrientation.HORIZONTAL);
        BufferedImage img2 = createGradientImg(200, 100, Color.BLUE,
                Color.YELLOW, GradientOrientation.HORIZONTAL);
        BufferedImage img3 = createGradientImg(200, 100, Color.WHITE,
                Color.YELLOW, GradientOrientation.VERTICAL);
        BufferedImage img4 = createGradientImg(200, 100, Color.BLACK,
                Color.WHITE, GradientOrientation.VERTICAL);

        BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
        getContentPane().setLayout(layout);
        getContentPane().add(new JLabel(new ImageIcon(img1)));
        getContentPane().add(new JLabel(new ImageIcon(img2)));
        getContentPane().add(new JLabel(new ImageIcon(img3)));
        getContentPane().add(new JLabel(new ImageIcon(img4)));
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ExampleFrame ef = new ExampleFrame();
                ef.createAndShow();
            }
        });
    }
}

Upvotes: 1

Related Questions