This is a bonus program! It's meant to be a bit challenging, but completing this program will definitely help you write your final project.
Write GraphicsPrograms that do some animation after the user clicks.
BonusEvents: ClickNPause.java
In ClickNPause.java
, write a GraphicsProgram that loops through the English alphabet (from A to Z). Also add functionality so that when the user clicks somewhere on the screen, a "pausing..." label shows up where the user clicked.
General guidelines:
pausing...
label that shows up where the user clicked.
Since we are animating letters and not integers, we should keep track of a char
class variable.
A char
is a single character long (so it's different from String, which can be multiple characters). To set a GLabel's text to a single character:
char c = 'c';
label.setLabel(c + "");
The benefit of using a char
over a String is that each character has a unique numeric representation representation, using something we call ASCII. So if we converted the character A
to an integer:
System.out.println((int) 'A'); // returns 65
Likewise, the numeric representation of Z
is 65 + 26 - 1 = 90
. The char
to int
mapping is decided by the ASCII Codes.
Use this insight to devise a method for looping through the letters A through Z, then loop back to A again.
Add a mouse listener so that when a user clicks, the counter temporarily stops incrementing, and "pausing..." sign shows up at the location that the user clicked. After one second, the "pausing..." sign should go away and the counter should continue incrementing.
It will be difficult to implement this all in mouseClicked()
!!! You cannot pause inside a listener method like mouseClicked()
or mouseMoved()
, because these methods are designed to act quickly. Instead, you need to use Event Flags.
A flag is a boolean variable that the programmer sets to true
when an event happens, and is set back to false
when the event ends. In practice, a programmer sets event flags to true in mouse listeners, checks if they are true in the main animation loop, and then does some action after it sees the flag is turned on. Here is an example of a main animation loop that also checks a flag:
private boolean eventFlag = false;
public void run() {
while(true) {
if (eventFlag) {
doSomethingSpecial();
eventFlag = false; // reset the flag once you're done
}
doSomethingNormal();
pause(DELAY);
}
}
public void mouseMoved(MouseEvent e) {
eventFlag = true; // turn on the flag
}
With this newfound knowledge, finish up part one.
BonusEvents: ClickNJump.java
In ClickNJump.java
, implement a program where Karel (below) moves side to side forever. When the user clicks, Karel jumps high, lands, and continues moving side to side.
General guidelines:
src
folder where your java files are located. You can find your project location by going to Eclipse, right-clicking on the project folder, and selecting "Properties."karel.png
visible in the Project Explorer.Guidelines for jumping:
Here are some images of Karel to help you out.
Modify your Karel program to do crazy things!!