Java Reference
In-Depth Information
Continued from previous page
However, the fill command covers the same pixels as the draw command, and
the green interior will be drawn over the black outline, leading to the following
appearance:
Instead, the code should fill the interior of the rectangle first, then draw
the black outline, to make sure that the outline shows on top of the filling. The
following is the correct code and its output:
g.setColor(Color.GREEN); // corrected code
g.fillRect(20, 50, 60, 30);
g.setColor(Color.BLACK);
g.drawRect(20, 50, 60, 30);
Drawing with Loops
In each of the preceding examples we used simple constants for the drawing and fill-
ing commands, but it is possible to use expressions. For example, suppose that we
stick with our DrawingPanel size of 200 pixels wide and 100 pixels tall and we want
to produce a diagonal series of four rectangles that extend from the upper-left corner
to the lower-right corner, each with a white oval inside. In other words, we want to
produce the output shown in Figure 3G.7.
The overall width of 200 and overall height of 100 are divided evenly into four
rectangles, which means that they must all be 50 pixels wide and 25 pixels high. So,
width and height values for the four rectangles are the same, but the positions of their
 
Search WWH ::




Custom Search