Prev Up Next
Go backward to Laying Out the Scrollbars
Go up to Top
Go forward to Making the Colors Vary

Drawing the Color Display

Now, you should write a new method that knows how to draw the display that the scrollbars are supposed to control. This display includes a background drawn in the color determined by the scrollbars and three rectangles painted in red, green and blue each containing the number describing the amount of that color used in the background.

We want you to define a new "private" method containing the Java instructions required to produce this display because there will be two places in the final applet where you will need to instruct the computer to draw or redraw the display. First you will need to tell the machine to draw the display in the "begin" method so that the applet shows more than the scrollbars when any web page containing the applet is first visited. Second, the method that handles the event that a scrollbar has been changed by the user will also need to tell the computer to draw the display to reflect the change. If you don't put the instructions to draw the display in a method of their own, you will need to put one copy of the instructions in the "begin" method and another in the method that handles scroll events. If you create a method to draw the display, you will only need a single line to invoke the display drawing method in each of the "begin" and the event handling method.

You get to choose a name for this method. Something like "drawDisplay" would be apropos.

When it is used, this method will require three pieces of information in order to correctly update the display: the red, green and blue values currently associated with the scrollbars. These values will be provided to your method as parameters just as the x and y coordinates of a point you click on are provided to the "mouseDown" method. You may choose whatever names appeal to you for these parameters, but something like "redness", "greenness", "blueness" (or even "r", "g", and "b" if you don't like typing) would be appropriate.

So, at this point, type in the header for your method. It should look like the header for "begin" except:

It is a good idea to also type a matching pair of braces ("{" and "}") on the two following lines. These braces which will eventually include the method body.

Now, lets fill in the body in three steps.

  • Drawing the Background
  • Adding the Color Value Rectangles
  • Adding the Text

  • Prev Up Next