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;; aGame show; public ImageButton(Image image, String actionCommand, aGame t, String h){ hint=h; button(image,image, actionCommand, t); } public ImageButton(Image image, String actionCommand, aGame t){ button(image,image, actionCommand, t); } public ImageButton(Image image, Image image_big, String actionCommand, aGame t) { button(image,image_big,actionCommand, t); } public void button(Image image, Image image_big, String actionCommand, aGame t){ show=t; name = base + nameCounter++; this.actionCommand = actionCommand; this.image = image; if(this.image != null) System.out.println( "image is not null!"); else this.image = image; if(image_big != null) this.image_big=image_big; else this.image_big=image; dimage=image; oldimage=image; addMouseListener(this); setVisible(false); } public ImageButton(Image image){ this(image, null, null); } public void setImage(Image im){ try{ image=im; dimage=im; bg=null; repaint(); }catch(Exception e){} } 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){ if(show.global.State == show.global.DRAW){ Dimension d = getSize(); if (bg == null){ bg = createImage(d.width, d.height); } if(bg!=null){ Graphics gg = bg.getGraphics(); super.paint(gg); if(gg !=null && image != null){ gg.drawImage(image, 3, 4, this); } if(visible) 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!=null){ Image i=createImage(100,100); Graphics g=i.getGraphics(); g.setColor(Color.white); Font textfont=new Font("Terminal", Font.PLAIN, 8); g.setFont(textfont); g.drawImage(image,0,0,this); g.drawString(hint, 3,10); image=i; repaint(); }else{ System.out.println("Hint or image not available!"); } } 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){ } // Animated Gif public boolean imageUpdate( Image img, int flags, int x, int y, int w, int h ){ repaint(); return true; } }