I have a problem in this block of code that I saw in oracles site.
Can someone explain it for me?
Action updateCursorAction = new AbstractAction() {
boolean shouldDraw = false;
public void actionPerformed(ActionEvent e) {
if (shouldDraw = !shouldDraw) { // <----- here is my problem, what's this condition for?
// isn't it always false?
drawCursor();
} else {
eraseCursor();
}
}
};
new Timer(300, updateCursorAction).start();
That is not doing
if(shouldDraw != shoundDraw). I think that’s what’s confusing you. It is instead doing a negation onshouldDrawand checking to see what the result was.So, the functionality is that if
shouldDrawwas everfalsegoing into that condition, it will be set totrue, and theifblock will execute. IfshouldDrawwent into the condition as true, it will get negated, and the else block will be executed.This will essentially toggle
shouldDrawbetweentrueandfalseon every execution of theActionListenerwhich will make the cursor flash.