home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / awt / imagebutton.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  6.5 KB  |  299 lines

  1. /*
  2.  * @(#)ImageButton.java  (c) 1996 Jan Kautz
  3.  */
  4.  
  5. package como.awt;
  6.  
  7. import como.util.*;
  8. import java.awt.*;
  9. import java.awt.image.*;
  10.  
  11. /**    
  12.  * ImageButton is a button which uses images to represent
  13.  * the states of the button - normal, pressed.
  14.  * They don't have to be fully loaded! They will be loaded
  15.  * asynchronously here!
  16.  */
  17.  
  18. public class ImageButton extends Canvas {
  19.     Image normal;
  20.     Image pressed;
  21.     Image current;
  22.     boolean down = false;
  23.     boolean focus = false;
  24.     boolean isswitch = false;
  25.     boolean nopush = false;
  26.     int width, height;
  27.     String text;
  28.  
  29.     /**
  30.      * Construct an image button. We have two states:
  31.      * Normal and pressed. They both must be non-null.
  32.      * Then you must specify the width and height of the button.
  33.      * (May be different from the â– icture's size and the picture
  34.      * will be loaded asynchronously!)
  35.      */
  36.     public ImageButton( Image normal, Image pressed, int w, int h ) {
  37.         width = w;
  38.     height = h;
  39.  
  40.     down = false;
  41.     
  42.     // set my current size to width/height
  43.     resize( width, height );
  44.     
  45.     setImages( normal, pressed );
  46.     }
  47.  
  48.     public ImageButton( int w, int h, String text ) {
  49.         width = w;
  50.     height = h;
  51.     down = false;
  52.  
  53.     this.normal = null;
  54.     this.pressed = null;
  55.     this.text = text;
  56.     this.current = null;
  57.  
  58.     drawIt();
  59.     }
  60.  
  61.     /**
  62.      * Here you may change the pictures used in the button.
  63.      */
  64.     public void setImages( Image normal, Image pressed ) {
  65.     this.normal = normal;
  66.     this.pressed = pressed;
  67.  
  68.     this.current = normal;
  69.     
  70.     // let's load and scale the image
  71.     if( normal != null )
  72.         prepareImage( normal, width, height, this );
  73.  
  74.     if( pressed != null )
  75.         prepareImage( pressed, width, height, this );
  76.  
  77.     drawIt();
  78.     }
  79.  
  80.     /**
  81.      * Make it a Switch instead of a Button. Now the user can press
  82.      * the switch and it's on or off then! You will get an action-message
  83.      * that the state changed. Get state with getState().
  84.      */
  85.     public void setSwitch( boolean sw ) {
  86.         isswitch = sw;
  87.     drawIt();
  88.     }
  89.  
  90.     /**
  91.      * Here you can set, if you want the button to be pushed
  92.      * down, if someone clicks or not. 
  93.      * (Usefull if you just want to display a picture!)
  94.      */
  95.     public void setNoPush( boolean np ) {
  96.         nopush = np;
  97.     drawIt();
  98.     }
  99.  
  100.     /**
  101.      * Set the current state of the switch!
  102.      * (If it is a button then do nothing!)
  103.      * @param onoff indicating on or off
  104.      */
  105.     public void setState( boolean onoff ) {
  106.         if( isswitch ) {
  107.         down = onoff;
  108.  
  109.         if( onoff == true )
  110.             current = pressed;
  111.         else
  112.             current = normal;
  113.  
  114.         drawIt();
  115.     }
  116.     }
  117.  
  118.     /**
  119.      * Get the current state of the switch! (if it's a button
  120.      * you will get true if it's currently pressed).
  121.      * @return if the switch is on then return true; else false.
  122.      */
  123.     public boolean getState() {
  124.         return down;
  125.     }
  126.  
  127.     /**
  128.      * Move the focus to the next component
  129.      * Does NOT work! (cause nextFocus() doesn't do what I wanted)
  130.      */
  131.     public boolean keyDown( Event evt, int key ) {
  132.         if( key == (int)'\t' || key == Event.DOWN || key == Event.RIGHT )
  133.     {
  134.         nextFocus();    // let's move to the next focus
  135.  
  136.         // it seems not to work like this!
  137.         // It would work like this I think:
  138.         // getParent().getComponents()[me+1].requestFocus()
  139.         
  140.         return true;
  141.     }
  142.         return false;
  143.     }
  144.  
  145.     /**
  146.      * This delivers the Event to the parent.
  147.      */
  148.     public void sendEvent() {
  149.         if( getParent() != null )
  150.         getParent().postEvent( new Event( this, Event.ACTION_EVENT, this ) );
  151.     }
  152.     
  153.     /**
  154.      * handle the MouseDownEvents.
  155.      */
  156.     public boolean mouseDown(Event event, int x, int y) {
  157.         if( isswitch == false )
  158.     {
  159.         down = true;
  160.         current = pressed;
  161.         drawIt();
  162.     }
  163.     else {
  164.         if( down == true ) {
  165.             down = false;
  166.             current = normal;
  167.         }
  168.         else {
  169.             down = true;
  170.             current = pressed;
  171.         }
  172.         drawIt();
  173.  
  174.         // now send an Event, that the state has changed!
  175.         sendEvent();
  176.     }
  177.  
  178.     return true;
  179.     }
  180.  
  181.     /**
  182.      * handle the MouseUpEvent.
  183.      */
  184.     public boolean mouseUp(Event event, int x, int y) {
  185.     if( down && isswitch == false ) {
  186.         down = false;
  187.         current = normal;
  188.         drawIt();
  189.  
  190.         // let's see if it's within my boundaries.
  191.         if((x >= 0) && (y >= 0) &&
  192.            (x < width) && (y < height))
  193.         sendEvent();
  194.     }
  195.     if( isswitch == true ) { 
  196.         // think nothing has to be done here
  197.         ;
  198.     }
  199.     return true;
  200.     }
  201.  
  202.     /**
  203.      * updates the image on screen
  204.      */
  205.     void drawIt() {
  206.     Graphics g = getGraphics();
  207.     if( g != null ) paint( g );
  208.     }
  209.     
  210.     /**
  211.      * Draws the picture on the screen (if parts are loaded,
  212.      * then it draws those parts).
  213.      * Overrides paint-Method. 
  214.      */
  215.     public void paint(Graphics graphics) {
  216.     boolean raise = true;
  217.     
  218.         if( current != null && 
  219.         (checkImage( current, width, height, this ) & (WIDTH|HEIGHT|ALLBITS)) != 0 ) {
  220.         // we know width and height of it -> draw it.
  221.         // or the image is complete -> draw it
  222.         graphics.clearRect(0,0,width,height);
  223.         graphics.drawImage(current, 0, 0, width, height, this);
  224.     }
  225.     else {
  226.         if( text != null )
  227.             graphics.drawString( text, 2, height/2 );
  228.     }
  229.  
  230.     if( nopush )
  231.        raise = true;    // always remain raised.
  232.     else
  233.        raise = !down;
  234.  
  235.     // just show a border!
  236.     graphics.setColor( Color.white );
  237.     graphics.draw3DRect( 0, 0, width-1, height-1, raise );
  238.     graphics.setColor( Color.white );
  239.     graphics.draw3DRect( 1, 1, width-3, height-3, raise );
  240.  
  241.     /* some lostFocus() Events don't come!
  242.     // and the nextFocus does not work!
  243.     if( focus == true ) {
  244.         do something
  245.     }
  246.     */
  247.     }
  248.  
  249.     /**
  250.      * tell my preferred-size
  251.      */
  252.     public Dimension preferredSize() {
  253.     return new Dimension( width, height );
  254.     }
  255.  
  256.     /**
  257.      * tell my minimum-size
  258.      */
  259.     public Dimension minimumSize() {
  260.     return new Dimension( width, height );
  261.     }
  262.  
  263.     public boolean gotFocus( Event evt, Object what ) {
  264.         focus = true;
  265.     drawIt();
  266.  
  267.     // pass it back
  268.         return false;
  269.     }
  270.  
  271.     public boolean lostFocus( Event evt, Object what ) {
  272.         focus = false;
  273.     drawIt();
  274.  
  275.     // pass it back
  276.         return false;
  277.     }
  278.  
  279.     /**
  280.      * Tell me all about the image ;-)
  281.      * (inspired from Component.java)
  282.      */
  283.     public boolean imageUpdate(Image img, int flags,
  284.                                int x, int y, int w, int h) {
  285.         int rate = -1;
  286.  
  287.         if ((flags & (FRAMEBITS|ALLBITS)) != 0) {
  288.             rate = 0;
  289.         } else if ((flags & SOMEBITS) != 0) {
  290.         rate = 100;
  291.         }
  292.         if (rate >= 0) {
  293.             repaint(rate, 0, 0, width, height);
  294.         }
  295.         return (flags & (ALLBITS|ABORT)) == 0;
  296.     }
  297. }
  298.  
  299.