home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / qtstreamingapplet.win / src / iconbutton.java next >
Encoding:
Java Source  |  2000-09-28  |  4.3 KB  |  142 lines

  1. /*
  2.  * quicktime.app: Sample Code for Initial Seeding
  3.  *
  4.  * © 1996, 97 Copyright, Apple Computer
  5.  * All rights reserved
  6.  */
  7.  
  8. import java.awt.*;
  9. import java.awt.image.*;
  10. import java.awt.event.*;
  11.  
  12. /* This class creates a animated button that listens to mouse events
  13.  * and changes the image of the button accordingly.
  14.  */
  15. public class IconButton extends Component implements MouseListener {
  16.     
  17.     /**
  18.      * Prepares an image for rendering on this component. The image data is
  19.      * downloaded asynchronously in another thread and the appropriate screen
  20.      * representation of the image is generated. 
  21.      * @param         iconPressed     the Pressed Button Icon
  22.      * @param         iconReleased     the Released Button Icon
  23.      */
  24.     public IconButton (Image iconPressed, Image iconReleased) {
  25.         this.iconPressed = iconPressed;
  26.         this.iconReleased = iconReleased;
  27.         prepareImage(iconPressed, this);
  28.         prepareImage(iconReleased, this);
  29.         
  30.         addMouseListener(this);
  31.     }
  32.     
  33.     private Image iconReleased, iconPressed;
  34.     transient private boolean buttonPressed = false;
  35.       transient private boolean drawPressed = false;
  36.       private boolean fireOnRelease = true;
  37.     transient ActionListener actionListener;
  38.     private String actionCommand = "";
  39.  
  40.     public synchronized void addActionListener (ActionListener l) {
  41.         actionListener = l;
  42.     }
  43.  
  44.     /**
  45.      * Removes the specified action listener so that it no longer 
  46.      * receives action events from this button. Action events occur  
  47.      * when a user presses or releases the mouse over this button.
  48.      * @param         l     the action listener.
  49.      * @see           java.awt.event.ActionListener
  50.      * @see           java.awt.Button#addActionListener
  51.      * @since         JDK1.1
  52.      */ 
  53.     public synchronized void removeActionListener(ActionListener l) {
  54.         actionListener = null;
  55.     }
  56.  
  57.     public void setFireActionOnRelease (boolean flag) {
  58.         fireOnRelease = flag;
  59.     }
  60.  
  61.     public boolean isFireActionOnRelease () {
  62.         return fireOnRelease;
  63.     }
  64.     
  65.     /**
  66.       * Depending upon the state of mouse button (pressed or released)
  67.       * the corresponding image is displayed. This gives the button a
  68.       * animated look and feel
  69.       */
  70.     public void paint(Graphics g) {
  71.         if (drawPressed)
  72.             g.drawImage(iconPressed, 0, 0, this);
  73.         else
  74.             g.drawImage(iconReleased, 0, 0, this);
  75.     }
  76.                           
  77.       public void mouseClicked(MouseEvent e) {}
  78.             
  79.       public void mouseEntered(MouseEvent e) {
  80.            if (buttonPressed) {
  81.                drawPressed = true;
  82.                repaint();
  83.            }
  84.      }
  85.       
  86.       public void mouseExited(MouseEvent e) {
  87.           if (buttonPressed) {
  88.               drawPressed = false;
  89.               repaint();
  90.           }
  91.       }
  92.       
  93.       /**
  94.         * This fires the action event which invokes the actionPerformed method on the 
  95.         * PopupMenu Button that displays the actual PopupMenu
  96.         */
  97.       public void mousePressed(MouseEvent e) {
  98.           buttonPressed = true;
  99.           drawPressed = true;
  100.           repaint();
  101.           if (actionListener != null && fireOnRelease == false)
  102.               actionListener.actionPerformed (new ActionEvent (this, ActionEvent.ACTION_PERFORMED, actionCommand, e.getModifiers()));
  103.       }
  104.       
  105.       public void mouseReleased(MouseEvent e) {
  106.           buttonPressed = false;
  107.           drawPressed = false;
  108.           repaint();
  109.           if (actionListener != null && fireOnRelease)
  110.               actionListener.actionPerformed (new ActionEvent (this, ActionEvent.ACTION_PERFORMED, actionCommand, e.getModifiers()));
  111.       }
  112.  
  113.    /**
  114.      * Sets the command name for the action event fired 
  115.      * by this button. By default this action command is 
  116.      * set to match the label of the button.
  117.      * @param     command  A string used to set the button's 
  118.      *                  action command.
  119.      * @see       java.awt.event.ActionEvent
  120.      * @since     JDK1.1
  121.      */
  122.     public void setActionCommand (String command) {
  123.         actionCommand = command;
  124.            if (actionCommand == null) 
  125.                actionCommand = "";
  126.     }
  127.  
  128.     /**
  129.      * Returns the command name of the action event fired by this button.
  130.      */
  131.     public String getActionCommand() {
  132.         return actionCommand;
  133.     }    
  134.     
  135.     /**
  136.      * This sets the size of the IconButton to be displayed.
  137.      */
  138.     public Dimension getPreferredSize () {
  139.         return new Dimension (iconReleased.getWidth(this), iconReleased.getHeight(this));
  140.     }
  141. }
  142.