"Knute Johnson" <nospam@rabbitbrush.frazmtn.com> wrote in message
news:dc%%n.10095$Zp1.6880@newsfe15.iad...
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());
}
}
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.