I'm trying to paint a background image onto a JFrame for a life simulation game I'm making. This is my code:
public class MainFrame extends JFrame {
//creates image variables
Image background;
public MainFrame(int w, int h) {
//creates new JFrame and sets some other properties
super("Life Simulation");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(w,h));
//creates images
background = Toolkit.getDefaultToolkit().createImage("img/default.jpg");
this.repaint();
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g.drawImage(background,0,0,null);
}
}
I've tried to repaint it before setting it visible, but nothing. When I launch my program from my main method, the JFrame is simply blank. However, if I resize it in the slightest, the paint method is called and the background image is painted.
This is my main method:
public class Main {
public static void main(String[] args) {
MainFrame frame = new MainFrame(1080,720);
frame.repaint(); //tried invoking paint() here as well but again to no avail
}
}
EDIT: I believe it is also worth mentioning that I have little to no experience beforehand with using paint() or any of its variants, only knowledge of how it SHOULD be implemented and its abilities.