guarda il codice sembra giusto... cmq fregatene.. fai così che va sicuro e senza problemi
copia questo codice così com'è
codice:
// CLASSE INTERNA PER LO SFONDO
class ContentPanel extends JPanel{
Image bgimage = null;
ContentPanel(String imagename){
if(imagename != null){
MediaTracker mt = new MediaTracker(this);
bgimage = Toolkit.getDefaultToolkit().getImage(imagename);
mt.addImage(bgimage, 0);
try{
mt.waitForAll();
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(bgimage != null){
int imwidth = bgimage.getWidth(null);
int imheight = bgimage.getHeight(null);
if((imwidth > 0) && (imheight > 0)){
for(int y = 0; y<getHeight(); y+=imheight){
for(int x = 0; x<getWidth(); x+=imwidth){
g.drawImage(bgimage, x, y, null);
}
}
}
}
}
}
e con questo lo richiami da dove vuoi...
tuoPanel.setContentPane(new ContentPanel("img/sfondo.jpg"));