Skip navigation links

Package acm.graphics

This package provides a set of classes that support the creation of simple, object-oriented graphical displays.

See: Description

Package acm.graphics Description

This package provides a set of classes that support the creation of simple, object-oriented graphical displays. The basic model is that of a felt board of the sort one might use as a child. The felt board itself is represented by the GCanvas class, which is a lightweight container that supports the addition of graphical objects that correspond to the shapes attached to the board. These graphical objects come from a variety of classes that represent geometrical shapes and other graphical entities including lines, ovals, rectangles, images, and strings.

The standard approach to using the acm.graphics package consists of the following steps:

  1. Allocate a new GCanvas object to serve as the surface to which subsequent graphical objects are attached.
  2. Make the canvas appear on the screen by adding it to some component that is displayed. The simplest strategy, for example, is to create a JFrame object and then add the GCanvas at the center of that frame.
  3. Use the add method in GCanvas to add new graphical objects from the GObject hierarchy.

As an example, the following main program follows this outline to display a red square on the screen:


 public static void main(String[] args) {
    GCanvas gc = new GCanvas();
    JFrame frame = new JFrame();
    frame.getContentPane().add(BorderLayout.CENTER, gc);
    GRectangle square = new GRectangle(100, 100, 200, 200);
    square.setFilled(true);
    square.setColor(Color.RED);
    gc.add(square);
    frame.show();
 }
 

Much of this boilerplate can be eliminated by using the GraphicsProgram class in the acm.program package.

Skip navigation links