package ciips.animation; import java.awt.*; import java.applet.*; import java.io.*; import java.net.*; import java.util.*; /** * The ImageButton class extends the original * java.awt.Button to display graphical images in * gif format. In this particular implementation, * the images to be loaded have to be located in the same * directory as the code base (which can be accessed * using Applet.getCodeBase()). *

* In order to work around the bug in some of the older java complient * web browser, in particular, the class not found exception * during the initilization of the frame using Netscape 2.0, * this class is casted to its parent class Button * and explicitly casted back to ImageButton when the * customized methods are required. * * @see java.awt.Button */ public class ImageButton extends Button { String imageFile; AlgAnimApp app; Image image = null, enabledImage = null, disabledImage = null; Font font = new Font("Helvetica", Font.PLAIN, 10); ControlPanel parent; private static final int img_height = 42; private static final int img_width = 52; private static final Dimension img_dim = new Dimension( img_height, img_width ); /** * Creates an ImageButton using the gif * file specified by the first parameter on the panel defined by * the third parameter. *

* Before calling this constructor, make sure that the image file * imageFile.gif exists. It is also required to have * another gif image imageFileDisable.gif, which * appears to be the disabled button. * * @param imageFile The gif image file without the * .gif extension. The extension will be appended * when calling the Applet.getImage() method. *

* @param app An instance to an applet. The only applet in this * cluster of classes, an instance of AlgAnimApp, is * usually passed in here. This particular applet is used to * obtain the code base, i.e. the location of the image files. *

* @param parent The panel where this image button is going to reside. * This paremeter is passed in so that the repaint method for the * panel can be called when a refreshing is required. */ public ImageButton(String imageFile, AlgAnimApp app, ControlPanel parent) { this.parent = parent; setLabel( imageFile ); this.imageFile = imageFile; this.app = app; URL codeBase = app.getCodeBase(); try { image = enabledImage = app.getImage(new URL(codeBase, imageFile+".gif")); disabledImage = app.getImage(new URL(codeBase, imageFile+"Disabled.gif")); prepareImage(image, img_height, img_width, null); repaint(); } catch (MalformedURLException e) { System.out.println("Cannot get button Image: " + imageFile + ".gif"); } } /** * Specify the dimension of the button. In this particular application, * images with resolution of 42x52 are used. Therefore, this * method always returns Dimension with * width 42 and height 52. * This method is only called by the system layout manager. * @return The dimention of the button. */ public Dimension getPreferredSize() { return img_dim; } /** * Specify the dimension of the button. In this particular application, * images with resolution of 42x52 are used. Therefore, this * method always returns Dimension with * width 42 and height 52. * This method is only called by the system layout manager. * @return The dimention of the button. */ public Dimension preferredSize() { return img_dim; } /** * Disable the image button and set the current image * to imageFileDisable.gif. */ public void setDisable() { image = disabledImage; prepareImage(image, img_height, img_width null); disable(); parent.refreshButtons(); } /** * Enable the image button and set the current image * to imageFile.gif */ public void setEnable() { image = enabledImage; prepareImage(image, img_height, img_width null); enable(); parent.refreshButtons(); } /** * Method to draw image on the button */ public void print(Graphics g) { g.drawImage(image, 0, 0, null); } /** * This method is invokes when the repaint method * is called. */ public void update(Graphics g) { g.drawImage(image, 0, 0, null); } /** * Method to draw image on the button */ public void paint(Graphics g) { g.drawImage(image, 0, 0, null); } }