acm.graphics.package.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javakarel Show documentation
Show all versions of javakarel Show documentation
This the original Stanford Karel for Java, packaged for Maven. ACM Library is included. See also https://cs.stanford.edu/people/eroberts/karel-the-robot-learns-java.pdf
The newest version!
The acm.graphics Package
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:
- Allocate a new
GCanvas
object to serve as the surface to which
subsequent graphical objects are attached.
- 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.
- 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.
The public classes in the acm.graphics
package can be diagrammed
as follows: