Additions to the ACM Libraries

The above packages, developed by Paul Oser, can be used in conjunction with the ACM package to develop programs very early in the process of learning Java that employ simple graphics and/or animation by providing pre-built graphical user interfaces. These packages also include improved versions of some of the ACM classes. For instance, using the GBreadboard and Turtle classes, one can create and control animated, logo-inspired turtles with code not much more complex than a simple console program would require to solve a quadratic equation.

As an example, consider the following program that animates the drawing of a square spiral with a user-input number of loops:

import java.util.Scanner;
import acm.breadboards.OneButtonBreadboard;
import acm.toys.Turtle;

public class SquareSpiralDrawer extends OneButtonBreadboard {
	
	public void run() {
		// display instructions to user
		this.getTextArea().setText("Enter a positive integer n below and click " +
		                "'Go' to draw a square spiral with n loops");
		this.getButton().setText("Go");
	}
	
	public void onButtonClick() {
		// get user input
		String userInput = this.getTextField().getText();
		Scanner scanner = new Scanner(userInput);
		
		// if the user typed an integer...
		if (scanner.hasNextInt()) {
			int numLoops = scanner.nextInt();
			
			// create a turtle
			Turtle turtle = new Turtle();
                        turtle.setLocation(250,200);
			this.add(turtle);
	
			// have the turtle draw a spiral with the desired number of loops
			for (int i = 0; i < 4 * numLoops; i++) {
				turtle.forward(10 * i);
				turtle.left(90); // degrees
			}
		}
		// if the user didn't type an integer, complain
		else {
			this.getTextArea().setText("Input not an integer! Try again.");
		}
	}
}

Sample output: