Java Reference
In-Depth Information
A Larger Example: DrawDiamonds
Now let's consider a slightly more complicated task: drawing the largest diamond
figure that will fit into a box of a particular size. The largest diamond that can fit into
a box of size 50
×
50 is shown in Figure 3G.12.
(0, 0)
(25, 0)
(0, 25)
(50, 25)
(25, 50)
Figure 3G.12
Diamond
The code to draw such a diamond would be the following:
g.drawRect(0, 0, 50, 50);
g.drawLine(0, 25, 25, 0);
g.drawLine(25, 0, 50, 25);
g.drawLine(50, 25, 25, 50);
g.drawLine(25, 50, 0, 25);
Now imagine that we wish to draw three such 50
50 diamonds at different loca-
tions. We can turn our diamond-drawing code into a drawDiamond method that we'll
call three times. Since each diamond will be in a different position, we can pass the
x - and y -coordinates as parameters to our drawDiamond method.
A diamond enclosed by a box with top-left corner at the location (78, 22) is shown
in Figure 3G.13.
×
(78, 22)
(103, 22)
(78, 47)
(128, 47)
(103, 72)
Figure 3G.13
Diamond at (78, 22)
 
Search WWH ::




Custom Search