Re: Translucent volatile image appears opaque
On 7/16/2010 3:30 AM, Qu0ll wrote:
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?
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/