Displaying Graphics Primitives

The Java Graphics class provides you with methods that make it easy to draw two-dimensional graphics primitives. You can draw any two-dimensional graphics primitive, including

  • Lines

  • Rectangles

  • Ovals

  • Arcs

  • Polygons

The following sections explain how to draw these graphics primitives.

Drawing Lines

Perhaps the simplest graphics primitive is a line. The Java Graphics class provides a single drawLine() method for drawing lines. The complete definition of the drawLine() method is

public abstract void drawLine(int  x1, int  y1, int  x2, int  y2)

The drawLine() method takes two pairs of coordinates—x1, y1 and x2, y2—and draws a line between them.

The applet in Listing 38.1 uses the drawLine() method to draw some lines. Figure ...

Get Platinum Edition Using XHTML™, XML, and Java™ 2 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.