AWT custom window shape using transparent image
awt, java, swing, transparency
Solution
AWT components don't have a concept of transparency, they are always opaque
Try taking a look at ...
- How can I smooth my JFrame shape
- JFrame the same shape as an Image / Program running in background
- Java Swing: Transparent PNG permanently captures original background
- How to Create translucent and Shaped Windows
For more examples of using Swing
Problem
I am trying to make a UI using AWT. I want to use only images and transparent components. Right now I cant understand how to make a main window which is supposed to be a PNG image with a custom shape. All the areas that are transparent in the image are replaced with a black color. Here is the code I use: ``` public class Test { static Image image; /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { //switch to the right thread image = ImageIO.read(Test.class.getClassLoader().getResource("resources/images/panel.png").openStream()); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Frame frame = new Frame("Test"); frame.setUndecorated(true); frame.setBackground(new Color(0,0,0,0)); frame.add(new BackGround(image,image.getWidth(frame),image.getHeight(frame))); frame.pack(); frame.setSize(image.getWidth(frame), image.getHeight(frame)); frame.setVisible(true); frame.setLocationRelativeTo(null); } } ); } private static class BackGround extends Component { private Image img; private int wid, hgt; public BackGround(Image img, int wid, int hgt){ this.img=img; this.wid=wid; this.hgt=hgt; } @Override public void paint(Graphics graphics) { graphics.drawImage(image,0,0,wid,hgt,0,0,wid,hgt,null); } } } ```