Difference between paint() and paintcomponent()?

后端 未结 2 1743
刺人心
刺人心 2020-11-27 21:33

I have tried tutorials on this but I still don\'t quite understand it. Basically my question is which method is better and why? Should I use paint or pain

2条回答
  •  半阙折子戏
    2020-11-27 22:10

    Generally speaking, when painting in Swing, it is recommended to override paintComponent.

    There are a number of reasons why, one is paintComponent is painted at the bottom layer, meaning you won't accidentally wiping out any components that were rendered during the paint process - this happens a lot to people who post here.

    There are a, very, few times you might need to override paint, but I would always encourage you to try making it work with paintComponent first.

    Check out

    • Performing custom painting
    • Painting in AWT and Swing (+1 to trashgod)

提交回复
热议问题