Beginning graphics: an introduction to Java2D


A simple illustration
Java2D
Graphics Primitives
Playing back a path
Graphics2D's internal state
Working with text
Affine transforms
Graphing a function
Stroking lines
Clipping
Compositing
Rendering hints

Based on notes Beginning graphics and AffineTransforms and stroking
ADDANCHOR simple FIRSTANCHOR A simple illustration
ANCHOR The code
import javax.swing.*;
import java.awt.*;

public class DrawRectangle extends JPanel {

    public void paintComponent(Graphics g) {
	super.paintComponent(g);         // always include this
        g.drawLine(100, 100, 100, 200);
        g.drawLine(100, 200, 300, 200);
        g.drawLine(300, 200, 300, 100);
        g.drawLine(300, 100, 100, 100);
    }
    
    public static void main(String[] args) {
        DrawRectangle drawRectangle = new DrawRectangle();
        JFrame frame = new JFrame("Draw Rectangle");
	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(drawRectangle);
        frame.setSize(400, 300);
        frame.setVisible(true);
    }
}
ANCHOR How it works

ANCHOR The default coordinate system

The units are given in pixels.

This is a left-handed coordinate system. ADDANCHOR j2d ANCHOR Java2D

Distance.java

ANCHOR Notice:

ADDANCHOR primitives ANCHOR Some more graphics primitives

Quadratic curves

A quadratic curve has the form

where

ANCHOR Subdivision

Quadratic curves may be drawn elegantly by a subdivision process

The curve is broken into two halves, each of which is itself a quadratic curve whose control points are obtained by averaging the original control points. ANCHOR Instantiation

Quadratic curves may be instantiated using either

    QuadCurve2D.Double quad = new QuadCurve2D.Double(double x0, double y0,
        double x1, double y1, double x2, double y2);

or

    QuadCurve2D.Double quad = new QuadCurve2D.Double();
    quad.setCurve(Point2D p0, Point2D p1, Point2D p2);
ANCHOR Cubic curves

A cubic curve has the form

where

ANCHOR Instantiation

Cubic curves may be instantiated using either

    CubicCurve2D.Double quad = new CubicCurve2D.Double(double x0, double y0,
        double x1, double y1, double x2, double y2, double x3, double y3);

or

    CubicCurve2D.Double quad = new CubicCurve2D.Double();
    quad.setCurve(Point2D p0, Point2D p1, Point2D p2, Point2D p3);
ANCHOR Rectangular shapes

Rectangular shapes, including rectangles and ellipses, are generally defined by stating the upper left corner of the smallest rectangle containing the shape along with its width and height.

    Rectangle2D.Double rectangle = new Rectangle2D.Double(double ulx, double uly,
        double width, double height);

    Ellipse2D.Double ellipse = new Ellipse2D.Double(double ulx, double uly,
        double width, double height);
ANCHOR Arcs

    Arc2D.Double arc = new Arc2D.Double(double ulx, double uly, double width,
        double height, double angleStart, double angleEnd, int closure);

ANCHOR General paths

The class GeneralPath lets us build paths like we might in PostScript.

GeneralPath path = new GeneralPath();
path.moveTo(50, 50);
path.lineTo(150, 150);
path.quadTo(200, 200, 250, 150);
path.curveTo(250, 250, 150, 250, 150, 200);
path.closePath();
g.draw(path);
ANCHOR Area

Constructive area geometry operations area available through the Area class.

Area area = new Area(new Rectangle(50, 25, 100, 125));
Area addArea = new Area(new Ellipse2D.Float(100, 100, 75, 75));
Area intersectArea = new Area(new Ellipse2D.Float(50, 50, 125, 125));
area.add(addArea);
area.intersect(intersectArea);

g.setPaint(Color.blue);
g.fill(area);
g.setPaint(Color.black);
g.setStroke(new BasicStroke(4f, BasicStroke.CAP_BUTT,
				    BasicStroke.JOIN_MITER, 3f));
g.draw(area);
ANCHOR

ADDANCHOR iterator ANCHOR Playing back a path

Any Shape object can produce a PathIterator, a description of the outline of the Shape in terms of line segments and quadratic and cubic curves.

Here is how Java2D draws a circle:

PathIteratedCircle.java ADDANCHOR state ANCHOR Graphic2D's internal state

A Graphics2D object uses some adjustable parameters when rendering a Shape. These include