Java Reference
In-Depth Information
TABLE 12-7 Some Constructors and Methods of the class Graphics (continued)
public abstract Font getFont()
//Returns the current font for this graphics context.
public abstract void setFont(Font f)
//Sets the current font for this graphics context to f.
public void String toString()
//Returns a string representation of this graphics context.
Before drawing in Java, let us first explain Java's coordinate system, which is used to
identify every point on the screen. The coordinates of the upper-left corner of a GUI
component, such as the content pane, are (0, 0); this point is called the origin. Every
coordinate pair has an x-coordinate and a y-coordinate. The x-coordinate specifies the
horizontal position, moving from left to right relative to the origin; the y-coordinate
specifies the vertical position, moving from top to bottom relative to the origin.
The x-axis specifies every x-coordinate and the y-axis specifies every y-coordinate.
Figure 12-6 illustrates Java's coordinate system.
x > 0
(0, 0)
x -axis
y > 0
( x , y )
y -axis
FIGURE 12-6 Java's coordinate system
You have already used the drawString method to output a string. Other draw methods
are used in a similar manner. For example, to draw a line from (10, 10) to (10, 40) ,
you use the method drawLine as follows:
g.drawLine(10, 10, 10, 40);
//left line
where g is a Graphics object.
Let us draw three more lines, so that our welcome message is inside a box.
g.drawLine(10, 40, 430, 40); //bottom line
g.drawLine(430, 40, 430, 10); //right line
g.drawLine(430, 10, 10, 10); //top line
Placing these lines in our GrandWelcome applet of Example 12-3 gives us the program
shown in Example 12-4.
Search WWH ::




Custom Search