Thursday, 14 May 2015

Extending the AWT in Java

ou can add more elements to your interface toolbox in two ways: you can extend existing components yourself or you can use someone else's classes.

Extending Components (the Image Button)

One of the nice features of object-oriented programming is that you can extend existing classes and add new functionality with minimal effort. The applet in Listing 17.43 contains the code for a class called image_button_component, which behaves like a button but has an image for its interface rather than the regular button picture.

Listing 17.43. The image_button_component class.
import java.applet.Applet;

import java.awt.*;


public class image_button extends Applet {
    MediaTracker the_tracker;
    image_button_component  the_button; 

    public void init()
    {
        Image the_picture, inverse_picture;
        //load the two images, one for the regular 
        //button image and one for when the
        //mouse button is held down on the button
        the_tracker = new MediaTracker(this);
        the_picture = getImage(getDocumentBase(),"test.gif");
        the_tracker.addImage(the_picture,0); 
        inverse_picture = getImage(getDocumentBase(),"invert.gif");
        the_tracker.addImage(inverse_picture,0); 
        //Wait until both images are fully loaded.
        try {
            the_tracker.waitForID(0); 
        } catch(InterruptedException e) {}
        the_button = new image_button_component(the_picture,inverse_picture);
        add(the_button); 
    }
    public boolean mouseDown(Event e, int x, int y) {
        if (e.target instanceof image_button_component) {
            System.out.println("image_button_component clicked");
        }
        return true;
    }
}

class image_button_component extends Canvas {
    Image my_image, inverse_image;
    boolean mousedown_flag;

    public image_button_component(Image an_image,Image i_image) {
        my_image = an_image; 
        inverse_image = i_image;
        //the button starts unclicked
        mousedown_flag = false;
        //make sure the canvas image is drawn
        repaint();

    }
    //override these methods to tell the layout manager 
    //the size of the button which is defined by the
    //size of the image. This assumes that the two images are the same size.
    public Dimension minimumSize() {
        return new Dimension(my_image.getWidth(null),my_image.getHeight(null)); 
    }
    public Dimension preferredSize() {
        return new Dimension(my_image.getWidth(null),my_image.getHeight(null)); 
    }
    //make sure the button image is changed when the mouse is clicked
    public boolean mouseDown(Event e, int x, int y) {
        mousedown_flag = true;
        repaint();
        //return false so the event is passed on so the mouseDown 
        //method in the applet can use it
        return false; 
    }
    public boolean mouseUp(Event e, int x, int y) {
        mousedown_flag = false;
        repaint();
        //return true because the applet doesn't need this 
        //event but you might want to
        //return false to make this more button like
        return true;
    }
    public void paint(Graphics g) {
        //determine which image to draw based on the mouse state
        if (!mousedown_flag) {
            g.drawImage(my_image,0,0,null); 
        } else {
            g.drawImage(inverse_image,0,0,null); 
        }
    }

}


The applet loads in two images-one for the regular button and one for when it's clicked-and then creates a new instance of the image_button_component class.

No comments:

Post a Comment