Have an ImageButtonTag(s): AWT
This implementation of an ImageButton requires JDK1.1 and is a good example of the new Event architecture. This ImageButton needs 2 GIF images representing the normal and pressed state (


[ImageButton.java]
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.net.*; public class ImageButton extends Canvas { protected ActionListener actionListener = null; int w,h; boolean clicked; boolean down; boolean enabled; Image UPimage; Image DOWNimage; Image disabledimage; public ImageButton(URL up_b, URL down_b) { clicked=false; down=false; enabled=true; InitImage(up_b,down_b); setSize(w,h); addMouseListener(new ImageButtonMouseListener()); addMouseMotionListener(new ImageButtonMouseMotionListener()); } public void InitImage(URL up, URL down) { MediaTracker tracker; try { UPimage = getToolkit().getImage(up); DOWNimage = getToolkit().getImage(down); tracker = new MediaTracker(this); tracker.addImage(UPimage,0); tracker.addImage(DOWNimage,1); tracker.waitForAll(); } catch (InterruptedException e) { e.printStackTrace(); } disabledimage=createImage(new FilteredImageSource (UPimage.getSource(),new ImageButtonDisableFilter())); w=UPimage.getWidth(this); h=UPimage.getHeight(this); } public void paint(Graphics g) { if (down) { g.drawImage(DOWNimage,0,0,this); } else { if (enabled) { g.drawImage(UPimage,0,0,this); } else { g.drawImage(disabledimage,0,0,this); } } } public void setEnabled(boolean b) { enabled=b; repaint(); } public boolean isEnabled() { return (enabled); } public void addActionListener(ActionListener l) { actionListener = AWTEventMulticaster.add(actionListener,l); } public void removeActionListener(ActionListener l) { actionListener = AWTEventMulticaster.remove(actionListener, l); } public class ImageButtonMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { Point p = e.getPoint(); if ((p.x < w)&&(p.y < h)&&(p.x > 0)&&(p.y > 0)&&(enabled==true)) { clicked=true; down=true; repaint(); } } public void mouseReleased(MouseEvent e) { Point p = e.getPoint(); if (down) { down=false; repaint(); } if ((p.x < w)&&(p.y < h)&&(p.x > 0)&&(p.y > 0)&&(clicked==true)) { ActionEvent ae = new ActionEvent(e.getComponent(),0,"click"); if (actionListener != null) { actionListener.actionPerformed(ae); } } clicked=false; } } public class ImageButtonMouseMotionListener extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { Point p = e.getPoint(); if ((p.x < w)&&(p.y < h)&&(p.x > 0)&&(p.y > 0)&&(clicked==true)) { if (down==false) { down=true; repaint(); } } else { if (down==true) { down=false; repaint(); } } } } public Dimension getPreferredSize() { return (new Dimension(UPimage.getWidth(this), UPimage.getHeight(this))); } public Dimension getMinimumSize() { return getPreferredSize(); } class ImageButtonDisableFilter extends RGBImageFilter { public ImageButtonDisableFilter() { canFilterIndexColorModel=true; } public int filterRGB(int x, int y, int rgb) { return (rgb & ~0xff000000) | 0x80000000; } } }
import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; public class TestImageButton extends Applet implements ActionListener,ItemListener { ImageButton ib; Checkbox c; public void init() { setLayout(new FlowLayout()); try { ib = new ImageButton (new URL(getCodeBase(), "Gumby.gif"), new URL(getCodeBase(), "Gumbyblu.gif")); c = new Checkbox("disable"); ib.addActionListener(this); c.addItemListener(this); add(ib); add(c); } catch (Exception e) { e.printStackTrace(); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == ib) System.out.println("Click ImageButton"); } public void itemStateChanged(ItemEvent ie) { ib.setEnabled(!ib.isEnabled()); } }
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com