CSC 112
Ileana Streinu

5. Paint Text

  1. Modify the applet (Java code) so that it paints the string "Hello, Alice" (replace Alice with your name) on the applet window, on a white background, AND prints the same string on the Java console. This should be done each time the applet window is redrawn.

  2. Play a little bit with it, keeping an eye on the Java console and see what happens. For instance, put another window (e.g. the terminal window) on top of the browser, then go back to your applet page: how did you realize that the window was redrawn when you did that? (Hint: a new message was printed on the Java console by the paint method). Minimize the browser, then maximize it: was the window redrawn?

  3. Now modify the applet to include the functionality of the previous applet (Button with Action), i.e. to have one button on it, not just the text. When the button is pressed, a message is printed on the Java console.

  4. Tricky exercise: make sure you get to do it in class, if not ask for hints/advice on Tuesday.

    Modify the applet so that it counts how many times the button was clicked, and draws that number on the applet after each click. This is a bit tricky, and involves something you have not seen before, namely the repaint method. Here's how you do it: in actionPerformed, when you find out that the button was clicked, increment the counter and then call repaint(). Just like this. In paint, the message printed on the applet should be the value of the counter.