Swing custom painting animation stops after reaching half width of frame

后端 未结 1 2023
谎友^
谎友^ 2021-01-26 02:56

I was trying to answer a question related to moving a ball across the screen while changing its color over time, however I came through a weird bug, (most probably in my code) a

相关标签:
1条回答
  • 2021-01-26 03:22

    The issue is that you are inadvertently overriding the getX() method defined in JComponent in your BallPane class.

    As a result the x coordinate of the the JPanel whenever accessed by getX() is also changing as getX() now returns your field x which is defining how the ball moves and thus resulting in this behavior. You should either remove the method getX() from BallPane or rename it.

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