The Java 2D API
by Bill Loeb

Listing One
import java.awt.*;
import java.awt.event.*;
public class TooDee extends Canvas {
    public TooDee() {
        setBackground(Color.white);
    }
    public void paint(Graphics g) {
        g.setColor(Color.blue);
        g.drawRect(100,100,100,100);
    }
    public static void main(String argv[]) {
        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        };
        Frame f = new Frame("Too Dee");
        f.addWindowListener(l);
        f.add("Center", new TooDee());
        f.pack();
        f.setSize(new Dimension(400,400));
        f.show();
    }
}

Listing Two
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setColor(Color.blue);
    g2.setStroke(new BasicStroke(4.0f));
    g2.drawRect(100,100,100,100);
}

Listing Three
 ...
import java.awt.geom.*; // need the geometry package for Shapes
 ...
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    // Step 1: Set up the graphics context
    g2.setColor(Color.blue);
    g2.setStroke(new BasicStroke(4.0f));
    // Step 2: Define the object to draw
    Rectangle2D r = new 
Rectangle2D.Float(100.0f,100.0f,100.0f,100.0f);
    // Step 3: Call one of the rendering methods
    g2.draw(r);
}

Listing Four
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    float dash[] = { 20.0f, 10.0f };
    g2.setPaint(Color.blue);
   g2.setStroke(new BasicStroke(
        4.0f,                   // line width
        BasicStroke.CAP_ROUND,  // end cap style
        BasicStroke.JOIN_BEVEL, // join style
        0.0f,                   // miter limit
        dash,                   // dash array
        0.0f));                 // dash phase
    Rectangle2D r = new
        Rectangle2D.Float(100.0f,100.0f,200.0f,200.0f);
    g2.draw(r);
}

Listing Five
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    Ellipse2D e = new Ellipse2D.Float(100.0f,100.0f,100.0f,100.0f);
    // draw the interior in blue
    g2.setPaint(Color.blue);
    g2.fill(e);
    // draw the outline in red
    g2.setPaint(Color.red);
    g2.draw(e);
}

Listing Six
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    Ellipse2D e = new Ellipse2D.Float(100.0f,100.0f,100.0f,100.0f);
    GradientPaint gp = new GradientPaint(
        100.0f, 100.0f, // starting point
        Color.blue,     // starting color
        200.0f, 200.0f, // ending point
        Color.green);   // ending color
    g2.setPaint(gp);
    g2.fill(e);
    g2.setPaint(Color.red);
    g2.draw(e);
}

Listing Seven
 ...
import java.awt.image.*; // needed for BufferedImage
 ...
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    Ellipse2D e = new Ellipse2D.Float(100.0f,100.0f,100.0f,100.0f);
    BufferedImage bi = new BufferedImage(
        5,  // width
        5,  // height
        BufferedImage.TYPE_INT_RGB);    // RGB image
    // Get the graphics context of the image and draw into it
    Graphics2D big = bi.createGraphics();
    big.setColor(Color.blue);
    big.fillRect(0,0,6,6);
    big.setColor(Color.green);
    big.fillRect(2,2,4,4);
    // Create the texture from the complete image
    Rectangle2D r = new Rectangle2D.Float(0.0f,0.0f,6.0f,6.0f);
    TexturePaint tp = new TexturePaint(bi, r);

    g2.setPaint(tp);
    g2.fill(e);
    g2.setPaint(Color.red);
    g2.draw(e);
}

Listing Eight
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    Ellipse2D e = new Ellipse2D.Float(100.0f,100.0f,100.0f,100.0f);
    GradientPaint gp = new GradientPaint(
        100.0f, 100.0f, // starting point
        Color.blue,     // starting color
        200.0f, 200.0f, // ending point
        Color.green);   // ending color
    g2.setPaint(gp);
    g2.setStroke(new BasicStroke(10.0f));
    g2.draw(e);
}

Listing Nine
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                        RenderingHints.VALUE_RENDER_QUALITY);
    Ellipse2D e = new Ellipse2D.Float(100.0f,100.0f,100.0f,100.0f);
    GradientPaint gp = new GradientPaint(
        100.0f, 100.0f, // starting point
        Color.blue,     // starting color
        200.0f, 200.0f, // ending point
        Color.green);   // ending color
    g2.setPaint(gp);
    g2.setStroke(new BasicStroke(10.0f));
    g2.draw(e);
}

Listing Ten
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    Rectangle r = new Rectangle(100,100,100,100);
    // Set up a rotation of 10 degrees around the origin.
    // Note that we need to convert from degrees to radians.
    AffineTransform t = new AffineTransform();
    t.rotate(10*java.lang.Math.PI/180);
    g2.setTransform(t);

    g2.setPaint(Color.blue);
    g2.fill(r);
    g2.setPaint(Color.red);
    g2.draw(r);
}

Listing Eleven
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;

    GeneralPath gp = new GeneralPath();
    Line2D l1 = new Line2D.Float(50.0f,100.0f,100.0f,100.0f);
    gp.append(l1, true);
    CubicCurve2D cc = new CubicCurve2D.Float(
        100.0f,100.0f,  // starting point
        125.0f,125.0f,  // control point 1
        150.0f,125.0f,  // control point 2
        175.0f,100.0f); // ending point
    gp.append(cc, true);

    Line2D l2 = new Line2D.Float(175.0f,100.0f,225.0f,100.0f);
    gp.append(l2, true);

    g2.setStroke(new BasicStroke(4.0f));
    g2.setPaint(Color.blue);
    g2.draw(gp);
}

Listing Twelve
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    Rectangle r = new Rectangle(100,100,100,100);
    Area a = new Area(r);

    Ellipse2D e = new Ellipse2D.Float(150.0f,150.0f,25.0f,25.0f);
    a.subtract(new Area(e));

    g2.setPaint(Color.blue);
    g2.fill(a);

    g2.setPaint(Color.red);
    g2.draw(a);
}


5


