Acor74u
Acor74u

Reputation: 17

transparent .png background

I know Im not the first person to ask this type of question, but I think mines a little bit different.

I have a png image I drew on MS paint that is a player and I want the background of the image to be transparent when I use a graphics objects to draw the image. I tried some stuff with magic pink but It doesn't seem to be working the same in java. Im not new to java, but Im inexperienced so could you explain any packages or methods that you use thanks!

Upvotes: 0

Views: 342

Answers (2)

Sage
Sage

Reputation: 15418

  1. You will need to use AlphaComposite to have the transparency effect:
  2. Assuming that you already know Graphics2D and Graphics uses BufferedImage
  3. Creating temporary graphics object g.create() and then dispose the object for safely restore the state of graphics object changed after the object creation.

    protected void paintComponent(Graphics g) {
        super.paintComponent(g); 
    
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
        g2d.drawImage(tileImage, 0, 0, getWidth(), getHeight());
        g2d.dispose();
    
        // draw player image
    
    } 
    

Upvotes: 1

saopayne
saopayne

Reputation: 177

With Java6, a PNG picture should be used for TrayIcon, but as mentioned in this SO question, check:

the background color chosen to represent the transparent pixels
the transparency options
the resolution of the icon
alternate format like SVG (provided you are using external library like Batik, and     
conversion mechnism to java.awt.Image)

Upvotes: 0

Related Questions