11

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;
    }
}
Primož Kralj
  • 5,534
  • 14
  • 65
  • 125

6 Answers6

16

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

Stefan
  • 11,176
  • 5
  • 40
  • 62
  • "The background color is used only if the component is opaque…"—[`setBackground()`](http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#setBackground%28java.awt.Color%29). – trashgod Apr 14 '12 at 01:58
  • 10
    +1, OP is writing `setOpaque(false)` and still expects the colour to be visible :( – nIcE cOw Apr 14 '12 at 03:29
15

You have to call the super.paintComponent(); as well, to allow the Java API draw the original background. The super refers to the original JPanel code.

public void paintComponent(Graphics g){
    super.paintComponent(g);

    g.setColor(Color.red);
    g.fillOval(player.getxCenter(), player.getyCenter(), player.getRadius(), player.getRadius());
}
Martijn Courteaux
  • 63,780
  • 43
  • 187
  • 279
4

You need to create a new Jpanel object in the Board constructor. for example

public Board(){
    JPanel pane = new JPanel();
    pane.setBackground(Color.ORANGE);// sets the background to orange
} 
Halvor Holsten Strand
  • 18,479
  • 16
  • 70
  • 84
nig
  • 41
  • 1
4
setOpaque(false); 

CHANGED to

setOpaque(true);
demongolem
  • 8,796
  • 36
  • 82
  • 101
Nilesh Jadav
  • 804
  • 9
  • 7
  • maybe you can explain in a few words what's the problem with the code the poster has and then what does your solution. Thank you! – Olimpiu POP Sep 16 '14 at 13:13
3

I just tried a bare-bones implementation and it just works:

public class Test {

    public static void main(String[] args) {
            JFrame frame = new JFrame("Hello");
            frame.setPreferredSize(new Dimension(200, 200));
            frame.add(new Board());
            frame.pack();
            frame.setVisible(true);
    }
}

public class Board extends JPanel {

    private Player player = new Player();

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

    public void paintComponent(Graphics g){  
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillOval(player.getCenter().x, player.getCenter().y,
             player.getRadius(), player.getRadius());
    } 
}

public class Player {

    private Point center = new Point(50, 50);

    public Point getCenter() {
        return center;
    }

    private int radius = 10;

    public int getRadius() {
        return radius;
    }
}
Torious
  • 3,284
  • 14
  • 24
  • I am working in Netbeans so I't hard to do it this way but nevertheless I've managed to manually create my Board() and manually added it to my Frame. I thought I could work with my board using integrated GUI builder but that way I can't change the size of this Board JPanel (not even in code since it's autogenerated and I can't access it). – Primož Kralj Apr 14 '12 at 08:57
0

In order to completely set the background to a given color :

1) set first the background color

2) call method "Clear(0,0,this.getWidth(),this.getHeight())" (width and height of the component paint area)

I think it is the basic procedure to set the background... I've had the same problem.

Another usefull hint : if you want to draw BUT NOT in a specific zone (something like a mask or a "hole"), call the setClip() method of the graphics with the "hole" shape (any shape) and then call the Clear() method (background should previously be set to the "hole" color).

You can make more complicated clip zones by calling method clip() (any times you want) AFTER calling method setClip() to have intersections of clipping shapes.

I didn't find any method for unions or inversions of clip zones, only intersections, too bad...

Hope it helps

bendeg
  • 107
  • 5
  • This answer is very vague, goes off into random topics that don't matter for OP, and step 2 doesn't even compile, or maybe it does, but there's no context given for said method. At least, it doesn't exist as a member method of JPanel or JFrame. Please give more context and be concise in your posts. – kevr May 28 '16 at 02:00