JPanel image won't show
I have one class that extends JFrame. The code is as below...
import java.awt.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import javax.swing.*;
publicclass NewGameextends JFrame
{
private JPanel gamearea =new JPanel();
private Beer beer=new Beer();
private Container c;
static JFrame window =new NewGame();
public NewGame()
{
gamearea.add(beer);
c = getContentPane();
c.setLayout(new BorderLayout());
c.add(gamearea, BorderLayout.WEST);
c.setSize(480, 480);
//... Set window characteristics
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Game");
pack();
setVisible(true);
}
publicstaticvoid main(String[] args)
{
window.setVisible(true);
}
}
And another class that extends the JPanel...
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
publicclass Beerextends JPanelimplements KeyListener
{
Image bg = Toolkit.getDefaultToolkit().getImage("res/pub.jpg");
public Beer()
{
this.setPreferredSize(new Dimension(480, 480));
this.addKeyListener(this);// This class has its own key listeners.
this.setFocusable(true);// Allow panel to get focus
}
publicvoid paint(Graphics g)
{
g.drawImage(bg,0,0,null);
}
publicvoid keyTyped(KeyEvent e){}
publicvoid keyPressed(KeyEvent e)
{
System.out.println("ok");
}
publicvoid keyReleased(KeyEvent e)
{
}
}
Both this classes compiles fine. But when execute, the image in the JPanel class wont show. I have to minimize the JFrame den maximize it to show the image. why is this strange thing is happening?

