Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


at the command line. You should see a spiral of shapes in a variety of colors.


NOTE:  Graphics2D is an abstract class—you must either subclass it yourself or rely on Sun’s subclasses to actually draw anything.
ON THE WEB
http://developer.java.sun.com/developer/technicalArticles/monicap/2DGraphics/Intro/simple2D.html If you’re a member of Sun’s Java Developer Connection, you can read this technical article, “New 2D Graphics Features,” by Monica Pawlan. The extended example in this section, Listing 39.18, is drawn from Pawlan’s article.

Listing 39.18 PathsFill.javaThis Program, from Monica Pawlan’s 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 Spaces

One 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 device’s characteristics.

In some cases you may want to write a program that enables a user to draw—and then play back—that 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 Sun’s 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, you’ll 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:

  The origin is in the upper-left corner of the target space (such as a screen or printed page).
  Increasing numbers in the x dimension move to the right.
  Increasing numbers in the y dimension move down.

If you specify a screen or an image buffer as the target for your drawing, you’ll get the identity transform. If your target is a printer or other high-resolution device, you’ll get a default transform of 72 user-space coordinates per device inch.

Understanding Rendering


NOTE:  You can use the Graphics2D class without understanding how the class places your shape, text, or image on the screen. If you’re building sophisticated graphics that may be rendered on different devices (such as monitors and printers), you need to know the basics of both coordinate spaces and color spaces. If you like, skip this section while you’re learning to use Graphics2D, then return here when you’re ready to build a sophisticated graphic.

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 rendered—it guarantees only that the finished graphic will appear as though it has gone through all four rendering steps.)

The four steps are

1.  Determine where to render.
2.  Constrain the current rendering operation to the current Clip.
3.  Determine what colors to render.
4.  Apply the colors to the destination drawing space.

By understanding how the Graphics2D class processes each step, you’ll 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;


Previous Table of Contents Next


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.