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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.