3

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);
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
black
  • 357
  • 3
  • 18

1 Answers1

6

AWT components don't have a concept of transparency, they are always opaque

Try taking a look at ...

For more examples of using Swing

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    reallyyyyyyyy time to create own www site (seems like as you have bunch of free time) something like as camickr or StanislavL, most or description there will by only simple action copy_paste, not jokes – mKorbel Aug 20 '13 at 07:05
  • @mKorbel Yeah, I've thought about doing that my self ;) – MadProgrammer Aug 20 '13 at 07:36
  • @mKorbel I thought about blogging some of the more interesting questions that I find here...and yes, it would be nice to simply link people back to the blog... – MadProgrammer Aug 20 '13 at 07:55
  • right, you can see that :-) whatever, atari from dinosaurs ages:-) by Andrew Thompson too – mKorbel Aug 20 '13 at 08:00
  • @mKorbel Unfortunately due to the ..unusual nature of my hosting, I have had trouble logging in and minimal help from support. I'd be uploading a few examples there myself, but I've decided I'd pinch SO's method of formatting code using JS. Brilliant! Blogs (even SE's own blogging areas!) tend to be ..more problematic for formatting and displaying code nicely. I had nightmares trying to get the code in [this blog](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/) to look anywhere near good, while knocking out the redundant 'white space'! – Andrew Thompson Aug 26 '13 at 01:49
  • @Andrew Thompson `Nit, but in credentials you’re linking to the swing tag, not applet.` - aaaaaach I see :-) brrrrrr, yes, agree :-) – mKorbel Aug 26 '13 at 06:35