MusicShopDisplay

MusicShopDisplay - - SPACER; GRect rect = new GRect(nextX,...

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

View Full Document Right Arrow Icon
/* * File: MusicShopDisplay * ---------------------- * This file handles the display of album inventory * for the music shop. */ import acm.graphics.*; import java.awt.event.*; i public class MusicShopDisplay extends GCanvas implements ComponentListener { i // Constructor public MusicShopDisplay() { addComponentListener(this); lastAlbum = null; } // Display the album name, band name, and number in stock // for a single album if it is in our inventory. Otherwise, // just clear the display. public void displayInventory(Album album) { removeAll(); lastAlbum = album; if (album != null) { int numStocked = album.getNumStocked(); add(new GLabel("Album [" + album.getAlbumName() + "] by [" + album.getBandName() + "]"), 10, (getHeight() - BAR_HEIGHT) / 2 - SPACER); 1 // Display squares indicating number in inventory double nextX = SPACER; for(int i = 0; i < numStocked; i++) { double barLength = (getWidth() / (double)MAX_INVENTORY)
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: - SPACER; GRect rect = new GRect(nextX, (getHeight() - BAR_HEIGHT) / 2, barLength, BAR_HEIGHT); rect.setFilled(true); add(rect); nextX += barLength + SPACER; } GLabel label = new GLabel(numStocked + " in stock"); add(label, 10, (getHeight() + BAR_HEIGHT) / 2 + SPACER + label.getAscent()); } } // Whenever we need to update the display, continue to // display the last album shown public void update() { displayInventory(lastAlbum); } public void componentResized(ComponentEvent e) { update(); } public void componentHidden(ComponentEvent e) { } public void componentMoved(ComponentEvent e) { } public void componentShown(ComponentEvent e) { } p /* constants */ private static final double BAR_HEIGHT = 20; private static final double SPACER = 10; private static final int MAX_INVENTORY = 20; p /* private instance variables */ private Album lastAlbum; }...
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 2

MusicShopDisplay - - SPACER; GRect rect = new GRect(nextX,...

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