views:

114

answers:

3

Hi there. I noted that when i draw something with Color(0,0,0,0), which is over another image, the color shown is the JFrame background, not the image just below it.

Reasons that'd help me to find a solution?

Thanks!!

Edit: See the circles, the grey area (corners) should be transparent but are not, instead, they are the color of the JFrame. alt text

And here is the code to draw 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);

Been toying with the Alphacomposites, i think its not the solution. So i added all this new info which i believe, will help you guys to give me another tip.

+2  A: 

Did you set the AlphaComposite before you draw?

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 note tested

Chuk Lee
+2  A: 

@Chuk Lee is right: Unless you change it, the default Graphics2D composite is AlphaComposite.SrcOver. This handy tool displays the composite result for a selected rule a specified pair of color and alpha.

Addendum: One approach is to override paintComponent() and render both map and circles, but you might be able to 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);
...

Does createImage(30, 30) relate to diameter? For what component do you override paint() and invoke super.paintComponent(g)?

trashgod
Nice tool. 3 chars.
Gabriel A. Zorrilla
+2  A: 

You are painting with an opacity of 0. If it's black you want, use Color(0,0,0,255).

UPDATE

Sorry, I have misunderstood your question. Having read the code you added to your post, I assume that the little targets are components placed on top of the map which is drawn on a parent component.

To ensure that transparency is used you need two things:

  1. you need to call setOpaque(false) in the constructor of the target component
  2. Either draw directly the target without backing store, or use an RGBA image:

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

Maurice Perry
The problem is the filled rect in the buffered image should be the color of the layer below it, also a buffered image. I set 0 as transparency.
Gabriel A. Zorrilla
Sorry, updated the post
Maurice Perry
In the field area of the class i have declared:BufferedImage unitImage = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);In the paint method of the same class:unitImage = (BufferedImage) (createImage(30, 30)); final Graphics2D gc = unitImage.createGraphics();---drawing code here---And now the targets are not drawn at all. Or they are transparent as well!
Gabriel A. Zorrilla
Check the opacity of your colors (255, not 0)
Maurice Perry
Oh, and call gc.dispose() when you are done with it.
Maurice Perry
I'm not sure I have been clear enough: you should replace unitImage = (BufferedImage) (createImage(30, 30)); with unitImage = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB); and not declare a local variable as your comment suggests.
Maurice Perry