Transparency in bufferedimage objects

I noticed that when I draw something with a color (0,0,0,0) that is above another image, the displayed color is the background of the JFrame, not the image below it.

Reasons to help me find a solution?

Thanks!!

Edit: see circles, the gray area (corners) should be transparent, but they are the JFrame color instead. alt text http://img72.imageshack.us/img72/9657/transparency.png

And here is the code for drawing the circles:

public void paint (final Graphics g) {
        super.paintComponent (g);
        final Graphics2D g2 = (Graphics2D) g;

        RenderingHints rh = new RenderingHints (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (unitImage == null) {

            unitImage = (BufferedImage) (createImage (30, 30));
            final Graphics2D gc = unitImage.createGraphics ();
            gc.setRenderingHints (rh);
            gc.setColor (outsideColor);
            gc.fillOval (0, 0, diameter, diameter);
            gc.setColor (middleColor);
            gc.fillOval (diameter / 6, diameter / 6, (diameter / 3) * 2, (diameter / 3) * 2);
            gc.setColor (innerColor);
            gc.fillOval (diameter / 3, diameter / 3, diameter / 3, diameter / 3);
        }

        g2.drawImage (unitImage, null, 0, 0);

I thought that with Alphacomposites I think this is not a solution. So I added all this new information that I believe will help you guys give me some more advice.

+2


a source to share


3 answers


@Chuk Lee is right: if you don't change it, then composition Graphics2D

will be AlphaComposite.SrcOver

. This handy tool displays a composite result for a selected rule and a given color and alpha pair.

Adding. One approach is to override paintComponent()

and display both the map and the circles, but you can make the corners transparent by clearing the alpha:



...
gc.setRenderingHints(rh);
gc.setComposite(AlphaComposite.Clear);
gc.fillRect(0, 0, diameter, diameter);
gc.setComposite(AlphaComposite.Src);
gc.setColor(outsideColor);
...

      

Whether it connected createImage(30, 30)

with diameter

? For which component are you overriding paint()

and calling super.paintComponent(g)

?

+6


a source


Have you installed AlphaComposite before drawing?

Graphics2D g2d = (Graphics2D)g; //Some graphics object
//Save the original
Composite original = g2d.getComposite();
//Set to semi translucent
Composite translucent = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
g2d.setComposite(translucent);

//Draw

//Set back to original
g2d.setComposite(original);

      



Disclaimer: Code Verification Verified

+7


a source


You are painting with an opacity of 0. If you want black, use the color (0,0,0,255).

UPDATE

Sorry, I misunderstood your question. After reading the code added to your post, I am assuming the small targets are components placed on top of the map that are drawn on the parent component.

To provide transparency, you need two things:

  • you need to call setOpaque (false) in the constructor of the target component
  • Either output the target directly with no backing store, or use an RGBA image:

    unitImage = new BufferedImage (30, 30, BufferedImage.TYPE_INT_ARGB);

+2


a source







All Articles