Java Reference
In-Depth Information
9
// draw a triangle on the panel
10
Graphics g = panel.getGraphics();
11
g.drawLine(25, 75, 100, 25);
12
g.drawLine(100, 25, 175, 75);
13
g.drawLine(25, 75, 175, 75);
14
}
15 }
This program draws three different lines to form a triangle, as shown in Figure
3G.3. The lines are drawn between three different points. In the lower-left corner we
have the point (25, 75). In the middle at the top we have the point (100, 25). And in
the lower-right corner we have the point (175, 75). The various calls on drawLine
simply draw the lines that connect these three points.
Figure 3G.3
Output of DrawLine2
The Graphics object also has methods for drawing particular shapes. For exam-
ple, you can draw rectangles with the drawRect method:
g.drawRect(<x>, <y>, <width>, <height>);
This draws a rectangle with upper-left coordinates ( x , y ) and the given height and
width.
Another figure you'll often want to draw is a circle, or, more generally, an oval.
But how do you specify where it appears and how big it is? What you actually spec-
ify is what is known as the “bounding rectangle” of the circle or oval. Java will draw
the largest oval possible that fits inside that rectangle. So, the method:
g.drawOval(<x>, <y>, <width>, <height>);
draws the largest oval that fits within the rectangle with upper-left coordinates ( x , y )
and the given height and width.
 
Search WWH ::




Custom Search