Search code examples
javaimageswingembedded-resource

Add a background to a JFrame


I want to add a background to my JFrame, but I can't get it done. I have searched on the internet to find to some tutorials on it. I am still a newbie and I want to learn these things. This is what I have so far.

package gui;

import java.awt.Desktop;

public class Gui extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JLabel BackgroundLabel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Gui frame = new Gui();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Gui() {
        setTitle("Exile Launcher");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1000, 563);
        contentPane = new JPanel();
        contentPane.setBorder(null);
        setContentPane(contentPane);
        contentPane.setLayout(null);

        BufferedImage BackgroundImage = null;
        try {
            BackgroundImage = ImageIO.read(this.getClass().getResource("/Images/Background.jpg"));
        } catch (IOException ex) {
        }

        JLabel BackgroundLabel = new JLabel(new ImageIcon(BackgroundImage));

        add(BackgroundLabel);

        JButton HomeButton = new JButton("Home");
        HomeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Desktop.getDesktop().browse(new URL("http://www.google.nl").toURI());
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (URISyntaxException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        HomeButton.setBounds(10, 9, 50, 50);
        contentPane.add(HomeButton);

        JButton ForumButton = new JButton("Vote");
        ForumButton.setBounds(10, 70, 50, 50);
        contentPane.add(ForumButton);

        JButton VoteButton = new JButton("New button");
        VoteButton.setBounds(10, 131, 50, 50);
        contentPane.add(VoteButton);
    }
}

But I get this error:

java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1388)
    at gui.Gui.<init>(Gui.java:59)
    at gui.Gui$1.run(Gui.java:36)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Solution

  • UPDATE

    I've got it running. First off your using null layout but not specifying the aspects you need to be focusing on. You don't actually create your background image when you just do "add(backgroundLabel);" you need to run backgroundLabel.setBounds(new Rectangle(int x, int y, width, hight); I'm going to give you the entire code. Make sure you have the file path reset to what you need.

    Just drop it in, set your path and it should run. You will need to make sure the class name matches whatever your expecting to have but here you go:

    import java.awt.Desktop;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URISyntaxException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Gui extends JFrame {
    
    private static final long serialVersionUID = 1L;
    private final JPanel contentPane;
    private JLabel BackgroundLabel;
    Image background;
    
    
    /**
     * Launch the application.
     */
    public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final Gui frame = new Gui();
                    frame.setVisible(true);
                } catch (final Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    
    /**
     * Create the frame.
     */
    public Gui() {
        this.setTitle("Exile Launcher");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 1000, 563);
        this.contentPane = new JPanel();
        this.contentPane.setBorder(null);
        this.setContentPane(this.contentPane);
        this.contentPane.setLayout(null);
    
        try {
            this.background = ImageIO.read(new File("src/Images/Background.jpg"));
        } catch (final IOException e) {
            e.printStackTrace();
        }
    
        final JLabel backgroundLabel = new JLabel(new ImageIcon(this.background));
        backgroundLabel.setBounds(new Rectangle(0, 0, 1000, 563));
        this.add(backgroundLabel);
    
        final JButton HomeButton = new JButton("Home");
        HomeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
                try {
                    Desktop.getDesktop().browse(new URL("http://www.google.nl").toURI());
                } catch (final MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (final IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (final URISyntaxException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
    
        HomeButton.setBounds(10, 9, 50, 50);
        this.contentPane.add(HomeButton);
    
        final JButton ForumButton = new JButton("Vote");
        ForumButton.setBounds(10, 70, 50, 50);
        this.contentPane.add(ForumButton);
    
        final JButton VoteButton = new JButton("New button");
        VoteButton.setBounds(10, 131, 50, 50);
        this.contentPane.add(VoteButton);
    }
    
    
    // Final Piece to add an image to the jpanel
    public void paintComponent(final Graphics pic) {
        this.paintComponent(pic);
        pic.drawImage(this.background, 0, 0, null);
    }
    }