Reputation: 531
This snippet of code essentially reveals the back side of the cards when clicked. The showFace
function sets the icon and text and therefore implying the front of the card. Light gray background is the back. If a non matching card is clicked, I first intend to showFace
for a brief moment ( 2 seconds) than revert to the "back side of the card." However, upon clicking a non matching card, the icon and text flashes instantaneously and reverts to its gray background.
Tried changing the 2000 milliseconds to something higher but no avail. Any help is appreciated.
else if (currentMode == 1){
//matched cards
if(checkCards(currentCard, names)){
showFace();
currentMode = 0;
currentCard = "";
deafTo(this);
}
//else non match, still checking mode
else{
showFace();
var timer: Timer = null;
val action = new ActionListener(){
override def actionPerformed(event : ActionEvent){
icon = null;
text = "";
background = Color.DARK_GRAY;
timer.stop();
}
};
timer = new Timer (2000, action);
timer.setInitialDelay(0);
timer.start();
}
}
def showFace()={
text = names;
horizontalTextPosition = Alignment.Center;
verticalTextPosition = Alignment.Bottom;
background = Color.WHITE;
val icons = new ImageIcon(path);
val newIc = icons.getImage();
val newIcons = newIc.getScaledInstance(100, 75,
java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newIcons);
repaint();
}
Upvotes: 0
Views: 83
Reputation: 763
Javadoc http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html
This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method.
Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.
Upvotes: 0
Reputation: 2147
This is because you set an initial delay of 2000ms in the constructor
timer = new Timer(2000, action)
But then you overwrite it to 0ms by:
timer.setInitialDelay(0);
Remove this line and you should be good.
You can check here Swing Timer API.
And see some examples here.
Upvotes: 1