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.

[2595 byte] By [codefirea] at [2007-10-3 2:33:18]
# 1
I may be totally wrong, but I believe you have to call setOpaque(true), or something like that, if I remember right (It's been a while).
masijade.a at 2007-7-14 19:32:18 > top of Java-index,Java Essentials,New To Java...
# 2

Well, it looked hopeful :) I added the call as follows:

public BouncyCircle() {

super();

d = new Dimension();

this.setBackground(Color.BLACK);

this.setOpaque(true);

}

still doesn't change background colour :(

codefirea at 2007-7-14 19:32:18 > top of Java-index,Java Essentials,New To Java...
# 3
I haven't done any Swing programming in a long time, but if I remember correctly you need to call super.paintComponent() for the background to be drawn.
JoachimSauera at 2007-7-14 19:32:18 > top of Java-index,Java Essentials,New To Java...
# 4

Ah, good point, it seems that is necessary! So changing the code:

public void paintComponent(Graphics g){

int x,y;

Graphics2D g2d = (Graphics2D)g;

super.paintComponent(g);

...

Certainly fixes it! Thanks a lot.

codefirea at 2007-7-14 19:32:18 > top of Java-index,Java Essentials,New To Java...
# 5

From the API docu

Further, if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts.

I read this as either call super.paintComponent() or you must draw a filled rectangle of the background color yourself, but that whether it changes the background color or not, also depends on the setOpaque(true).

Reading that it seems as though it is a combination of our suggestions.

masijade.a at 2007-7-14 19:32:18 > top of Java-index,Java Essentials,New To Java...
# 6
Yes, it's a bit confusing. It seems JComponent is not opaque, but JPanel is opaque by default. So I guess when I extend JPanel I either need to handle the background myself (by drawing a filled rectangle) or I need to call JPanel to do it for me.Thanks a lot for your help.
codefirea at 2007-7-14 19:32:18 > top of Java-index,Java Essentials,New To Java...