JPanel Graphics clearing and repainting?

前端 未结 4 593
情书的邮戳
情书的邮戳 2020-12-16 20:25

I have a JPanel with a paintComponent() function. I\'ll call it once, then when the user clicks a different JButton, I\'ll set some fl

相关标签:
4条回答
  • 2020-12-16 20:27

    When you change a property of the panel then you need to invoke:

    panel.repaint();
    

    to cause the component to be repainted.

    Then the first statement in the paintComponent() method should be:

    super.paintComponent(g);
    

    This will paint the background so you can now do your custom painting.

    If you need more help then post your SSCCE that demonstrates the problem.

    0 讨论(0)
  • 2020-12-16 20:35

    I would suggest calling revalidate(); instead of repaint(). revalidate() needs to be called when changing the size / layout or when you add/remove objects onto your jpanel and will update all of it's children. From what I can tell, you're still using the same paint object tho but changing it's layout.

    0 讨论(0)
  • 2020-12-16 20:36

    To clear all previously drawn graphics, invoke g.clearRect(0, 0, getWidth(), getHeight()).

    0 讨论(0)
  • 2020-12-16 20:45

    First, why not use an enum instead of a boolean?

    enum Enum { 
        RECTANGLE,
        LINE,
        CIRCLE
    }
    
    Enum choice = RECTANGLE; //default to RECTANGLE
    
    switch(choice) { 
       // case RECTANGLE, LINE, CIRCLE
    }
    

    With regards to your issue, can you answer my comments in your question?

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