Blakes 21 Days Chapter 13 Document


Day 13, Creating Java2D Graphics

The Graphics2D Class

The Graphics Coordinate System

Figure 13.1 - The Java graphics coordinate system. - goes here

401

Drawing Text

Improving Fonts and Graphics with Antialiasing

Finding Information About a Font

Table 13.1 Font Metrics Methods
Method Name Description
stringWidth(String) Given a string, returns the full width of that string in pixels
charWidth(char) Given a character, returns the width of that character
getHeight() Returns the font's total height

First Program - Listing 13.1

Command Line & Run Project Instructions

The Explanation

Figure 13.2 - Displaying centered text in a graphical user interface - goes here

402

Color

Using Color Objects

Testing and Setting the Current Colors

13 colors presented
black (0, 0, 0) magenta (255, 0, 255)
blue (0, 0, 255) orange (255, 200, 0)
cyan (0, 255, 255) pink (255, 175, 175)
darkGray (64, 64, 64) red (255, 0, 0)
gray (128, 128, 128) white (255, 255, 255)
green (0, 255, 0) yellow (255, 255, 0)
lightGray (192, 192, 192)  

Drawing Lines and Polygons

User and Device Coordinate Spaces

Specifying the Rendering Attributes

Fill Patterns

Figure 13.3 - acyclic and cyclic gradients shifts. - goes here

403

Figure 13.4 - Two rectangles using the same GradientPaint. - goes here

404

Setting a Drawing Stroke

Figure 13.5 - Endpoint cap styles. - goes here

405

Figure 13.6 - Endpoint juncture styles. - goes here

406

Creating Objects to Draw

Lines

Rectangles

Ellipses

Arcs

Figure 13.7 - Determining the starting degree of an arc. - goes here

407

Figure 13.8 - Arc closure styles. - goes here

408

Polygons

Drawing Objects

Drawing a Map - (The Second Program)

Second Program - Listing 13.2

The Explanation

Figure 13.9 - The Map application. - goes here

409

Summary



Q & A



Quiz - Questions

  1. What object is required before you can draw something in Java using Swing ?
    1. Graphics2D
    2. WindowListener
    3. JFrame
  2. Which of the following is not a valid Java statement to create a Color object ?
    1. Color c1 = new Color(0F, 0F, 0F);
    2. Color c2 = new Color(0, 0, 0);
    3. Both are valid
  3. What does getSize().width refer to ?
    1. The width of the interface component's window
    2. The width of the frame's window
    3. The width of any graphical user interface component in Java

Answers

  1. A. The Graphics2D object is cast from a Graphics object and represents a graphics context for a graphical user interface component.
  2. C. Both are valid ways to create the object.
  3. C. You can call getSize().width and getSize().height on any user interface component.


Certification Practice



Exercise