|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
at the command line. You should see a spiral of shapes in a variety of colors.
Listing 39.18 PathsFill.javaThis Program, from Monica Pawlans Article on the JDC Site, Demonstrates the Major Capabilities of 2D Graphics import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class PathsFill extends Canvas { public PathsFill() { setBackground(Color.cyan); } public void paint(Graphics g) { int n = 0; Dimension theSize = getSize(); Graphics2D g2; g2 = (Graphics2D) g; g2.setRenderingHints(Graphics2D.ANTIALIASING, ⇒Graphics2D.ANTIALIAS_ON); GeneralPath p = new GeneralPath(1); p.moveTo( theSize.width/6, theSize.height/6); p.lineTo(theSize.width*5/6, theSize.height/6); p.lineTo(theSize.width*5/6, theSize.height*5/6); p.lineTo( theSize.width/6, theSize.height*5/6); p.closePath(); g2.setColor(Color.blue); g2.draw(p); AffineTransform at = new AffineTransform(); at.scale(.5, .5); at.translate(theSize.width/2, theSize.height/2); g2.setTransform(at); g2.setColor(Color.red); g2.fill(p); Color colorArray[] = new Color[10]; colorArray[0] = Color.blue; colorArray[1] = Color.green; colorArray[2] = Color.magenta; colorArray[3] = Color.lightGray; colorArray[4] = Color.pink; colorArray[5] = Color.white; colorArray[6] = Color.yellow; colorArray[7] = Color.black; colorArray[8] = Color.gray; colorArray[9] = Color.orange; for(n = 0; n < 10; n++){ at.scale(.9, .9); at.rotate(15, theSize.width/2, theSize.height/2); g2.setTransform(at); g2.setColor(colorArray[n]); g2.fill(p); } } public static void main(String s[]) { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) ⇒{System.exit(0);} public void windowClosed(WindowEvent e) ⇒{System.exit(0);} }; Frame f = new Frame(Simple 2D Demo ...); f.addWindowListener(l); f.add(Center, new PathsFill()); f.pack(); f.setSize(new Dimension(500,500)); f.show(); } } Understanding Coordinate SpacesOne reason that graphics display is an interesting problem is that not all graphics devices have the same coordinate system or resolution. This problem is compounded for Java, which has staked its reputation on being the Write Once, Run Anywhere language. In order to live up to this name, Sun needed a way for designers to produce graphics that look good regardless of the devices characteristics. In some cases you may want to write a program that enables a user to drawand then play backthat file of captured points. If the playback device has different characteristics than the one on which the original drawing was captured, a potential for error exists. Sun solved this problem by introducing a user coordinate space, also known in some of Suns documentation as user space. Using Transforms To actually display a Graphics2D object on a device, you must transform the object from user space to a specific device space. Although you may apply a transform at any time, youll start with a default transform that is selected for you based on the target of your Graphics2D object. All transforms from user space to device space share three characteristics:
If you specify a screen or an image buffer as the target for your drawing, youll get the identity transform. If your target is a printer or other high-resolution device, youll get a default transform of 72 user-space coordinates per device inch. Understanding Rendering
Rendering is the mechanism by which the graphics object is made to appear on the output device. With Graphics2D-based objects, rendering proceeds in four conceptual steps. (Because of optimization, Sun does not guarantee that each step will be performed each time an image is renderedit guarantees only that the finished graphic will appear as though it has gone through all four rendering steps.) The four steps are
By understanding how the Graphics2D class processes each step, youll be better able to produce high-quality graphics. Determining Where to Render a Shape The first step in rendering is to determine the area of the screen, printed page, or other output device that is affected by the rendering operation. Graphics2D objects may either be shapes, text, or images. If the object is a shape, the where to render question is answered by computing the outline of the shape. If the object is text, then Graphics2D computes the outline of the text by using information about the font. In the case of an image, the class computes a bounding box into which the image is rendered. This section provides details on how each of these three kinds of objects is rendered. In Listing 39.18, for example, you see the lines: Graphics2D g2; g2 = (Graphics2D) g;
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |