0

I want my image, a buffered Image, to have a transparent background, I first tries using a png, then a gif, then I tried using imageFilters but I could also not hit it off, so now I decided to use a simple jpeg, set the background to a color and then get rid of that color, again, I assume imageFilters would fit for that, but I don't know exactly how to use them, the color I want to get rid of is 0xff00d8 (Magenta).

Can anyone help with a way to do this, or an example?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ted
  • 629
  • 2
  • 8
  • 19

2 Answers2

2

jpeg doesn't support transparency. Make sure your buffered image also supports transparency:

BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

The A in TYPE_INT_ARGB stands for alpha which is a measure of opacity.

You need to set the pixel value to 0x00000000 in order to make it transparent.

//Load the image
BufferedImage in = ImageIO.read(img);
int width = in.getWidth(), height = in.getHeight();
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.drawImage(in, 0, 0, null);
g.dispose();

//Change the color
int colorToChange = 0xff00d8;
for (int x=0;x<width;x++)
    for (int y=0;y<height;y++)
        if(bi.getRGB(x,y)==colorToChange)
            bi.setRGB(x,y,0x00FFFFFF&colorToChange);

bi.save(new File("out.png"));
Jason
  • 13,563
  • 15
  • 74
  • 125
  • ok so now I went back to png, added the ARGB, but if I just do that, all of me image turns gray, so I assume I need to do something more, but I don't know how to choose a color from an exact pixel and turn all pixels of that color to 0x00000000, I looked how to do it a lot but can't find anything, can you help me with that? Thank you so much for all the help – Ted May 04 '13 at 14:06
  • Thank you , I tried it, but save doesn't work, trying to just put the image you called bi, instead of the original in also doesn't work and gives out the same color where transparency should be – Ted May 04 '13 at 15:13
  • how are you outputting the image? – Jason May 04 '13 at 17:27
  • I first read the image like so : image =ImageIO.read(new File("adam.png")); then I output it via a ImagePanel which extends JPanel like this : ImagePanel imagePanel = new ImagePanel(bi); It uses this method : public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, getWidth(), getHeight(), this); } – Ted May 04 '13 at 18:12
  • JPanel natively does not support transparent backgrounds. See http://stackoverflow.com/a/10059218/895876 – Jason May 04 '13 at 18:20
  • you could save it to a file? Not sure what you are trying to do – Jason May 04 '13 at 18:53
  • I read that JLabels are transparent my default, but I still have that damn grey color as background, which is not there...everywhere I read JLabels should be transparent – Ted May 04 '13 at 18:53
  • I am trying to show it in a window, I have the png as a file, I want to have it as a background ui thing, while still leaving spaces around which are transparent – Ted May 04 '13 at 18:54
  • Apparently, the image is working properly, the issue is the content pane, I need to make it transparent – Ted May 04 '13 at 19:20
  • I found http://www.pushing-pixels.org/2008/02/27/translucent-and-shaped-windows-in-core-java.html but it is only for the whole window, is there anything for the content pane only? – Ted May 04 '13 at 19:37
1

I managed to fix it using JWindow, still, thank you Jason for all of your help

I have a translucentPane extending JPanel :

public TranslucentPane() {
    setOpaque(false);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g); 

    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setComposite(AlphaComposite.SrcOver.derive(0.0f));
    g2d.setColor(getBackground());
    g2d.fillRect(0, 0, getWidth(), getHeight());

}

then I do this in my main UI :

robotUI roboUI = new robotUI();
    roboUI.setBackground(new Color(0,0,0,0));

and my content pane is :

TranslucentPane pane = new TranslucentPane();

I hope this is enough for anyone to understand

Ted
  • 629
  • 2
  • 8
  • 19