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.

Part one: Pausing the English alphabet

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:

  • Use a 100 millisecond or less delay between each alphabet character.
  • Add a 1 second pause after each user click.
  • Start with animating numbers before the alphabet to establish your main animation loop.
  • Add a pausing... label that shows up where the user clicked.

Animating the characters

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.

Event Flags

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.

Part two: The Amazing Jumping Karel

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:

  • Save the Karel png to the 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."
  • Refresh the folder in Eclipse until you see karel.png visible in the Project Explorer.
  • Karel should move smoothly, so about once every 50 milliseconds (or even more frequently).

Guidelines for jumping:

  • The total jump should last two seconds.
  • While Karel is jumping, she should not be able to restart the jump.
  • Start by having Karel jump vertically up and down, constant velocity.
  • Then, consider adding in gravity. What should your starting velocity be?
  • Finally, add horizontal movement as Karel is jumping.

Here are some images of Karel to help you out.

Part three: The Amazing Anything Karel

Modify your Karel program to do crazy things!!