Java Reference
In-Depth Information
(a)
(b)
figure 7.14
(a) A fractal star outline drawn by the code shown in Figure 7.15; (b) The same star immediately before the last square is added.
As with the previous example, the method
drawFractal
uses a Java library
routine. In this case,
fillRect
draws a rectangle; its upper left-hand corner
and dimensions must be specified. The code is shown in Figure 7.15. The
figure 7.15
Code for drawing the
fractal star outline
shown in Figure 7.14
1
// Draw picture in Figure 7.14.
2
void drawFractal( Graphics g, int xCenter,
3
int yCenter, int boundingDim )
4
{
5
int side = boundingDim / 2;
6
7
if( side < 1 )
8
return;
9
10
// Compute corners.
11
int left = xCenter - side / 2;
12
int top = yCenter - side / 2;
13
int right = xCenter + side / 2;
14
int bottom = yCenter + side / 2;
15
16
// Recursively draw four quadrants.
17
drawFractal( g, left, top, boundingDim / 2 );
18
drawFractal( g, left, bottom, boundingDim / 2 );
19
drawFractal( g, right, top, boundingDim / 2 );
20
drawFractal( g, right, bottom, boundingDim / 2 );
21
22
// Draw central square, overlapping quadrants.
23
g.fillRect( left, top, right - left, bottom - top );
24
}
Search WWH ::
Custom Search