Java TimerTick event for game loop

后端 未结 2 1162
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 05:25

I tried making a game loop in Java using the Timer from java.util.Timer. I am unable to get my game loop to execute during the timer tick. Here is an example of this issue.

2条回答
  •  情歌与酒
    2020-12-21 05:42

    First of all, Timer.schedule schedules the task for one execution, not for repeated executions. So this program can only make the button move once.

    And you have a second problem : all the interactions with swing components should be done in the event dispatch thread, and not in a background thread. Read http://download.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading for more details. Use a javax.swing.Timer to perform swing actions at repeated intervals.

提交回复
热议问题