(When rendered on the off-screen image, these units will be translated to pixels.)
For the initial value of six points, the first point will be located at an angle of zero degreesrelative to the horizontal, and each of the remaining five points will located on the circumference of the circle at an angle that is an even multiple of 360/6or 60 degrees.
To draw or not to draw the points
Recall that a point has no width, no height, and no depth and is therefore not visible to the human eye. However, when you call the draw method on an object of the GM2D04.Point class, a small circle is drawn around the location of the point marking that location for human consumption.
An if statement embedded in the for loop in Listing 3 tests the value of the boolean instance variable named drawPoints (see Listing 1 , which initializes the value to true) to determine whether or not to draw the circle marking the location of each point as itis instantiated and saved in the array object. If true , the circle is drawn as shown in Figure 1 . If false , the circle is not drawn as shown in Figure 3 . As you will see later, the user can modify the value of the variable named drawPoints using one of the checkboxes and the Replot button in Figure 1 .
The default drawing color is BLACK. When the points are drawn the first time the drawOffScreen method is called, they will be drawn in BLACK, which is a public static final variable in the Color class.
To draw or not to draw the lines
Listing 4 tests the value of the instance variable named drawLines to determine whether or not to draw lines connecting each of the points. Thisvariable is also initialized to true in Listing 1 , and its value can be modified by the user later using one of the checkboxes and the Replot button shown in Figure 1 .
Listing 4 . To draw or not to draw the lines. |
---|
GM2D04.Line line;
if(drawLines){//Instantiate and draw lines if true.for(int cnt = 0;cnt<numberPoints-1;cnt++){
//Begin by drawing all of the lines but one.line = new GM2D04.Line(points[cnt],points[cnt+1]);
line.draw(g2D);}//end for loop
//Draw the remaining line required to close the// polygon.
line = new GM2D04.Line(points[numberPoints-1],
points[0]);
line.draw(g2D);}//end if |
Slightly more complicated
Drawing the lines is only slightly more complicated than drawing the points. A for loop is used in Listing 4 to draw lines connecting successive pairs of points whose references were stored in the array named points in Listing 3 . This takes care of all of the required lines but one. Following the for loop, one additional statement is executed to draw a line connecting the points whose references are stored in the first and the last elements in thearray.
(I will show you another way to accomplish this wraparound in the program named StringArt01 later in this module.)
No need to save the GM2D04.Line objects
Note that the GM2D04.Line object that is used to draw each connecting line has no value in this program after the line is drawn on the off-screenimage. Therefore, there is no need to consume memory by saving a large number of such objects. A single reference variable of the class GM2D04.Line is used to refer to all the objects of the GM2D04.Line class that are used to draw the connecting lines. As each new object of that class is instantiated,the object previously referred to by the reference variable becomes eligible for garbage collection .