4

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);

    }

}
Adam
  • 35,919
  • 9
  • 100
  • 137
Huazhe Yin
  • 355
  • 1
  • 4
  • 19

4 Answers4

8

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);
    }
}
Adam
  • 35,919
  • 9
  • 100
  • 137
  • Thanks for answering. I use BufferedImage, and i try in ur way. but it doesn't work either. i use a black image( an image with all black background). – Huazhe Yin Nov 23 '14 at 14:28
  • Can you post your code to create bufferedimage and display it – Adam Nov 23 '14 at 17:59
  • @HuazheYin I've added a fully working example showing red, I've not seen the all black problem you describe... – Adam Nov 26 '14 at 22:28
2

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

striving_coder
  • 798
  • 1
  • 5
  • 7
1

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))
Ammar
  • 719
  • 6
  • 21
  • thanks for answering. what is the alpha, what value should i pass to it? – Huazhe Yin Nov 23 '14 at 14:30
  • the alpha value is between 0(transparent) -> 255, don't forget as @Adam mentioned to set your BufferedImage to TYPE_INT_ARGB – Ammar Nov 23 '14 at 14:42
  • i just tried it, it still does not work. my program is simple, i just put a buffered image into a frame and i can change the frame transparency, but i wanna change the partial transparency. do u have more resolution? – Huazhe Yin Nov 23 '14 at 15:07
  • if you just want to change frame transparency I recommend to use AlphaComposite. Check this: http://www.java2s.com/Code/Java/2D-Graphics-GUI/MakeimageTransparency.htm – Ammar Nov 23 '14 at 15:14
  • i know how to change the transparency of a frame, and i implement it. it can change the whole transparency of frame, but my requirement is change partial transparency, thats why i use buffered image. so i change the partial transparency of frame by changing the partial transparency of buffered image. – Huazhe Yin Nov 23 '14 at 15:48
1

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(...);
Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41