Building the Close Box Button
File:
CloseBoxButton.java
Contents
Overview
The CloseBoxButton is a special class that is derived from RolloverButton.
Its function mimics a standard close box such as the one that you would find in the upper left-hand corner of a window. The CloseBox is used by the Controller class.
The image on the right shows the two images used by this button. Note that there is only an up state and a down state. There is no rollover state. This class implements a single method, initImages( ), which is declared as abstract in RolloverButton.
Steps to Follow
This class does not import any packages. It uses only the default package java.lang.* and classes in its default package. This class is derived from RolloverButton, which we examined earlier.
public class CloseBoxButton extends RolloverButton
{
protected void initImages( )
{
|
//Initialize images for the CloseBoxButton
//Insert "CloseBoxButton initImages"
|
We have only a single method that was defined as an abstract method in RolloverButton.
This method specifies the images to be used for this button. Locate the CloseBoxButton initImages clipping in the CloseBoxButton folder and drag it directly below the last line of code shown above. Your code should now look like this:
public class CloseBoxButton extends RolloverButton
{
protected void initImages( )
{
//Initialize images for the CloseBoxButton
//Insert "CloseBoxButton initImages"
|
addImage("images/closebox.jpg", upImage);
addImage("images/closeboxa.jpg", downImage);
|
}
}
|
Note that we only specify an up image and a down image. We do not specify a rollover image. Is this going to cause a problem? No, because the RolloverButton
code we inherit is smart. When handleRollover( ) is called, it will try to call setImage(rolloverImage). There will be no entry in the hashTable for the rolloverImage, so it will return null. Our code in ImageButton.setImage( ) knows not to change the current image if it is null. Thus, the button will not change its visual appearance if a rollover state occurs because there is no rollover image.
Back to top
The close box button is a very simple class. Like its fellow RolloverButton-derived classes, it overrides initImages( ) but it specifies only two images. This is because we only use an up state and a down state and no intermediary states.
Now lets look at the next class, Controller, by returning
to the main tutorial file here.
|