Java Reference
In-Depth Information
public void removeActionListener(ActionListener listener) {
listeners.removeElement (listener);
}
public void paint (Graphics g) {
Dimension dim = getSize();
int w = image.getWidth (this);
int h = image.getHeight (this);
g.drawImage (image,
(dim.width - w) / 2,
(dim.height - h) / 2,
this);
Color fg = g.getColor();
g.setColor (fg);
g.drawRoundRect (0, 0, dim.width-1, dim.height-1, 4, 4);
}
public Dimension getMinimumSize() {
return new Dimension (image.getWidth (null),
image.getHeight (null));
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public void processMouseEvent(MouseEvent e) {
if (e.getID() == MouseEvent.MOUSE_CLICKED) {
ActionEvent ae = new ActionEvent
(this, ActionEvent.ACTION_PERFORMED, command);
for (int i = 0; i < listeners.size(); i++)
((ActionListener) listeners.elementAt (i))
.actionPerformed (ae);
}
super.processMouseEvent(e);
}
}
For testing purposes, we provide a small application shown in Listing 4.9 to show how the
ImageButton can be integrated into a MIDlet. The application uses an ImageButton that is
registered to an ActionListener of the Frame . When the ImageButton is clicked, we
increment a clickCount variable and display its value in the frame title.
Listing 4.9 ImageTest.java
import java.awt.*;
import java.awt.event.*;
import javax.microedition.midlet.*;
public class ImageTest extends MIDlet implements ActionListener {
 
 
Search WWH ::




Custom Search