Step 6


Overview

Great news: this is an easy step. All you need to do is add a changeTitle() method into the DrawPanel class and add a statement at the end of the updateDrawing() method that invokes changeTitle(). In fact, you will be given all but two lines of code in the changeTitle() method, along with an explanation of what it is doing.

The changeTitle() Method

Here is most of the code for this method.
    void changeTitle() {
	String comma = ", ";
	String quote = "\"";
	String message = "g." + method + "(";
	if (method.endsWith("String")) {
	    message += quote + string + quote + comma;
	}
	border.setTitle(message);
    }
The first two statements just declare named variables for some frequently used portions of the text that appears in the title around the draw panel. The message variable is used in the last statement to set the border title. Its initialization expression uses the + operator for adding three strings end-to-end. The first and last are string literals, the middle is a string instance variable. If method currently has the value "drawString" then so far, message has the value "g.drawString(". The += means add the right hand side to the variable on the left.

Later statements append more characters onto the end of message. We want to append each of the drawing parameters, separated by commas, followed by a close parenthesis and a semicolon. The drawing parameters that we need depend on the drawing method, so we need some if statements that test the method variable to see if a drawing parameter is needed. For the drawString() method, the first parameter is the draw panel instance variable string. This is the only drawing method whose name ends with "String" and it is the only one whose first parameter is string. If method currently has the value "drawString" and string has the value "Hit return after changes." then after the if statement, message has the value "g.drawString("Hit return after changes."". Here, the two inner quotes are parts of the actual string text.

Of course, this is not exactly what we want. We also need to append the x and y drawing parameters with separating commas, and a close parenthesis and semicolon. You should use one statement for x and y and the commas, and a separate statement for the close parenthesis and semicolon. All of the rest of the drawing methods have x and y as their first two parameters. Other parameters will be handled later by adding statements just before the one for the parenthesis and semicolon.

Invoking changeTitle() in updateDrawing()

Just before the repaint() message at the end of updateDrawing, you should invoke the changeTitle() method to ensure that the title changes when the drawing is updated.

After recompiling the DrawPanel class, your applet should look like the following demonstration applet. When you change the string in the text area or adjust the sliders, the title for the draw panel should also change.

Demonstration Applet