// Processed by NMI's Java Code Viewer 4.8.2 © 1997-2000 B. Lemaire // Website: http://njcv.htmlplanet.com E-mail: info@njcv.htmlplanet.com // Copy registered to Evaluation Copy // Source File Name: ImageButton.java import java.awt.*; class ImageButton extends Canvas { private Image button; private Image enabledButton; private Image disabledButton; private Dimension buttonDim; private Color c; public boolean isButtonRaised; private boolean buttonDisabled; private boolean isMouseOnButton; private Timer timer; private Component target; public ImageButton() { buttonDim = new Dimension(); isButtonRaised = true; buttonDisabled = false; isMouseOnButton = false; timer = new Timer(this, 500, false, 1001); } public ImageButton(Image image, Image image1, int i, int j, Color color, Component component) { buttonDim = new Dimension(); isButtonRaised = true; buttonDisabled = false; isMouseOnButton = false; timer = new Timer(this, 500, false, 1001); target = component; button = image; enabledButton = image; disabledButton = image1; buttonDim.width = button.getWidth(this); buttonDim.height = button.getHeight(this); c = color; resize(buttonDim.width + 3, buttonDim.height + 3); move(i, j); } public void paint(Graphics g) { update(g); } public void update(Graphics g) { g.setColor(c); g.fillRect(0, 0, buttonDim.width + 4, buttonDim.height + 4); if(isButtonRaised) { g.draw3DRect(0, 0, buttonDim.width + 1, buttonDim.height + 1, true); g.draw3DRect(0, 0, buttonDim.width + 2, buttonDim.height + 2, true); g.drawImage(button, 1, 1, this); return; } else { g.draw3DRect(1, 1, buttonDim.width + 1, buttonDim.height + 1, false); g.drawImage(button, 2, 2, this); return; } } public void disableButton() { button = disabledButton; repaint(); buttonDisabled = true; } public void enableButton() { button = enabledButton; repaint(); buttonDisabled = false; } private synchronized boolean handle_mouseEnter() { isMouseOnButton = true; timer.start(); return true; } private synchronized boolean handle_mouseExit() { isMouseOnButton = false; timer.stop(); target.handleEvent(new Event(this, 505, null)); return true; } private synchronized boolean handle_mouseDown() { timer.stop(); target.handleEvent(new Event(this, 505, null)); if(!buttonDisabled) { isButtonRaised = false; repaint(); } return true; } private synchronized boolean handle_mouseUp() { if(!buttonDisabled) { isButtonRaised = true; repaint(); target.handleEvent(new Event(this, 1001, null)); } return true; } public boolean handleEvent(Event event) { if(event.target == this && event.id == 501) return handle_mouseDown(); if(event.target == this && event.id == 502) return handle_mouseUp(); if(event.target == this && event.id == 504) return handle_mouseEnter(); if(event.target == this && event.id == 505) return handle_mouseExit(); if(event.target == timer && event.id == 1001 && isMouseOnButton) { target.handleEvent(new Event(this, 504, null)); return true; } else { return false; } } }