We shall briefly discuss another project to revisit and practice the concepts discussed in this chapter. The project is named scribble, and you can find it in the Chapter 5 folder of the book projects. This section does not introduce any new concepts, so it consists in large part of exer- cises, with some commentary sprinkled in.
5.12.1 The scribble demo
Thescribble project provides three classes: DrawDemo,Pen, and Canvas (Figure 5.4).
'UDZ'HPR
3HQ
&DQYDV Figure 5.4
Thescribble project
TheCanvas class provides a window on screen that can be used to draw on. It has operations for drawing lines, shapes, and text. A canvas can be used by creating an instance interactively
5.12 Learning about classes from their interfaces | 187
or from another object. The Canvas class should not need any modification. It is probably best to treat it as a library class: open the editor and switch to the documentation view. This displays the class’s interface with the javadoc documentation.
ThePen class provides a pen object that can be used to produce drawings on the canvas by moving the pen across the screen. The pen itself is invisible, but it will draw a line when moved on the canvas.
The DrawDemo class provides a few small examples of how to use a pen object to produce a drawing on screen.
The best starting point for understanding and experimenting with this project is the DrawDemo class.
Exercise 5.50 Create a DrawDemo object and experiment with its various methods. Read theDrawDemo source code and describe (in writing) how each method works.
Exercise 5.51 Create a Pen object interactively using its default constructor (the construc- tor without parameters). Experiment with its methods. While you do this, make sure to have a window open showing you the documentation of the Pen class (either the editor window in Documentation view or a web-browser window showing the project documentation). Refer to the documentation to be certain what each method does.
Exercise 5.52 Interactively create an instance of class Canvas and try some of its meth- ods. Again, refer to the class’s documentation while you do this.
Some of the methods in the classes Pen and Canvas refer to parameters of type Color. This type is defined in class Color in the java.awt package (thus, its fully qualified name is java.
awt.Color). The Color class defines some color constants, which we can refer to as follows:
Color.RED
Using these constants requires the Color class to be imported in the using class.
Exercise 5.53 Find some uses of the color constants in the code of class DrawDemo.
Exercise 5.54 Write down four more color constants that are available in the Color class.
Refer to the class’s documentation to find out what they are.
When calling methods interactively that expect parameters of the Color class, we have to refer to the class slightly differently. Because the interactive dialog has no import statement (and thus theColor class is not automatically known), we have to write the fully qualified class name to refer to the class (Figure 5.5). This enables the Java system to find the class without using an import statement.
Now that we know how to change the color for pens and canvases, we can do some more exercises.
Figure 5.5
Exercise 5.55 Create a canvas. Using the canvas’s methods interactively, draw a red circle near the center of the canvas. Now draw a yellow rectangle.
Exercise 5.56 How do you clear the whole canvas?
As you have seen, we can either draw directly on to the canvas or we can use a pen object to draw. The pen provides us with an abstraction that holds a current position, rotation, and color, and this makes producing some kinds of drawings easier. Let us experiment with this a bit more, this time by writing code in a class instead of using interactive calls.
Figure 5.6 A spiral drawn on the canvas
Exercise 5.57 In class DrawDemo, create a new method named drawTriangle. This method should create a pen (as in the drawSquare method) and then draw a green triangle.
Exercise 5.58 Write a method drawPentagon that draws a pentagon.
Exercise 5.59 Write a method drawPolygon(int n) that draws a regular polygon with n sides (thus, n=3 draws a triangle, n=4 draws a square, etc.).
Exercise 5.60 Write a method called spiral that draws a spiral (see Figure 5.6).
5.12 Learning about classes from their interfaces | 189
5.12.2 Code completion
Often, we are reasonably familiar with a library class that we are using but we still cannot re- member the exact names of all methods or the exact parameters. For this situation, development environments commonly offer some help: code completion.
Code completion is a function that is available in BlueJ’s editor when the cursor is behind the dot of a method call. In this situation, typing CTRL-space will bring up a pop-up listing all methods in the interface of the object we are using in the call (Figure 5.7).
Figure 5.7 Code completion
When the code completion pop-up is displayed, we can type the beginning of the method name to narrow down the method list. Hitting Return enters the selected method call into our source code. Code completion can also be used without a preceding object to call local methods.
Using code completion should not be a replacement for reading the documentation of a class, because it does not include all information (such as the introductory class comment). But once we are reasonably familiar with a class in general, code completion is a great aid for more eas- ily recalling details of a method and entering the call into our source.
Exercise 5.61 Add a method to your DrawDemo class that produces a picture on the can- vas directly (without using a pen object). The picture can show anything you like, but should at least include some shapes, different colors, and text. Use code completion in the process of entering your code.
5.12.3 The bouncing-balls demo
Open the bouncing-balls project and find out what it does. Create a BallDemo object and ex- ecute its bounce method.
Exercise 5.62 Change the method bounce in class BallDemo to let the user choose how many balls should be bouncing.
For this exercise, you should use a collection to store the balls. This way, the method can deal with 1, 3, or 75 balls—any number you want. The balls should initially be placed in a row along the top of the canvas.
Which type of collection should you choose? So far, we have seen an ArrayList, a HashMap, and a HashSet. Try the next exercises first, before you write your implementation.
Exercise 5.63 Which type of collection (ArrayList,HashMap, or HashSet) is most suita- ble for storing the balls for the new bounce method? Discuss in writing, and justify your choice.
Exercise 5.64 Change the bounce method to place the balls randomly anywhere in the top half of the screen.
Exercise 5.65 Write a new method named boxBounce. This method draws a rectan- gle (the “box”) on screen and one or more balls inside the box. For the balls, do not use BouncingBall, but create a new class BoxBall that moves around inside the box, bounc- ing off the walls of the box so that the ball always stays inside. The initial position and speed of the ball should be random. TheboxBounce method should have a parameter that specifies how many balls are in the box.
Exercise 5.66 Give the balls in boxBounce random colors.