Java Reference
In-Depth Information
Line2D.Double segment = new Line2D.Double(from, to);
The second option is more object-oriented and is often more useful, particularly if the
point objects can be reused elsewhere in the same drawing.
You often want to put text inside a drawing, for example, to label some of the parts.
Use the drawString method of the Graphics2D class to draw a string anywhere
in a window. You must specify the string and the x- and y-coordinates of the basepoint
of the first character in the string (see Figure 24 ). For example,
The drawString method draws a string, starting at its basepoint.
g2.drawString(ÐMessageÑ, 50, 100);
2.13.1 Colors
When you first start drawing, all shapes and strings are drawn with a black pen. To
change the color, you need to supply an object of type Color . Java uses the RGB
color model. That is, you specify a color by the amounts of the primary colorsȌred,
green, and blueȌthat make up the color. The amounts are given as integers between
0 (primary color not present) and 255 (maximum amount present). For example,
Color magenta = new Color(255, 0, 255);
constructs a Color object with maximum red, no green, and maximum blue, yielding
a bright purple color called magenta.
For your convenience, a variety of colors have been predefined in the Color class.
Table 1 shows those predefined colors and their RGB values. For example,
Color.PINK has been predefined to be the same color as new Color(255,
175, 175) .
When you set a new color in the graphics context, it is used for subsequent
drawing operations.
To draw a rectangle in a different color, first set the color of the Graphics2D
object, then call the draw method:
g2.setColor(Color.RED);
Search WWH ::




Custom Search