package lawyer; import java.awt.*; import java.net.*; /** * Provides a canvas on which to display Images. The Canvas sizes itself so * that it will be able to display the largest Image it contains, so that the * parent Container doesn't need to adjust the layout at all. * * @author David Danks * @version 1.0; July 24, 1998 * @see LessonManager */ public class ImageCanvas extends Canvas { /** @serial */ Image images[]; /** @serial */ MediaTracker mt; /** @serial */ int imageNum; /** @serial */ int xMax, yMax; /** @serial */ boolean door = false; /** * The constructor loads all of the images, and sets the max sizes appropriately. * * @param imageLocats An array of the image URLs. */ public ImageCanvas(URL imageLocats[]) { imageNum = 0; xMax = 0; yMax = 0; // set up the Image[] and MediaTracker object images = new Image[imageLocats.length]; mt = new MediaTracker(this); for (int i=0; itrue, if there should be a sign. * false, otherwise. */ public void setDoorSign(boolean b) { door = b; } /** * @return The number of images contained in the ImageCanvas */ public int getNumImages() { return images.length; } /** * @return A Dimension object encoding the size it wants to be */ public Dimension getPreferredSize() { return new Dimension(xMax, yMax); } /** * @return A Dimension object encoding the size it has to be */ public Dimension getMinimumSize() { return new Dimension(xMax, yMax); } /** * Sets the current Image being displayed to num. Requires a * repaint() call to take effect. * * @param num The number of the Image to be displayed */ public void setImageNum(int num) { if (num >= images.length) num = images.length-1; imageNum = num; } /** * Advances the current Image number by one. Requires a repaint() call. */ public void incrementImageNum() { if (imageNum == images.length-1) { } else { imageNum++; } } /** * Reduces the current Image number by one. Requires a repaint() call. */ public void decrementImageNum() { if (imageNum == 0) { } else { imageNum--; } } /** * Draws the current Image on the Canvas. If door is set, it also * draws a sign on the door (Note: the door code is very specific to the * picture we are currently using. */ public void paint(Graphics g) { g.drawImage(images[imageNum], 0, 0, this); if (door) { int x = 120; int y = 175; int width = Math.max(g.getFontMetrics().stringWidth("Enter your"), g.getFontMetrics().stringWidth("name below")); width += 10; int height = g.getFontMetrics().getHeight()*3; g.setColor(Color.gray.brighter()); g.fillRoundRect(x, y, width, height, width/5, height/5); g.setColor(Color.black); g.drawString("Enter your", x+width/2-g.getFontMetrics().stringWidth("Enter your")/2, y+g.getFontMetrics().getHeight()/2+g.getFontMetrics().getAscent()); g.drawString("name below", x+width/2-g.getFontMetrics().stringWidth("name below")/2, y+g.getFontMetrics().getHeight()*3/2+g.getFontMetrics().getAscent()); } } }