to change background image you should have
*array with image names on ImageTest class
*setImg() method on ImagePanel class
* I'm using ImageHelper.class to load Images
*counter to go to next index of array
*repaint() method calls paintComponent() method
frame.addKeyListener(new KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode()==KeyEvent.VK_SPACE)
panel.setImg(ImageHelper.loadImage(array[counter]).getImage());
panel.repaint();
counter++;
}
});
ImageHelper.class:
import java.net.*;
import java.awt.*;
import javax.swing.*;
public class ImageHelper {
private ImageHelper() {
}
public static ImageIcon loadImage(String name) {
ImageIcon image = null;
try {
URL url = ImageHelper.class.getResource(name);
if (url != null) {
java.awt.Image img = Toolkit.getDefaultToolkit().createImage(url);
if (img != null) {
image = new ImageIcon(img);
}
}
} catch (Throwable ex) {
System.out.println("ERROR: loading image " + name + " failed");
}
return image;
}
}