Huazhe Yin
Huazhe Yin

Reputation: 365

Change the color of each pixel in an image java

I wanna change different pixel to different color. Basically, change part of pixel to transparent.

for(int i = 0; i < image.getWidth();i++)
        for(int j = 0; j < image.getHeight(); j ++)
        {
            image.setRGB(i,j , 0);
        }

//I aslo change the third parameter 0 to another attribute. but it still does not work. it all show black. do you have some ideas?

yin. thanks

class ImagePanel extends JPanel {

    private BufferedImage image;

    public ImagePanel(int width, int height, BufferedImage image) {
        this.image = image;
        image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        repaint();
    }

    /**
     * Draws the image.
     */

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < image.getWidth(); i++) {
            for (int j = 0; j < image.getHeight(); j++) {
                image.setRGB(i, j, 0);
            }
        }
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

    }

}

Upvotes: 4

Views: 32711

Answers (4)

Adam
Adam

Reputation: 36733

The third parameter is ARGB value in 32 bits. This is laid out in bit form as:

AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBBB

See the javadoc for BufferedImage.setRGB (assuming your using BufferedImage, your question doesn't actually say...)

Sets a pixel in this BufferedImage to the specified RGB value. The pixel is assumed to be in the default RGB color model, TYPE_INT_ARGB, and default sRGB color space. For images with an IndexColorModel, the index with the nearest color is chosen

  • If you're using an image type that supports transparency it is important you set alpha 255 means fully opaque, 0 is fully transparent.

You can create such a value using bit shifting.

int alpha = 255; 
int red   = 0;
int green = 255;
int blue  = 0;

int argb = alpha << 24 + red << 16 + green << 8 + blue

image.setRGB(i, j, argb);

Luckily there is a getRGB() method on java.awt.Color instances, so you could use

image.setRGB(i, j, Color.green.getRGB());

Here's a full working example, perhaps you can compare to your code:

public class StackOverflow27071351 {
    private static class ImagePanel extends JPanel {
        private BufferedImage image;
        public ImagePanel(int width, int height, BufferedImage image) {
            this.image = image;
            image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_ARGB);
            repaint();
        }
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (int i = 0; i < image.getWidth(); i++) {
                for (int j = 0; j < image.getHeight(); j++) {
                    image.setRGB(i, j, new Color(255, 0, 0, 127).getRGB());
                }
            }
            g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        int width = 640;
        int height = 480;
        frame.setSize(width, height);
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(new ImagePanel(width, height, image));
        frame.setVisible(true);
    }
}

Upvotes: 8

Phil Freihofner
Phil Freihofner

Reputation: 7910

I use the following form:

int[] pixel = new int[4];

// the following four ints must range 0..255
pixel[0] = redValue;
pixel[1] = greenValue;
pixel[2] = bluleValue;
pixel[3] = alphaValue;

raster.setPixel(x, y, pixel);

To get a raster for a BufferedImage, I do this:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = image.getRaster(); 

I've done some performance testing, and have not found that stuffing all the bytes of color values into a single number to make much of a difference.

It is also good to know the technique where one can draw an opaque image (e.g., RGB rather than an ARGB) with an alpha value.

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) (yourAlpha)));
g2d.drawImage(...);

Upvotes: 1

Ammar
Ammar

Reputation: 719

here is a sample code:

private int colorToRGB(int alpha, int red, int green, int blue) {
        int newPixel = 0;
        newPixel += alpha;
        newPixel = newPixel << 8;
        newPixel += red;
        newPixel = newPixel << 8;
        newPixel += green;
        newPixel = newPixel << 8;
        newPixel += blue;

        return newPixel;
    }

then

image.setRGB(i, j, colorToRGB(alpha, 0, 0, 0))

Upvotes: 1

striving_coder
striving_coder

Reputation: 798

Well, 3rd parameter is the color in RGB, so it will be black if you set it to 0.

Upvotes: 2

Related Questions