1 6 7 package com.quikj.client.beans; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.util.*; 12 13 17 public class ImageButton extends java.awt.Canvas 18 { 19 private Image image = null; 20 private String label = null; 21 private Vector actionListeners = new Vector(); 22 private Color backgroundColor = Color.white; 23 private boolean raised = true; 24 25 26 public ImageButton() 27 { 28 super(); 29 addMouseListener(new ImageButton.ButtonClickListener()); 30 } 31 32 public void paint(java.awt.Graphics graphics) 33 { 34 Rectangle r = getBounds(); 35 graphics.setColor(backgroundColor); 36 graphics.fill3DRect(0, 0, r.width-1, r.height-1, raised); 37 38 if (image != null) 39 { 40 graphics.drawImage(image, 41 0, 0, this); 42 43 int height = image.getHeight(this); 44 if (height != -1) 45 { 46 if (label != null) 47 { 48 graphics.setColor(Color.black); 49 graphics.drawString(label, 0, height); 50 } 51 } 52 } 57 } 58 59 62 public java.awt.Image getImage() 63 { 64 return image; 65 } 66 67 70 public void setImage(java.awt.Image image) 71 { 72 this.image = image; 73 repaint(); 74 } 75 76 79 public java.lang.String getLabel() 80 { 81 return label; 82 } 83 84 87 public void setLabel(java.lang.String label) 88 { 89 this.label = label; 90 repaint(); 91 } 92 93 public void addActionListener(ActionListener l) 94 { 95 synchronized (actionListeners) 96 { 97 actionListeners.addElement(l); 98 setCursor(new Cursor(Cursor.HAND_CURSOR)); 99 100 } 101 } 102 103 public void removeActionListener(ActionListener l) 104 { 105 synchronized (actionListeners) 106 { 107 actionListeners.removeElement(l); 108 if (actionListeners.size() == 0) 109 { 110 setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 111 } 112 } 113 } 114 115 119 public java.awt.Color getBackgroundColor() 120 { 121 return backgroundColor; 122 } 123 124 128 public void setBackgroundColor(java.awt.Color backgroundColor) 129 { 130 this.backgroundColor = backgroundColor; 131 } 132 133 class ButtonClickListener extends MouseAdapter 134 { 135 public void mousePressed(java.awt.event.MouseEvent mouseEvent) 136 { 137 synchronized (actionListeners) 140 { 141 int size = actionListeners.size(); 142 143 for (int i = 0; i < size; i++) 144 { 145 ActionListener l = (ActionListener)actionListeners.elementAt(i); 146 l.actionPerformed(null); 147 } 148 } 149 } 151 152 } 153 } 154 | Popular Tags |