import java.awt.*; import java.awt.event.*; //class to make an animated button using images. //Written by Mark Bernard //mark.bernard@home.com class ImageButton extends Button implements MouseListener { Image i[]; int select=1; int w=0; int h=0; int iw,ih; //The constructor requires 4 images as described below. // 1. Greyed out image of the button // 2. Normal/unselected image // 3. Hover image(if mouse is hovering over the button // 4. Pressed image //Please note that the image will always take up the entire //display of the button. If layout managers are used //the image will be stretched to fit the area layed out. public ImageButton(Image im[]) { super(" "); i=new Image[4]; i=im; iw=i[0].getWidth(this); ih=i[0].getHeight(this); setSize(iw,ih); addMouseListener(this); } public Dimension getPreferredSize() { return new Dimension(iw,ih); } public Dimension getMinimumSize() { return new Dimension(iw,ih); } public void setBounds(int x,int y,int width, int height) { w=width; h=height; super.setBounds(x,y,width,height); } public void setSize(int width,int height) { w=width; h=height; super.setSize(width,height); } public void setEnabled(boolean e) { if(e) { select=1; } else { select=0; } repaint(); super.setEnabled(e); } public void paint(Graphics g) { g.drawImage(i[select],0,0,w,h,this); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { if(select!=0) { select=2; repaint(); } } public void mouseExited(MouseEvent e) { if(select!=0) { select=1; repaint(); } } public void mousePressed(MouseEvent e) { if(select!=0) { select=3; repaint(); } } public void mouseReleased(MouseEvent e) { if(select!=0) { select=2; repaint(); } } }