这章内容不熟,希望能够通过做练习加深理解!
Summary Section 13.1 Introduction • Java’s coordinate system is a scheme for identifying every point on the screen. • A coordinate pair 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. A pixel is a display monitor’s smallest unit of resolution. Section 13.2 Graphics Contexts and Graphics Objects • A Java graphics context enables drawing on the screen. • Class Graphics 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. • Class Graphics is an abstract class. Each Java implementation has a Graphics subclass that provides drawing capabilities. This implementation is hidden from us by class Graphics, which supplies the interface that enables us to use graphics in a platform-independent manner. • Method paintComponent can be used to draw graphics in any JComponent component. • Method paintComponent receives a Graphics object that is passed to the method by the system when a lightweight Swing component needs to be repainted. • When an application executes, the application container calls method paintComponent. For paintComponent to be called again, an event must occur. • When a JComponent is displayed, its paintComponent method is called. • Calling method repaint on a component updates the graphics drawn on that component. Section 13.3 Color Control • Class Color declares methods and constants for manipulating colors in a Java program. • Every color is created from a red, a green and a blue component. Together these components are called RGB values. The RGB components specify the amount of red, green and blue in a color, respectively. The larger the value, the greater the amount of that particular color. • Color methods getRed, getGreen and getBlue return int values from 0 to 255 representing the amount of red, green and blue, respectively. • Graphics method getColor returns a Color object with the current drawing color. • Graphics method setColor sets the current drawing color. • Graphics method fillRect draws a rectangle filled by the Graphics object’s current color. • Graphics method drawString draws a String in the current color. • The JColorChooser GUI component enables application users to select colors. • JColorChooser static method showDialog displays a modal JColorChooser dialog. Section 13.4 Manipulating Fonts • Class Font contains methods and constants for manipulating fonts. • Class Font’s constructor takes three arguments—the font name , font style and font size. • A Font’s font style can be Font.PLAIN, Font.ITALIC or Font.BOLD (each is a static field of class Font). Font styles can be used in combination (e.g., Font.ITALIC + Font.BOLD). • The font size is measured in points. A point is 1/72 of an inch. • Graphics method setFont sets the drawing font in which text will be displayed. • Font method getSize returns the font size in points. • Font method getName returns the current font name as a string. • Font method getStyle returns an integer value representing the current Font’s style. • Font method getFamily returns the name of the font family to which the current font belongs. The name of the font family is platform specific. • Class FontMetrics contains methods for obtaining font information. • Font metrics include height, descent and leading. Section 13.5 Drawing Lines, Rectangles and Ovals • Graphics methods fillRoundRect and drawRoundRect draw rectangles with rounded corners. • Graphics methods draw3DRect and fill3DRect draw three-dimensional rectangles. • Graphics methods drawOval and fillOval draw ovals. Section 13.6 Drawing Arcs • An arc is drawn as a portion of an oval. • Arcs sweep from a starting angle by the number of degrees specified by their arc angle. • Graphics methods drawArc and fillArc are used for drawing arcs. Section 13.7 Drawing Polygons and Polylines • Class Polygon contains methods for creating polygons. • Polygons are closed multisided shapes composed of straight-line segments. • Polylines are sequences of connected points. • Graphics method drawPolyline displays a series of connected lines. • Graphics methods drawPolygon and fillPolygon are used to draw polygons. • Polygon method addPoint adds pairs of x- and y-coordinates to the Polygon. Section 13.8 Java 2D API • The Java 2D API provides advanced two-dimensional graphics capabilities. • Class Graphics2D —a subclass of Graphics—is used for drawing with the Java 2D API. • The Java 2D API’s classes for drawing shapes include Line2D.Double, Rectangle2D.Double, RoundRectangle2D.Double, Arc2D.Double and Ellipse2D.Double. • Class GradientPaint helps draw a shape in gradually changing colors—called a gradient. • Graphics2D method fill draws a filled object of any type that implements interface Shape. • Class BasicStroke helps specify the drawing characteristics of lines. • Graphics2D method draw is used to draw a Shape object. • Classes GradientPaint and TexturePaint help specify the characteristics for filling shapes with colors or patterns. • A general path is a shape constructed from straight lines and complex curves and is represented with an object of class GeneralPath. • GeneralPath method moveTo specifies the first point in a general path. • GeneralPath method lineTo draws a line to the next point in the path. Each new call to lineTo draws a line from the previous point to the current point. • GeneralPath method closePath draws a line from the last point to the point specified in the last call to moveTo. This completes the general path. • Graphics2D method translate is used to move the drawing origin to a new location. • Graphics2D method rotate is used to rotate the next displayed shape. |