Java Reference
In-Depth Information
18
// number of stairs at the given (x, y) position
19
// with the given color
20
public static void drawPyramid(Graphics g, Color c,
21
int x, int y, int stairs) {
22
23
// draws the border rectangle
24
g.drawRect(x, y, SIZE, SIZE);
25
26
// draws the stairs of the pyramid
27
for ( int i = 0; i < stairs; i++) {
28
int stairHeight = SIZE / stairs;
29
int stairWidth = stairHeight * (i + 1);
30
int stairX = x + (SIZE - stairWidth) / 2;
31
int stairY = y + stairHeight * i;
32
33
// fills the rectangles with the fill colors
34
g.setColor(c);
35
g.fillRect(stairX, stairY, stairWidth, stairHeight);
36
37
// draws the black rectangle outlines
38
g.setColor(Color.BLACK);
39
g.drawRect(stairX, stairY, stairWidth, stairHeight);
40
}
41
}
42 }
Chapter Summary
DrawingPanel is a custom class provided by the authors
to easily show a graphical window on the screen. A
DrawingPanel contains a Graphics object that can be
used to draw lines, text, and shapes on the screen using
different colors.
The Graphics object can write text on the screen with its
drawString method. You can specify different font styles
and sizes with the setFont method.
Graphical programs that are decomposed into methods
must pass appropriate parameters to those methods (for
example, the Graphics object, as well as any ( x , y )
coordinates, sizes, or other values that guide the figures to
be drawn).
A Graphics object has many useful methods for drawing
shapes and lines, such as drawLine , fillRect , and
setColor . Shapes can be “drawn” (drawing only the
outline) or “filled” (coloring the entire shape).
 
 
Search WWH ::




Custom Search