I want to read a jpg image from a file to an Image object and then paint it on my JPanel, but the image doesn't appear.I've tried drawing some other stuff on the panel (like lines etc.) and everything appeared without any problems.
My code:
This class represents my JPanel:
public class Scene extends JPanel {
private Image backgroundImage=null;
public Scene(){
super();
setDoubleBuffered(true);
try {
backgroundImage = ImageIO.read(new File("bg.jpg"));
} catch (IOException ex) {
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(backgroundImage, 0, 0, 400, 600, this);
}
}
This is my JFrame, on which i put my JPanel.
public class Window extends JFrame{
private Scene scene;
public Window(){
super();
setSize(400,600);
setLocation(300,100);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setResizable(false);
scene=new Scene();
add(scene,BorderLayout.CENTER);
requestFocus();
}
Any ideas?