JPanel setBackground(Color.BLACK) does nothing

前端 未结 6 1101
不知归路
不知归路 2021-01-04 12:29

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()

相关标签:
6条回答
  • 2021-01-04 12:31

    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
    } 
    
    0 讨论(0)
  • 2021-01-04 12:38

    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

    0 讨论(0)
  • 2021-01-04 12:45
    setOpaque(false); 
    

    CHANGED to

    setOpaque(true);
    
    0 讨论(0)
  • 2021-01-04 12:47

    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());
    }
    
    0 讨论(0)
  • 2021-01-04 12:49

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-04 12:53

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

    0 讨论(0)
提交回复
热议问题