0

I didn't know how, and there is no background image property. I researched the answer but all I could find was to set a labels icon inside a panel with a null layout. This worked and my image is there, but it is covering all but a single text field. Can I change the Z value of this label? I do not see a 'move to back' option, if there is one.

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

1

This should solve your problem:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestImage {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ContentPane panel = new ContentPane();
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.add(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    private static class ContentPane extends JPanel {
        BufferedImage image = null;

        public ContentPane() {
            try {
                String pathToImage = "C:/Users/Branislav/Pictures/sun.jpg";
                image = ImageIO.read(new File(pathToImage));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(image.getWidth(), image.getHeight());
        }
    }
}

Basically, I set image on JPanel (ContentPane). Also, size of your JPanel depends on size of image (at least in this case).

Regards.

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • Wouldn't be better to use JLabel and setIcon(ImageIcon)? – Adrian Ber May 09 '13 at 17:39
  • Yes. But he said background image. `JLabel` causes issues if you want to add more components on container (as he stated in his question). – Branislav Lazic May 09 '13 at 17:41
  • 1
    Then shouldn't you call the super's paintComponent method in the overridden paintComponent method after painting the background? – Adrian Ber May 09 '13 at 17:43
  • Oh dear...Let me guess: You are creating your code just by drag-and-drop in NetBeans GUI editor. Am I right or am I right? – Branislav Lazic May 09 '13 at 17:48
  • woah. nevermind, i accidently moved the rest of my objects down the form very far and couldnt see them... sorry for the mistake – Brandon Durst May 09 '13 at 17:49
  • @brano88 Calling super should happen after painting the background. – Adrian Ber May 09 '13 at 17:54
  • Hm...I don't think so..He will get blank page in that case. – Branislav Lazic May 09 '13 at 17:58
  • 1) Using a `JLabel` will get you the same result as this. 2) If you wanted to add components to the background `JLabel` you would need to set a `LayoutManager` (by default JPanel has FlowLayout) which you would do anyway, so no issue here. 3) When calling drawImage, don't pass in `null` as an `ImageObserver` but rather provide `this` (this is useful when image loading is slow). 4) `super.paintComponent` must be called first, then you draw the image – Guillaume Polet May 09 '13 at 18:36