KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > client > beans > ImageButton


1 /*
2  * ImageButton.java
3  *
4  * Created on May 7, 2002, 1:32 AM
5  */

6
7 package com.quikj.client.beans;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.util.*;
12
13 /**
14  *
15  * @author amit
16  */

17 public class ImageButton extends java.awt.Canvas JavaDoc
18 {
19     private Image image = null;
20     private String JavaDoc label = null;
21     private Vector actionListeners = new Vector();
22     private Color backgroundColor = Color.white;
23     private boolean raised = true;
24     
25     /** Creates a new instance of ImageButton */
26     public ImageButton()
27     {
28         super();
29         addMouseListener(new ImageButton.ButtonClickListener());
30     }
31     
32     public void paint(java.awt.Graphics JavaDoc 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             // else
53
// {
54
// invalidate();
55
// }
56
}
57     }
58     
59     /** Getter for property image.
60      * @return Value of property image.
61      */

62     public java.awt.Image JavaDoc getImage()
63     {
64         return image;
65     }
66     
67     /** Setter for property image.
68      * @param image New value of property image.
69      */

70     public void setImage(java.awt.Image JavaDoc image)
71     {
72         this.image = image;
73         repaint();
74     }
75     
76     /** Getter for property label.
77      * @return Value of property label.
78      */

79     public java.lang.String JavaDoc getLabel()
80     {
81         return label;
82     }
83     
84     /** Setter for property label.
85      * @param label New value of property label.
86      */

87     public void setLabel(java.lang.String JavaDoc 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     /** Getter for property backgroundColor.
116      * @return Value of property backgroundColor.
117      *
118      */

119     public java.awt.Color JavaDoc getBackgroundColor()
120     {
121         return backgroundColor;
122     }
123     
124     /** Setter for property backgroundColor.
125      * @param backgroundColor New value of property backgroundColor.
126      *
127      */

128     public void setBackgroundColor(java.awt.Color JavaDoc backgroundColor)
129     {
130         this.backgroundColor = backgroundColor;
131     }
132     
133     class ButtonClickListener extends MouseAdapter
134     {
135         public void mousePressed(java.awt.event.MouseEvent JavaDoc mouseEvent)
136         {
137             // if (mouseEvent.getModifiers() == InputEvent.BUTTON1_MASK)
138
// {
139
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             // }
150
}
151         
152     }
153 }
154
Popular Tags