Java Reference
In-Depth Information
30 uses GeneralPath method lineTo to draw a line to the next point in the star . Each
new call to lineTo draws a line from the previous point to the current point. Line 32 uses
GeneralPath method closePath to draw a line from the last point to the point specified
in the last call to moveTo . This completes the general path.
Line 34 uses Graphics2D method translate to move the drawing origin to location
(150, 150). All drawing operations now use location (150, 150) as (0, 0).
The for statement in lines 37-46 draws the star 20 times by rotating it around the
new origin point. Line 39 uses Graphics2D method rotate to rotate the next displayed
shape. The argument specifies the rotation angle in radians (with 360° = 2 π radians). Line
45 uses Graphics2D method fill to draw a filled version of the star .
13.9 Wrap-Up
In this chapter, you learned how to use Java's graphics capabilities to produce colorful
drawings. You learned how to specify the location of an object using Java's coordinate sys-
tem, and how to draw on a window using the paintComponent method. You were intro-
duced to class Color , and you learned how to use this class to specify different colors using
their RGB components. You used the JColorChooser dialog to allow users to select colors
in a program. You then learned how to work with fonts when drawing text on a window.
You learned how to create a Font object from a font name, style and size, as well as how
to access the metrics of a font. From there, you learned how to draw various shapes on a
window, such as rectangles (regular, rounded and 3D), ovals and polygons, as well as lines
and arcs. You then used the Java 2D API to create more complex shapes and to fill them
with gradients or patterns. The chapter concluded with a discussion of general paths, used
to construct shapes from straight lines and complex curves. In the next chapter, we discuss
class String and its methods. We introduce regular expressions for pattern matching in
strings and demonstrate how to validate user input with regular expressions.
Summary
Section 13.1 Introduction
• Java's coordinate system (p. 556) is a scheme for identifying every point (p. 567) on the screen.
• A coordinate pair (p. 556) has an x -coordinate (horizontal) and a y -coordinate (vertical).
• Coordinates are used to indicate where graphics should be displayed on a screen.
• Coordinate units are measured in pixels (p. 556). A pixel is a display monitor's smallest unit of
resolution.
Section 13.2 Graphics Contexts and Graphics Objects
• A Java graphics context (p. 558) enables drawing on the screen.
• Class Graphics (p. 558) contains methods for drawing strings, lines, rectangles and other shapes.
Methods are also included for font manipulation and color manipulation.
•A Graphics object manages a graphics context and draws pixels on the screen that represent text
and other graphical objects, e.g., lines, ellipses, rectangles and other polygons (p. 558).
• Class Graphics is an abstract class. Each Java implementation has a Graphics subclass that pro-
vides drawing capabilities. This implementation is hidden from us by class Graphics , which sup-
plies the interface that enables us to use graphics in a platform-independent manner.
 
 
Search WWH ::




Custom Search