- Getting Started with Java 2D
- Drawing Shapes
- Paint Styles
- Transparent Drawing
- Using Local Fonts
- Stroke Styles
- Coordinate Transformations
- Other Capabilities of Java 2D
- Summary
10.8 Other Capabilities of Java 2D
Since Java 2D already does a lot of calculations compared to the old AWT, by default, many of the optional features to improve performance are turned off. However, for crisper drawings, especially for rotated text, the optional features should be turned on.
The two most important settings are to turn on antialiasing (smooth jagged lines by blending colors) and to request the highest-quality graphics rendering. This approach is illustrated below:
RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); ... public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; g2d.setRenderingHints(renderHints); ... }
Core Approach
For the highest-quality graphics, use RenderingHints and turn antialiasing on, VALUE_ANTIALIAS_ON, and set the presentation for quality, not speed, with VALUE_RENDER_QUALITY.
Thus far, we've only presented a small fraction of the power behind Java 2D. Once you've gained experience with the basics, you'll want to tackle advanced Java 2D techniques that allow you to:
Create custom color mixing (implement Composite and CompositeContext interfaces).
Perform bounds/hit testing (see contains and intersects methods of Shape).
Create new fonts by transforming old ones (use Font.deriveFont).
Draw multifont or multicolor strings (use the draw method of TextLayout).
Draw outlines of fonts, or fill fonts with images or gradient colors (use the getOutline method of TextLayout).
Perform low-level image processing and color model manipulation.
Produce high-quality printing for Swing components. See Section 15.5 (Swing Component Printing) for details.