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;
String name;
Font font = new Font("Helvetica", Font.PLAIN, 10);
ControlPanel parent;
private static final String img_ext = ".gif";
private static final String disabled_name = "Disabled" + img_ext;
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 );
private static final boolean DEBUG = true;
/**
* 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) {
String img_name;
URL img_src;
this.parent = parent;
this.imageFile = imageFile;
this.app = app;
name = imageFile;
setLabel( name );
img_name = imageFile + img_ext;
URL codeBase = app.getCodeBase();
if( DEBUG ) System.out.println("ImageButton - [" + imageFile + "]" );
if( DEBUG ) System.out.println("ImageButton - codeBase [" + codeBase + "]" );
try {
img_src = new URL(codeBase, img_name);
image = enabledImage = app.getImage( img_src );
img_name = imageFile + disabled_name;
disabledImage =
app.getImage(new URL(codeBase, img_name));
if( DEBUG ) System.out.println("ImageButton - URL [" +
((img_src==null)?"null":img_src.toString()) + "]" );
/*
img_src = ClassLoader.getSystemResource( codeBase.toString() + "/" + img_name );
if( DEBUG ) System.out.println("ImageButton - URL [" +
((img_src==null)?"null":img_src.toString()) + "]" );
image = enabledImage = app.getImage(img_src);
if( DEBUG ) System.out.println("ImageButton - image [" +
((image==null)?"null":"OK") + "]" );
disabledImage = app.getImage(
ClassLoader.getSystemResource(imageFile+disabled_name));
*/
prepareImage(image, img_height, img_width, null);
repaint();
}
// catch (MalformedURLException e) {
catch ( Exception e ) {
System.out.println("Cannot get button Image: " + img_name );
}
}
/**
* 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);
paint(g);
}
/**
* This method is invokes when the repaint
method
* is called.
*/
public void update(Graphics g) {
System.out.println("ImageButton:update - gr is " + ((g==null)?"null":"OK") );
// g.drawImage(image, 0, 0, null);
paint( g );
}
/**
* Method to draw image on the button
*/
public void paint(Graphics g) {
System.out.println("ImageButton:paint - gr is " + ((g==null)?"null":"OK") );
if( image == null ) {
System.out.println("ImageButton:paint - null image [" + name + "]");
}
else
g.drawImage(image, 0, 0, null);
}
}