Java Reference
In-Depth Information
13.1 Introduction
13.2 Graphics Contexts and Graphics
Objects
13.3 Color Control
13.4 Manipulating Fonts
13.5 Drawing Lines, Rectangles and Ovals
13.6 Drawing Arcs
13.7 Drawing Polygons and Polylines
13.8 Java 2D API
13.9 Wrap-Up
Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises |
Making a Difference
13.1 Introduction
In this chapter, we overview several of Java's capabilities for drawing two-dimensional
shapes, controlling colors and controlling fonts. Part of Java's initial appeal was its support
for graphics that enabled programmers to visually enhance their applications. Java contains
more sophisticated drawing capabilities as part of the Java 2D API (presented in this chapter)
and its successor technology JavaFX (presented in Chapter 25 and two online chapters). This
chapter begins by introducing many of Java's original drawing capabilities. Next we present
several of the more powerful Java 2D capabilities, such as controlling the style of lines used
to draw shapes and the way shapes are filled with colors and patterns . The classes that were
part of Java's original graphics capabilities are now considered to be part of the Java 2D API.
Figure 13.1 shows a portion of the class hierarchy that includes various graphics
classes and Java 2D API classes and interfaces covered in this chapter. Class Color contains
methods and constants for manipulating colors. Class JComponent contains method
paintComponent , which is used to draw graphics on a component. Class Font contains
methods and constants for manipulating fonts. Class FontMetrics contains methods for
obtaining font information. Class Graphics contains methods for drawing strings, lines,
rectangles and other shapes. Class Graphics2D , which extends class Graphics , is used for
drawing with the Java 2D API. Class Polygon contains methods for creating polygons . The
bottom half of the figure lists several classes and interfaces from the Java 2D API. Class
BasicStroke helps specify the drawing characteristics of lines . Classes GradientPaint and
TexturePaint help specify the characteristics for filling shapes with colors or patterns .
Classes GeneralPath , Line2D , Arc2D , Ellipse2D , Rectangle2D and RoundRectangle2D
represent several Java 2D shapes.
To begin drawing in Java, we must first understand Java's coordinate system
(Fig. 13.2), which is a scheme for identifying every point on the screen. By default, the
upper-left corner of a GUI component (e.g., a window) has the coordinates (0, 0). A coor-
dinate pair is composed of an x -coordinate (the horizontal coordinate ) and a y -coordinate
(the vertical coordinate ). The x -coordinate is the horizontal distance moving right from
the left edge of the screen. The y -coordinate is the vertical distance moving down from the
top of the screen. The x -axis describes every horizontal coordinate, and the y -axis every ver-
tical coordinate. The coordinates are used to indicate where graphics should be displayed
on a screen. Coordinate units are measured in pixels (which stands for “picture elements”).
A pixel is a display monitor's smallest unit of resolution .
 
 
Search WWH ::




Custom Search