I am working on a Tetris project and I am finding that I need to use both javax.swing.Timer and java.util.Timer. But one of them is not working. Can anyone provide any insight into this problem?
-
yes you can, what was it that did not work?Vegard– Vegard2013-06-11 18:30:29 +00:00Commented Jun 11, 2013 at 18:30
-
If you want to use them in the same file, you are probably doing something wrong.Tom Hawtin - tackline– Tom Hawtin - tackline2013-06-11 19:39:39 +00:00Commented Jun 11, 2013 at 19:39
4 Answers
Yes you can use the 2 classes together but you have to enter the full package like this
java.util.Timer myUtilTimer= ..
javax.swing.Timer myUtilTimer= ..
Besides you have to take care that output from java.util.Timer must be wrapped into SwingUtilities.invokeLater(myRunnable); to add to the EDT otherwise is no guaranted that anything should be diplayed/changed/repainted
2 Comments
In this concrete example, "A javax.swing.Timer increments an int at
100 Hz, while a java.util.TimerTask samples the value at 1 Hz." A LinkedBlockingQueue<Integer> synchronizes access to the shared data and records the last N samples. The example illustrates features of all three preceding answers:
Use of a fully qualified name.
Use of
invokeLater().