Can't set background colour of component
I'm having problems setting the background colour of a component. Despite calling setBackground the background colour is not changing. Here's the code I'm using:
publicclass Mainextends JFrame{
public Main(){
super("Circle Frame");
setSize(640,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BouncyCircle bc =new BouncyCircle();
this.getContentPane().add(bc);
this.setBackground(Color.BLACK);
setVisible(true);
}
publicstaticvoid main(String[] args){
Main m =new Main();
}
}
and the actual panel I'm drawing to:
publicclass BouncyCircleextends JPanel{
Dimension d;
public BouncyCircle(){
super();
d =new Dimension();
this.setBackground(Color.BLACK);
}
publicvoid paintComponent(Graphics g){
int x,y;
Graphics2D g2d = (Graphics2D)g;
this.getSize(d);
x = (d.width)/2;
y = (d.height)/2;
Ellipse2D.Float circle =new Ellipse2D.Float(x,y,50,50);
g2d.setBackground(Color.BLUE);
g2d.setColor(Color.RED);
g2d.draw(circle);
}
}
As you can see from the code - I try to set the background colour in various ways/places - it still stubbornly remains white. One thing I did notice - when I added the setBackground statement to set the background of the Frame - it does actually go black for a fraction of a second before going to white again - presumably when the panel is added/drawn.
Any help greatly appreciated.
Codefire.

