|
|
| ★ wanayoo — archive 1999 http://developer.apple.com/java/javatutorial/imagebutton2.html | Nouvelle recherche | Portail wanayoo |
|
Step 3 - Registering the Action Listener Now that we have methods that can respond to mouse events, we need to register our listener with the |
public ImageButton( )
{
|
//REGISTER_LISTENERS
//Insert "ImageButton register listener
|
|
Locate the ImageButton register listener clipping in the ImageButton folder and drag it directly below the last line of code shown above. Your code should now look like this: |
public ImageButton( )
{
//REGISTER_LISTENERS
//Insert "ImageButton register listener
|
Mouse aMouse = new Mouse( );
this.addMouseListener(aMouse);
|
|
First, we create a new instance of our To complete our constructor, we have some additional initialization to perform: |
Mouse aMouse = new Mouse( );
this.addMouseListener(aMouse);
|
//Initialize state information
//Insert "ImageButton init state"
|
|
Locate the ImageButton init state clipping in the ImageButton folder and drag it directly below the last line of code shown above. Your code should now look like this: |
Mouse aMouse = new Mouse( );
this.addMouseListener(aMouse);
|
imageHash = new Hashtable( );
actionCommand = "ImageButton Action";
|
|
We allocate a new hashtable to contain the button images, and then we initialize our action command string. The action command string will allow objects which receive the action event from our button to determine the source of the message. Back to top Step 4 - Handling MouseReleased Messages We have defined our inner class that handles mouse events and registers that class as a |
/**
* Gets called when the mouse button is pressed on this button.
* @param isMouseInside, if true, the mouse is located inside
* the button area, if false the mouse is outside the button
* area.
*/
protected void handleMouseRelease(Boolean isMouseInside)
{
|
//Handle firing an ActionEvent to our listeners if the
//mouse was released inside the button.
//Insert "ImageButton handleMouseReleased"
|
|
As you can see from the JavaDoc, the Locate the ImageButton handleMouseReleased clipping in the ImageButton folder and drag it directly below the last line of code shown above. Your code should now look like this: |
/**
* Gets called when the mouse button is pressed on this button.
* @param isMouseInside, if true, the mouse is located inside
* the button area, if false the mouse is outside the button
* area.
*/
protected void handleMouseRelease(Boolean isMouseInside)
{
//Handle firing an ActionEvent to our listeners if the
//mouse was released inside the button.
//Insert "ImageButton handleMouseReleased"
|
if (isMouseInside)
fireActionEvent( );
|
} |
|
We check to see if the mouse was still inside the button when it was released. The Boolean Back to top Step 5 - Implementing addImage( ) Skipping down past the abstract declarations of |
/**
* Adds an image to the button.
* @param imagePath, the location of the image resource to use.
* This path is relative to the location of this class file.
* @param imageName, the name used to identify the image for
* later use in this button.
* @see #removeImage
*/
public void addImage(String imagePath, String imageName)
{
|
//Handle storing the information in our internal data
//structure.
//Insert "ImageButton addImage"
|
|
Locate the ImageButton addImage clipping in the ImageButton folder and drag it directly below the last line of code shown above. Your code should now look like this: |
/**
* Adds an image to the button.
* @param imagePath, the location of the image resource to use.
* This path is relative to the location of this class file.
* @param imageName, the name used to identify the image for
* later use in this button.
* @see #removeImage
*/
public void addImage(String imagePath, String imageName)
{
//Handle storing the information in our internal data
//structure.
//Insert "ImageButton addImage"
|
if (imageName != null && !imageName.equals(""))
{
Image newImage = Misc.loadImage(imagePath, this, true);
if (newImage != null)
{
imageHash.put(imageName, newImage);
}
}
|
} |
|
This method checks the If the image was loaded successfully (i.e., the image loaded is not null), we add the item to our hashtable, using the image name as the key and the image as the data. What is a hashtable? A hashtable is a data structure that allows you to store data in several storage slots retrievable by a key. The key is used to determine which slot the item is stored in. It is a very fast and efficient storage mechanism which is built-in to java. Now that we have a mechanism for adding images to our pool of button images, we need to be able to remove them. Back to top Step 6 - Implementing removeImage( ) The |
/**
* Removes an image from the button
* @param imageName, the identifying name of the image to remove.
* @see #addImage
*/
public void removeImage(String imageName)
{
|
//Handle removing the image from our internal data
//structure.
//Insert "ImageButton removeImage"
|
|
This method only takes a string as a parameter. It takes the Locate the ImageButton removeImage clipping in the ImageButton folder and drag it directly below the last line of code shown above. Your code should now look like this: |
/**
* Removes an image from the button
* @param imageName, the identifying name of the image to remove.
* @see #addImage
*/
public void removeImage(String imageName)
{
//Handle removing the image from our internal data
//structure.
//Insert "ImageButton removeImage"
|
if (imageName != null && !imageName.equals(""))
{
imageHash.remove(imageName);
}
|
} |
|
The body of this method is fairly simple. We check to see if the name passed to the function is non-empty and non-null, and then call Back to top Step 7 - Implementing setImage( ) The routine |
/**
* Sets the image for the button to use as its current image.
* @param imageName, the identifying name of the image to use.
*/
public void setImage(String imageName)
{
|
//Handle locating the image in our internal data structure,
//setting it as the current image, and repainting the
//button.
//Insert "ImageButton setImage"
|
|
Locate the ImageButton setImage clipping in the ImageButton folder and drag it directly below the last line of code shown above. Your code should now look like this: |
//**
* Sets the image for the button to use as its current image.
* @param imageName, the identifying name of the image to use.
*/
public void setImage(String imageName)
{
//Handle locating the image in our internal data structure,
//setting it as the current image, and repainting the
//button.
//Insert "ImageButton setImage"
|
if (imageName != null && !imageName.equals(""))
{
Image temp = (Image)imageHash.get(imageName);
if (temp != null)
{
image = temp;
this.imageName = imageName;
repaint( );
}
}
|
} |
|
image = (Image)imageHash.get(imageName); and then check to see if image is null? Well then if the image we were loading did not exist, we would have no idea what the image variable previously contained, and our current image would be null. This would be a bad idea. So we retrieve the image into a temporary variable, and then if it is valid, set the current image variable to the temporary. Then we store the image name: this.imageName = imageName; Whats up with the Last but not least, we call Back to top Step 8 - Implementing getImage( ) This method quite simply returns the name of the current image. |
/**
* Gets the name of the image currently in use.
* @return The identifying name of the image being used.
*/
public String getImage( )
{
|
//Return the current image name.
//Insert "ImageButton getImage"
|
|
Locate the ImageButton getImage clipping in the ImageButton folder and drag it directly below the last line of code shown above. Your code should now look like this: |
/**
* Gets the name of the image currently in use.
* @return The identifying name of the image being used.
*/
public String getImage( )
{
//Return the current image name.
//Insert "ImageButton getImage"
|
return imageName; |
} |
|
It really doesnt get much easier than this. We simply return our current image name stored in the image button data member Back to top Step 9 - Implementing getImageObject( ) This method returns the actual image object associated with the current button image, not just the name. |
/**
* Gets the actual Image Object which is currently being used.
* @return The java.awt.Image currently in use.
*/
public Image getImageObject( )
{
|
//Return the current image object.
//Insert "ImageButton getImageObject"
|
|
Locate the ImageButton getImageObject clipping in the ImageButton folder and drag it directly below the last line of code shown above. Your code should now look like this: |
/**
* Gets the actual Image Object which is currently being used.
* @return The java.awt.Image currently in use.
*/
public Image getImageObject( )
{
//Return the current image object.
//Insert "ImageButton getImageObject"
|
return image; |
} |
|
This should come as no surprise. We simply return our current image stored in our image data member of Back to top Step 10 - Handling Action Events As we recall from Step 2 and Step 3, there is a very specific chain of events that occur when the user clicks on the button. The first thing that happens is our |
public Image getImageObject( )
{
//Return the current image object.
//Insert "ImageButton getImageObject"
return image;
}
|
//Routines for handling ActionListener management. //Insert "ImageButton Action Management" |
|
Lets look at the mechanism for action management. Locate the ImageButton Action Management clipping in the ImageButton folder and drag it directly below the last line of code shown above. Your code should now look like this: |
//Routines for handling ActionListener management. //Insert "ImageButton Action Management" |
/**
* Sets the command name of the action event fired by this
* button.
* @param command The name of the action event command fired
* by this button
*/
public void setActionCommand(String command)
{
actionCommand = command;
}
/**
* Returns the command name of the action event fired by this
* button.
* @return the action command name
*/
public String getActionCommand( )
{
return actionCommand;
}
/**
* Adds the specified action listener to receive action events
* from this button.
* @param l the action listener
*/
public void addActionListener(ActionListener l)
{
actionListener = AWTEventMulticaster.add(actionListener, l);
}
/**
* Removes the specified action listener so it no longer receives
* action events from this button.
* @param l the action listener
*/
public void removeActionListener(ActionListener l)
{
actionListener = AWTEventMulticaster.remove(
actionListener, l);
}
/**
* Fire an action event to the listeners.
*/
protected void fireActionEvent( )
{
if (actionListener != null)
actionListener.actionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, actionCommand));
}
|
|
These methods encapsulate a mechanism for broadcasting notification that our button was pressed. This notification takes place in the form of an action event. Lets look at these functions one at a time. |
public void setActionCommand(String command)
{
actionCommand = command;
}
|
When an |
public String getActionCommand( )
{
return actionCommand;
}
|
This routine retrieves the current action command by returning the contents of our |
public void addActionListener(ActionListener l)
{
actionListener = AWTEventMulticaster.add(actionListener, l);
}
|
This routine allows |
public void removeActionListener(ActionListener l)
{
actionListener = AWTEventMulticaster.remove( actionListener, l);
}
|
This allows previously interested Listeners to tell the button they no longer need to be notified when an |
protected void fireActionEvent( )
{
if (actionListener != null)
actionListener.actionPerformed(new
ActionEvent(this,
ActionEvent.ACTION_PERFORMED, actionCommand));
}
|
This calls the Now its time to implement Back to top Step 11 - Implementing getPreferredSize( ) Because our button selects images from an image pool, we dont know at design time how big to make the button. Thus, we implement a |
/**
* Returns the preferred size of this component.
* @see #getMinimumSize
* @see LayoutManager
*/
public Dimension getPreferredSize( )
{
|
//If the current image is not null, then return the size of
//the image.
//If it is null, defer to the super class.
//Insert "ImageButton getPreferredSize"
|
|
We are overriding the |
/**
* Returns the preferred size of this component.
* @see #getMinimumSize
* @see LayoutManager
*/
public Dimension getPreferredSize( )
{
//If the current image is not null, then return the size of
//the image. If it is null, defer to the super class.
//Insert "ImageButton getPreferredSize"
|
if (image != null)
return new Dimension(image.getWidth(this),
image.getHeight(this));
return super.getPreferredSize( );
|
} |
/**
* Paints the component. This method is called when the contents
* of the component should be painted in response to the
* component first being shown or damage needing repair. The
* clip rectangle in the Graphics parameter will be set to the
* area which needs to be painted.
* @param g the specified Graphics window
* @see #update
*/
public void paint(Graphics g)
{
|
//Let the super class draw, then handle drawing the current
//image.
//Insert "ImageButton paint"
|
|
As you can see from the JavaDoc, the |
/**
* Paints the component. This method is called when the contents
* of the component should be painted in response to the
* component first being shown or damage needing repair. The
* clip rectangle in the Graphics parameter will be set to the
* area which needs to be painted.
* @param g the specified Graphics window
* @see #update
*/
public void paint(Graphics g)
{
//Let the super class draw, then handle drawing the current
//image.
//Insert "ImageButton paint"
|
super.paint(g);
if (image != null)
g.drawImage(image, 0, 0, this);
|
} |
|
First, we call the Summary In review, we set up our class to be derived from That may seem like a lot of work, but a lot of it is to simplify the creation of our derived classes which for the most part are much more simple than this class. We have implemented the core functionality for our button, and the road is now much easier from here. Now we are ready to go back to our main tutorial file and prepare for the next step, Building the Rollover button. |