Java Reference
In-Depth Information
}
else {
keyMinor++;
if (keyMinor >= keys [keyMajor].length())
keyMinor = 0;
}
keyTimer = new Timer();
keyTimer.schedule (new KeyConfirmer (this), 500);
}
repaint();
}
Now you need to implement a timer task that confirms the letter if no other key is pressed for half a
second. In that case, the KeyConfirmer class just calls keyConfirmed() in the original Face
class:
import java.util.*;
public class KeyConfirmer extends TimerTask {
Face face;
public KeyConfirmer (Face face) {
this.face = face;
}
public void run() {
face.keyConfirmed();
}
}
Back in the Face class, you can now implement the functionality performed when the letter is finally
confirmed. You just compare the letter to the vitamin needed by the Javagochi . If the right vitamin
is fed, the weight of the Javagochi is increased 10 units by calling transform() :
synchronized void keyConfirmed() {
if (keyMajor != -1) {
if (keys [keyMajor].charAt (keyMinor) == needed) {
javagochi.score += javagochi.getHappiness();
if (!javagochi.isDead())
needed = (char) ('a'
+ ((System.currentTimeMillis() / 10) % 26));
javagochi.transform (10);
}
keyMajor = -1;
repaint();
}
}
Finally, you add some status information about the current score and selected key to the
Face.paint() method. Just insert the following code at the end of the previous implementation of
paint() :
String keySelect = "";
Search WWH ::




Custom Search