JPanel setBackground(Color.BLACK) does nothing

Primož Kralj picture Primož Kralj · Apr 13, 2012 · Viewed 107.1k times · Source

I have the folowing custom JPanel and I have aded it to my frame using Netbeans GUI builder but the background won't change! I can see the circle, drawing with g.fillOval(). What's wrong?

public class Board extends JPanel{

    private Player player;

    public Board(){
        setOpaque(false);
        setBackground(Color.BLACK);  
    }

    public void paintComponent(Graphics g){  
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillOval(player.getxCenter(), player.getyCenter(), player.getRadius(), player.getRadius());
    }

    public void updatePlayer(Player player){
        this.player=player;
    }
}

Answer

Stefan picture Stefan · Apr 14, 2012

If your panel is 'not opaque' (transparent) you wont see your background color.