Original Post
Hey everyone,
I used the code provided by Tortoise for buffered image transparency, but it doesn''t seem to work. Here is my program so far:
// Test Panel for Space Game
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class TestPanel extends JPanel
{
BufferedImage background;
BufferedImage star;
BufferedImage img;
public TestPanel()
{
setPreferredSize(new Dimension(640, 480));
img = createBuffImage(640, 480, true);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
super.paint(g2);
setBackground(Color.black);
g2.setColor(Color.white);
Graphics2D gbi = img.createGraphics();
Composite originalComp = gbi.getComposite();
gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
gbi.setColor(new Color(0, 0, 0, 0));
gbi.fillRect(0, 0, img.getWidth(), img.getHeight());
gbi.setComposite(originalComp);
try
{
URL u1 = getClass().getClassLoader().getResource("Space Game/trifid_hst.jpg");
URL u2 = getClass().getClassLoader().getResource("Space Game/star_white_3.gif");
background = ImageIO.read(u1);
star = ImageIO.read(u2);
gbi.drawImage(background, null, 0, 0);
gbi.drawImage(star, null, 50, 50);
}
catch(IOException ioe) { gbi.drawString("Could not load image.", 50, 50); }
gbi.dispose();
g2.drawImage(img, null, 0, 0);
}
BufferedImage createBuffImage(int w, int h, boolean transparent)
{
// setup graphics config
GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = genv.getDefaultScreenDevice();
GraphicsConfiguration graphicsConfig = device.getDefaultConfiguration();
if (transparent)
return graphicsConfig.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
else
return graphicsConfig.createCompatibleImage(w,h,Transparency.OPAQUE);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Test Panel Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new TestPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
Tortoise, or anyone else, do you know why it doesn''t work like it''s supposed to?
Thanks,
Rob