Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWT custom window shape using transparent image

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);
        }
    }
}
like image 1000
black Avatar asked Apr 23 '26 17:04

black


1 Answers

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

like image 143
MadProgrammer Avatar answered Apr 26 '26 07:04

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!