Java Reference
In-Depth Information
Table 3G.2
Some Useful Methods of Graphics Objects
Method
Description
draws a line between the points ( x 1, y 1) and ( x 2, y 2)
drawLine(x1, y1, x2, y2)
draws the outline of the largest oval that fits within the
specified rectangle
drawOval(x, y, width, height)
draws the outline of the specified rectangle
drawRect(x, y, width, height)
draws the given text with its lower-left corner at ( x , y )
drawString(message, x, y)
fills the largest oval that fits within the specified
rectangle using the current color
fillOval(x, y, width, height)
fills the specified rectangle using the current color
fillRect(x, y, width, height)
sets this graphics context's current color to the
specified color (all subsequent graphics operations
using this graphics context use this specified color)
setColor(color)
sets this graphics context's current font to the specified
font (all subsequent strings drawn using this graphics
context use this specified font)
setFont(font)
5 public class DrawLine1 {
6
public static void main(String[] args) {
7
// create the drawing panel
8
DrawingPanel panel = new DrawingPanel(200, 100);
9
10
// draw a line on the panel using
11
// the Graphics paintbrush
12
Graphics g = panel.getGraphics();
13
g.drawLine(25, 75, 175, 25);
14
}
15 }
When you run this program, the window shown in Figure 3G.2 appears. Though it
isn't text on the console, as in previous chapters, we'll still refer to this as the “output”
of the program.
(Java can be run on a variety of systems. Depending on your operating system,
your output may differ slightly from the screenshots in this chapter.)
(25, 75)
(175, 25)
Figure 3G.2
Output of DrawLine1
 
Search WWH ::




Custom Search