Keeping draw graphics - removing super.paintComponent

前端 未结 2 1133
眼角桃花
眼角桃花 2021-01-22 02:56

I have a class named Foo that extends a class named Bar that extends JPanel and implements ActionListener. When I select Circle and click the draw button, I draw a circle, and w

2条回答
  •  失恋的感觉
    2021-01-22 03:20

    Suggestions:

    • Don't remove super.paintComponent(g) as it has a necessary important role to play.
    • Instead why not draw to a BufferedImage and then draw that BufferedImage in the paintComponent(...) method override.
    • Then if you want to erase drawn images, simply create a new BufferedImage, or draw over it.

    As an aside, don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

    if (fu == "bar") {
      // do something
    }
    

    do,

    if ("bar".equals(fu)) {
      // do something
    }
    

    or,

    if ("bar".equalsIgnoreCase(fu)) {
      // do something
    }
    

提交回复
热议问题