import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.util.*; //public class ImageButton extends Canvas implements FocusListener, MouseListener, MouseMotionListener, ComponentListener, KeyListener public class ImageButton extends Canvas implements MouseListener { private static final String base = "imagebutton"; private static int nameCounter = 0; private String name; private Image image; Image oldimage; private Image image_big; private Image dimage; private Image bg; private transient ActionListener actionListener; private String actionCommand; private String hint; private Dimension dim; private boolean visible=false;; Show show; public ImageButton(Image image, Image big,String actionCommand, Show t, String h){ image_big=big; hint=h; button(image,big, actionCommand, t); } public ImageButton(Image image, String actionCommand, Show t, String h){ hint=h; button(image,image, actionCommand, t); } public ImageButton(Image image, String actionCommand, Show t){ button(image,image, actionCommand, t); } /* public ImageButton(Image image, Image image_big, String actionCommand, Show t) { button(image,image_big,actionCommand, t); } */ public void button(Image image, Image image_big, String actionCommand, Show t){ show=t; name = base + nameCounter++; this.actionCommand = actionCommand; this.image = image; /* if(image_big != null) this.image_big=image_big; else this.image_big=image; */ dimage=image; oldimage=image; //addComponentListener(this); addMouseListener(this); // addMouseMotionListener(this); // addFocusListener(this); // addKeyListener(this); setVisible(false); } public ImageButton(Image image) { this(image, null, null); } public void setImage(Image im){ try{ image=im; dimage=im; oldimage=im; bg=null; repaint(); //System.out.println("Setting image ImageButton: "+im.toString()); }catch(Exception e){System.out.println("Failed setImage !");} } public void setHintImage(Image im){ // bg=im; image_big=im; repaint(); } public String getName() { return name; } public synchronized void addActionListener(ActionListener l) { if(show!=null) show.hidden.requestFocus(); actionListener = AWTEventMulticaster.add(actionListener, l); } public synchronized void removeActionListener(ActionListener l) { show.hidden.requestFocus(); actionListener = AWTEventMulticaster.remove(actionListener, l); } protected void sendActionEvent() { show.hidden.requestFocus(); processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getActionCommand())); } protected void processEvent(AWTEvent e) { show.hidden.requestFocus(); if (e instanceof ActionEvent) { processActionEvent((ActionEvent)e); return; } super.processEvent(e); } protected void processActionEvent(ActionEvent e) { show.hidden.requestFocus(); if (actionListener != null) { actionListener.actionPerformed(e); } } public void setVisible(boolean o){ visible=o; super.setVisible(o); repaint(); } public void setActionCommand(String command) { actionCommand = command; } public String getActionCommand() { return (actionCommand == null ? name : actionCommand); } public boolean isFocusTraversable() { return true; } public void paint(Graphics g) { Dimension d = getSize(); if (bg == null) { bg = createImage(d.width, d.height); } if(bg!=null){ try{ Graphics gg = bg.getGraphics(); super.paint(gg); if(image!=null && gg!=null){ gg.drawImage(image, 3, 4, this); } }catch(Exception egeg){} if(visible){ if(g !=null) g.drawImage(bg, 0, 0, this); } } } //public void mouseDragged(MouseEvent e){} // public void mouseMoved(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) { if(image_big!=null){ //System.out.println("image_big != null"); Rectangle d=getBounds(); Image i=createImage(d.width,d.height); //System.out.println("Draw ("+actionCommand+")"); Graphics g=i.getGraphics(); // g.drawImage(image,0,0,this); g.drawImage(image_big, 0,0,this); image=i; repaint(); }else{ //System.out.println("Hint not available!"); //System.out.println("image_big == null"); } } public void mouseExited(MouseEvent e) { if(oldimage!=null) image=oldimage; repaint(); } public void mousePressed(MouseEvent e) { show.hidden.requestFocus(); processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getActionCommand())); show.hidden.requestFocus(); } public void mouseReleased(MouseEvent e) { } }