2

Currently, I'm using this code to generate a background image for my JFrame:

    BufferedImage myImg = ImageIO.read(myUrl);
    this.setContentPane(new ImagePanel(myImg));

    //ImagePanel class
    public class ImagePanel extends JComponent {
        private Image image;
        public ImagePanel(Image image) {
            this.image = image;
        }

        @Override
        protected void paintComponent(Graphics g) {
            g.drawImage(image, 0, 0, null);
        }
    }

As a result of this, the image covers up everything that I have in the JFrame. How can I toggle the transparency of the image being drawn so that I can still see my content while having the image set as the background?

  • If the image is covering everything on the fame, then you're doing something wrong (like not calling super.paintComponent). Take a look at AffineTrnsformatio, but remember, the graphics context is a shared resource – MadProgrammer Jun 10 '13 at 03:54
  • Well using super.paintComponent still covered up everything. And I'm not sure what you mean by the last bit, sorry, still pretty unfamiliar with the swing package. –  Jun 10 '13 at 03:56
  • Here's a duplicate. http://stackoverflow.com/questions/660580/change-the-alpha-value-of-a-bufferedimage Hope the solution helps... – aksh1t Jun 10 '13 at 03:59
  • @KeybordPiano459 : Hopefully this [example](http://stackoverflow.com/a/11428289/1057230) might be of some help !! – nIcE cOw Jun 10 '13 at 04:25

2 Answers2

4
protected void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);

Should be:

protected void paintComponent(Graphics g) {
        super.paintComponent(g);  // paint the parent component!
        g.drawImage(image, 0, 0, this);

E.G.

  1. Answer to Drawing a background image.
  2. Answer to Background image hides all GUI design components.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I see that you changed that, but the code didn't actually change anything about what happened- how do I make the image transparent? –  Jun 10 '13 at 03:54
  • Calling super first means that you cover whatever the parent drew with the image, making it a foreground and not a background. Reverse the two lines. – Mel Nicholson Jun 10 '13 at 04:00
  • @MelNicholson No it doesn't. See the linked SSCCEs on the answers. – Andrew Thompson Jun 10 '13 at 04:02
4

Why do you keep talking about transparency? You would only care about transparency if you are painting an image "on top" of your components.

If you want a background image on your frame then you add the image to a panel and add the panel to the frame. Then you add other components to the background panel so that those components are displayed on top of the image.

This has nothing to do with transparency.

The background panel is generally a JPanel, not a JComponent since a panel is designed to be used as a container.

See Background Panel for an example.

You can't just change an image to make it transparent. You need to load an image with transparent pixels.

camickr
  • 321,443
  • 19
  • 166
  • 288