/* * @(#)HilitCanvas.java 1.0 97/09/15 * * Copyright (c) 1997 Erica Schulman. All Rights Reserved. */ import java.awt.*; /** * This class provides a set of canvases, each capable of * trapping user events. If the mouse is positioned over one * of them, it displays an alternate (highlight) image. * *

Highlight and normal images may be of different dimensions; they * will both be centered on the coordinates given to the constructor. * *

mouseDown is an abstract function; it is expected that each class will * want to do something when the mouse is clicked. */ abstract public class HilitCanvas extends Canvas { /** * Paint either the highlight or non-highlight * image, with appropriate offsets, * depending upon the state of the object. */ public void paint(Graphics g) { if(isHilit()) { g.drawImage(imageOn, xOnOffset, yOnOffset, this); } else { g.drawImage(imageOff, xOffOffset, yOffOffset, this); } } private boolean hilit = false; private int xCenter; private int yCenter; private int xOnOffset; private int yOnOffset; private int xOffOffset; private int yOffOffset; private Image imageOn; private Image imageOff; // x and y are the center of the canvas // inherit background HilitCanvas(int x, int y, int num, Image off, Image on) { imageOn = on; imageOff = off; setBackground(null); xCenter = x; yCenter = y; calculateOffsets(x, y); } // set own background HilitCanvas(int x, int y, int num, Image off, Image on, Color bground) { this(x, y, num, off, on); setBackground(bground); } /* * Determines the offsets needed to ensure that both * images will be centered on the coordinates given to * the constructor. x, y = center coordinates */ private void calculateOffsets(int x, int y) { int width; int height; int widthOn; int widthOff; int heightOn; int heightOff; int xnew; int ynew; widthOn = imageOn.getWidth(this); heightOn = imageOn.getHeight(this); widthOff = imageOff.getWidth(this); heightOff = imageOff.getHeight(this); // integer math is fine for the following if( widthOn > widthOff) { width = widthOn; xOnOffset = 0; xOffOffset = (width - widthOff)/2; } else { width = widthOff; xOffOffset = 0; xOnOffset = (width - widthOn)/2; } if( heightOn > heightOff) { height = heightOn; yOnOffset = 0; yOffOffset = (height - heightOff)/2; } else { height = heightOff; yOffOffset = 0; yOnOffset = (height - heightOn)/2; } xnew = x - (width / 2); ynew = y - (height / 2 ); resize(width, height); move(xnew, ynew); } /** * Show if this object is highlighted. */ public boolean isHilit() { return(hilit); } /** * Do something when the mouse is clicked. */ abstract public boolean mouseDown(Event e, int x, int y); /** * Highlight this object when the mouse enters it. * Extended classes may wish to overload this function so that it * only happens under certain circumstances. */ public boolean mouseEnter(Event e, int x, int y) { hilitOn(); repaint(); return(true); } /** * Un-highlight this object when the mouse leaves it. * Extended classes may wish to overload this function so that it * only happens under certain circumstances. */ public boolean mouseExit(Event e, int x, int y) { hilitOff(); repaint(); return(true); } /** * Set the highlighted image. * @param i Image to be used */ protected void setImageOn(Image i) { imageOn = i; calculateOffsets(xCenter, yCenter); } /** * Set the non-highlighted image. * @param i Image to be used */ protected void setImageOff(Image i) { imageOff = i; calculateOffsets(xCenter, yCenter); } /** * Turn highlighting on. */ protected void hilitOn() { hilit = true; } /** * Turn highlighting off. */ protected void hilitOff() { hilit = false; } /** * Move the canvas. */ public void setCenter(int newX, int newY) { xCenter = newX; yCenter = newY; calculateOffsets(newX, newY); } }