10.2. Drawing Shapes

With the AWT, you generally drew a shape by calling the drawXxx or fillXxx method of the Graphics object. In Java 2D, you generally create a Shape object, then call either the draw or fill method of the Graphics2D object, supplying the Shape object as an argument. For example:

public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2d = (Graphics2D)g;
  // Assume x, y, and diameter are instance variables.
  Ellipse2D.Double circle =
						new Ellipse2D.double(x, y, diameter, diameter);
						g2d.fill(circle);
  ...
}

Most of the Shape classes define both a Shape.Double and a Shape.Float version of the class. Depending on the version of the class, the coordinate locations are stored as either double precision numbers ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.