I need this for implementing the secret maze mini-game and I am unable to set a background image for a JPanel. It has to be, strictly, JPanel. In addition it would be good to be solved via an URL. If you can give me some, not just ideas I would appreciate it, cause I have read some ideas, but they weren't working or at least I didn't succeed in implementing them.
I have the following code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PanelLooks extends JFrame
{
JPanel content = new JPanel();
private JButton NewGameBtn = new JButton("New Game");
private JButton TopTimesBtn = new JButton("Top Times");
private JButton QuitGameBtn = new JButton("Quit Game");
public PanelLooks() {
setPreferredSize(new Dimension(600, 600));
content.setLayout(new FlowLayout());
content.add(NewGameBtn);
content.add(TopTimesBtn);
content.add(QuitGameBtn);
NewGameBtn.setBounds(250, 160, 100, 30);
TopTimesBtn.setBounds(250, 260, 100, 30);
QuitGameBtn.setBounds(250, 360, 100, 30);
this.setContentPane(content);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Secret Maze");
}
The latest version after editing contains an attempt that fails because of a NullPointerException:
private BufferedImage myPicture;
private JLabel picLabel = new JLabel(new ImageIcon(myPicture));
public void backgroundImage()
{
try
{
BufferedImage myPicture = ImageIO.read(new File("D:/Dokumentumok/Egyetem/Object oriented programming/Java project/Project/Background.jpg"));
}
catch(IOException ex)
{
System.err.println("File not found!");
}
}
and this picLabel is called in the constructor as the others were by:
content.add(picLabel);