Background image in Applet or JFrame
Its a little tricky, add this code (actually a small class) to the bottom or your class (or as a new class):
class ImagePanel extends JComponent {
private Image image;
public ImagePanel(String fileName) {
try
{
image=javax.imageio.ImageIO.read(new java.io.File(fileName));
}
catch (Exception e){}
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.green);
g.fillRect(50,50,5,50);
g.drawImage(image, 0, 0, null);
}
}
In your init, before Container screen=getContentPane(); put in:
setContentPane(new ImagePanel("imageName.GIF"));
|