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 http://img72.imageshack.us/img72/9657/transparency.png
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.
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 is right: Unless you change it, the default Graphics2D
composite is AlphaComposite.SrcOver
. This handy tool displays the composite result for a selected rule and 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)
?
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:
- you need to call setOpaque(false) in the constructor of the target component
Either draw directly the target without backing store, or use an RGBA image:
unitImage = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);
精彩评论