Tower -...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//******************************************************************** // Author: Lewis/Loftus // // Solution to Programming Project 11.11 // // Encapsulates a tower in the grapical Towers of Hanoi solution //******************************************************************** import java.util.Stack; import java.awt.Color; import java.awt.Graphics; class Tower { Stack disks; int towerHeight; static final int WIDTH = 10; //----------------------------------------------------------------- // Creates a new tower, height should be set to the max number of // disks the tower will hold. //----------------------------------------------------------------- public Tower(int height) { disks = new Stack(); towerHeight = height; } //----------------------------------------------------------------- // Adds disk to the top of the tower //----------------------------------------------------------------- void addDisk(Disk d) { disks.push(d); } //----------------------------------------------------------------- // Removes the top disk from the tower
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //-----------------------------------------------------------------Disk removeDisk() { if (disks.empty()) return null; else return (Disk)disks.pop(); } //-----------------------------------------------------------------// Draws the tower using graphics context g. The coordinates (x,y) // represent the bottom left corner of the tower //----------------------------------------------------------------- void draw(Graphics g, int x, int y) { // draw tower int height = Disk.HEIGHT * towerHeight; g.setColor(; g.fillRect(x,y-height, WIDTH, height); // draw disks if (!disks.isEmpty()) { // create a stack of disks in reverse order Stack temp = new Stack(); while (!disks.empty()) temp.push(disks.pop()); // draw the disks int location = 1; int diskX = x + WIDTH / 2; while (!temp.isEmpty()) { Disk d = (Disk)temp.pop(); int diskY = y - Disk.HEIGHT * location; d.draw(g, diskX, diskY); // put disk back on stack disks.push(d); location ++; } } } }...
View Full Document

This note was uploaded on 05/17/2011 for the course COP 3530 taught by Professor Davis during the Spring '08 term at University of Florida.

Page1 / 2

Tower -...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online