I want to be able to change the Background with a simple Jbutton. The background and every other component I will add will be displaced on a JPanel. I already figured out how to set a background with Java.awt, although I can't figure out how to repaint the background without getting errors.
public class StartPanel extends JPanel{
private Image virus = "ImagePath";
private Image ecoli = "ImagePath";
private Graphics g;
private boolean img;
public StartPanel(){
this.setLayout(null);
this.add(Button());
img = true;
}
private JButton Button(){
JButton b = new JButton("a");
b.setBounds(230,480,20,20);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
img = !img;
repaint();
}
});
return b;
}
public void paint(Graphics g){
If(img){
g.drawImage(ecoli,0,0,this);
} else {
g.drawImage(virus,0,0,this);
}
}
}