2

How can I set the wall1.jpg as background to my JFrame or JPanel?

public class JBackGroundImageClass extends JFrame
{
  Image img;
  private final JPanel JPanel;

  public JBackGroundImageClass()
  {
    setLayout (new BorderLayout ());    
    setBounds(22,33,400, 400);
    setVisible(true);

    img = Toolkit.getDefaultToolkit().createImage("wall1.jpg");    
    JPanel = new JPanel()
    {
      public void paintComponent(Graphics g)
      {img = Toolkit.getDefaultToolkit().createImage("wall1.jpg");
        g.drawImage(img, 0, 0, null);
      }
    };

    this.add("North" , JPanel);
    JPanel.setSize(400, 400);
    JPanel.setBackground(Color.red);
    JPanel.setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }


  public static void main(String[] args) 
  {
    new JBackGroundImageClass().setVisible(true);
  }

}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[Painting in AWT and Swing: The Paint Methods](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks). – trashgod Nov 04 '11 at 21:27
  • @trashgod: I retried but still same. –  Nov 04 '11 at 21:30
  • check the accepted answer in this post. I have used code from the same link before and it works: http://stackoverflow.com/questions/4051408/jpanel-with-image-background –  Nov 04 '11 at 21:50
  • Not exact, but perhaps helpful: http://stackoverflow.com/questions/1549346/images-and-panels/1549386#1549386 – SingleShot Nov 04 '11 at 21:51

2 Answers2

1

Here's one common approach:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JBackGroundImageClass extends JFrame {

    private Image img;
    public JBackGroundImageClass() {
        this.setLayout(new BorderLayout());
        try {
            img = ImageIO.read(new File("image.jpg"));
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        this.add(new JPanel() {

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(img, 0, 0, null);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(img.getWidth(null), img.getHeight(null));
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args) throws IOException {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JBackGroundImageClass().setVisible(true);
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Use `ImageIO`. Don't load an image in `paintComponent()`. Invoke `super.paintComponent()` if overlaying components. See also [Initial Threads](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Nov 04 '11 at 21:53
  • Works, i had to apply explicitly directories such as "/var/tmp/image.jpg" –  Nov 04 '11 at 21:55
0

In the answer by trashgod, there is no reason to do custom painting. The image is painted at its preferred size. Therefore you can just add an ImageIcon to a JLabel and add the label to the frame. You can set the layout manager of the label to be anything you want, the same as you can for a panel.

You would use custom painting is you want to scale the image in which case you would use:

drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) 
camickr
  • 321,443
  • 19
  • 166
  • 288