But that is not the case. Instead I see an opaque black background which
obliterates anything already on the screen. The graphics I rendered in the
image are appearing but the image itself does not appear to be translucent.
So, is this the expected behaviour? Does a translucent volatile image start
off life with an opaque black background? If so, how do I get it to be
translucent?
--
And loving it,
-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llS...@gmail.com
[Replace the "SixFour" with numbers to email me]
> I create a TRANSLUCENT volatile image, render some stuff in it and
> then draw the image on to the screen. I expected that the rendering
> in the image would be "overlaid" over the existing screen graphics
> and that the regions of the image that I didn't render to would be
> transparent and not obscure any graphics on the screen.
>
> But that is not the case. Instead I see an opaque black background
> which obliterates anything already on the screen. The graphics I
> rendered in the image are appearing but the image itself does not
> appear to be translucent.
>
> So, is this the expected behaviour? Does a translucent volatile image
> start off life with an opaque black background? If so, how do I get
> it to be translucent?
Do you want a translucent (heavyweight, peer) window?
<http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows>
a translucent Swing component?
<http://stackoverflow.com/questions/2234020>
or a little of both?
<http://stackoverflow.com/questions/2166500>
You may need to alter the graphic context's AlphaComposite:
<http://sites.google.com/site/drjohnbmatthews/composite>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
If I had to guess, I would say that your image isn't really translucent.
Compile and run this little SSCCE. Then change the image type to RGB
instead of ARGB and run it again.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class test extends JPanel {
private BufferedImage bi;
public test() {
setPreferredSize(new Dimension(400,300));
setBackground(Color.BLUE);
bi = new BufferedImage(400,300,BufferedImage.TYPE_INT_ARGB);
if (bi.getTransparency() == BufferedImage.TRANSLUCENT)
System.out.println("TRANSPARENT");
System.out.println(bi.getRGB(0,0));
Graphics2D g = bi.createGraphics();
g.setColor(Color.WHITE);
g.drawString("Hello World",5,20);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bi,0,0,null);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t);
f.pack();
f.setVisible(true);
}
});
}
}
--
Knute Johnson
email s/nospam/knute2010/
>I create a TRANSLUCENT volatile image, render some stuff in it and then draw
>the image on to the screen. I expected that the rendering in the image would
>be "overlaid" over the existing screen graphics and that the regions of the
>image that I didn't render to would be transparent and not obscure any
>graphics on the screen.
What colour did you draw the transparent parts with?
You probably want something like this:
private static final Color transparent = new Color( 0x00ffffff, true
);
--
Roedy Green Canadian Mind Products
http://mindprod.com
You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
> If I had to guess, I would say that your image isn't really translucent.
[...]
I have adapted your SSCCE for using a volatile image as below. The result
is that the VI has a background of opaque white and paints over the blue
background of the panel. How can I get it to be translucent? As you can
see I am explicitly creating a translucent image.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.VolatileImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class VITest extends JPanel {
private VolatileImage vi;
public VITest() {
setPreferredSize(new Dimension(500, 400));
setBackground(Color.BLUE);
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final VITest t = new VITest();
f.add(t);
f.pack();
f.setVisible(true);
t.init();
}
});
}
private void createVI() {
vi = getGraphicsConfiguration().createCompatibleVolatileImage(400,
300, Transparency.TRANSLUCENT);
}
private void init() {
createVI();
renderToVI();
}
@Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
do {
final int returnCode = vi.validate(getGraphicsConfiguration());
if (returnCode == VolatileImage.IMAGE_RESTORED) {
renderToVI();
} else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
createVI();
renderToVI();
}
final Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(vi, 0, 0, this);
} while (vi.contentsLost());
}
private void renderToVI() {
do {
if (vi.validate(getGraphicsConfiguration()) ==
VolatileImage.IMAGE_INCOMPATIBLE) {
createVI();
}
final Graphics2D g = vi.createGraphics();
g.setColor(Color.BLACK);
g.drawString("Hello World", 5, 20);
g.dispose();
} while (vi.contentsLost());
Ah thanks!. The second stackoverflow article contained what I was looking
for (i.e. setting the alpha composite to Clear before rendering to the
off-screen buffer).
OK. It is translucent but its pixels aren't. You need to get them to
have an alpha of less than 1.0f. Using the CLEAR AlphaComposite will do
that for you as John suggests. You can manipulate the pixels directly too.
> On 7/16/2010 7:59 PM, Qu0ll wrote:
> > "Knute Johnson" <nos...@rabbitbrush.frazmtn.com> wrote in message
> > news:dc%%n.10095$Zp1....@newsfe15.iad...
> >
> >> If I had to guess, I would say that your image isn't really
> >> translucent.
> >
> > [...]
> >
> OK. It is translucent but its pixels aren't. You need to get them
> to have an alpha of less than 1.0f. Using the CLEAR AlphaComposite
> will do that for you as John suggests. You can manipulate the pixels
> directly too.
Qu0ll: Not long ago, Knute summarized the principle with this pithy
apothegm: "you can't draw alpha into an image."
<http://groups.google.com/group/comp.lang.java.gui/browse_frm/thread/3be71308f7ccf1d3>