Syvered
Syvered

Reputation: 43

Background Image of a window in Java using JFrame

So, I've recently started learning Java, and I am really enjoying it despite the several issues and things that I have not yet understood, it makes me keep going on with it. So, considering that it is my first ever programming language, bear with me on any "noob" mistakes I make.

So, I created a fullscreen window using JFrame:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame {

    public static void main(String[] args) {

        //Window
        JFrame mainWindow = new JFrame("Day One");
        mainWindow.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        mainWindow.setUndecorated(true);
        mainWindow.setVisible(true);
        //End Window

    }

}

And then I tried to add a background image to the window by adding this:

        mainWindow.setLayout(new BorderLayout());
        mainWindow.setContentPane(new JLabel(new ImageIcon("C:\\Users\\Recordings\\Desktop\\Day One\\Images\\Main-Background-Image.png")));
        mainWindow.setLayout(new FlowLayout()); 

I found this code here but it isn't working at all. In this website there are two different ways of making it but I have tried both of them and none works.

I have also searched here, in stackoverflow, for similar questions, but all of them were either unanswered or answered with the same example as mine.

I really hope I have been clear enough, thank you very much for your time

EDIT:

As suggested, I have separated the long single statement:

            mainWindow.setContentPane(new JLabel(new ImageIcon("C:\\Users\\Recordings\\Desktop\\Day One\\Images\\Main-Background-Image.png")));

Into three more easier debugged statements:

        ImageIcon image = new ImageIcon("C:\\Users\\Recordings\\Desktop\\Day One\\Images\\Main-Background-Image.png");
        JLabel label = new JLabel(image);
        mainWindow.setContentPane(label);

Upvotes: 0

Views: 1579

Answers (1)

Kyal Bond
Kyal Bond

Reputation: 296

Just a few tips

  • Its usually best to put a panfel within a frame and then add components to that. Makes for good containment when swing classes get a bit bigger.
  • Its better to create a resource folder for your projects. Create one in the source of your project e.g. where the src and bin folders are located for your project and name it "resources".
  • When creating and image icon its good practice to surround with a try catch so you can give appropriate errors and locate easily, or even handle the error at runtime.

With all that being said, here is your code with a little extra. It creates a panel to hold the jlabel(image) and it adds that panel to the frame. It creates an image icon with a quick method, all you have to do is pass in the file name. This method assumes you have created the folder in your project directory called resources and placed your image in there.

public static void main(String[] args) {
    //Window
    JFrame mainWindow = new JFrame("Day One");
    mainWindow.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    mainWindow.setUndecorated(true);

    //Create image
    JLabel imageHolder = new JLabel();
    imageHolder.setIcon(makeImageIcon("example.png"));

    //Add image to panel, add panel to frame
    JPanel panel = new JPanel();
    panel.add(imageHolder);

    mainWindow.add(panel);

    mainWindow.setVisible(true);
}

//Creates imageicont from filename
public static ImageIcon makeImageIcon(String filename) {
    BufferedImage myPicture = null;
    try {
        myPicture = ImageIO.read(new File("resources/" + filename));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new ImageIcon(myPicture);
}

Hope this helps

Upvotes: 1

Related Questions