home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / awt / imageclick.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.0 KB  |  172 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 ImageClick extends Canvas {
  19.     Image image;
  20.     boolean focus = false;
  21.     String text;
  22.     Point pos = null;
  23.     boolean editable;
  24.  
  25.     int RADIUS = 6;    
  26.  
  27.     int imagesizex = 400;
  28.     int imagesizey = 200;
  29.  
  30.     /**
  31.      */
  32.     public ImageClick( Image i ) {
  33.         image = i;
  34.     }
  35.  
  36.     public void setPos(int x, int y) {
  37.         setPos(new Point(x,y));
  38.     }
  39.     /**
  40.      * Set the current state of the switch!
  41.      * (If it is a button then do nothing!)
  42.      * @param onoff indicating on or off
  43.      */
  44.     public void setPos( Point pos ) {
  45.         this.pos = pos;
  46.         repaint();
  47.     }
  48.     public void setEditable(boolean e) {
  49.     editable = e;
  50.     }
  51.  
  52.     /**
  53.      * Get the current state of the switch! (if it's a button
  54.      * you will get true if it's currently pressed).
  55.      * @return if the switch is on then return true; else false.
  56.      */
  57.     public Point getPos() {
  58.         return pos;
  59.     }
  60.  
  61.     /**
  62.      * Move the focus to the next component
  63.      * Does NOT work! (cause nextFocus() doesn't do what I wanted)
  64.      */
  65.     public boolean keyDown( Event evt, int key ) {
  66.         if( key == (int)'\t' || key == Event.DOWN || key == Event.RIGHT )
  67.     {
  68.         nextFocus();    // let's move to the next focus
  69.         return true;
  70.     }
  71.         return false;
  72.     }
  73.  
  74.     
  75.     /**
  76.      * handle the MouseDownEvents.
  77.      */
  78.     public boolean handleEvent(Event evt) {
  79.     // This will transform the coordinates of the event to the
  80.     // pixel coordinates in the picture.
  81.  
  82.     evt.x = evt.x * image.getWidth(this) / size().width;
  83.     evt.y = evt.y * image.getHeight(this) / size().height;
  84.  
  85.         if ((editable) && (evt.id == Event.MOUSE_DOWN)) {
  86.             setPos(evt.x,evt.y);    
  87.             evt.arg = getPos();
  88.             evt.id = Event.ACTION_EVENT;
  89.         }
  90.     return false;
  91.     }
  92.  
  93.     /**
  94.      * Draws the picture on the screen (if parts are loaded,
  95.      * then it draws those parts).
  96.      * Overrides paint-Method. 
  97.      */
  98.     public void paint(Graphics graphics) {
  99.     setImageSize();
  100.         if( image != null && 
  101.         (checkImage( image, size().width, size().height, this ) & (WIDTH|HEIGHT|ALLBITS)) != 0 ) {
  102.         // we know width and height of it -> draw it.
  103.         // or the image is complete -> draw it
  104.  
  105.         graphics.drawImage(image, 0, 0, size().width,size().height, this);
  106.     }
  107.     else {
  108.         if( text != null )
  109.             graphics.drawString( text, 2, size().height/2 );
  110.     }    
  111.     if (pos != null) {
  112.         graphics.setColor(Color.white);
  113.         int x = pos.x * size().width / imagesizex;
  114.         int y = pos.y * size().height / imagesizey;
  115.         graphics.drawOval(x-RADIUS,y - RADIUS,RADIUS*2,RADIUS*2);    
  116.     }
  117.     }
  118.  
  119.     /* some lostFocus() Events don't come!
  120.     // and the nextFocus does not work!
  121.     if( focus == true ) {
  122.         do something
  123.     }
  124.     */
  125.     public boolean gotFocus( Event evt, Object what ) {
  126.         focus = true;
  127.     // pass it back
  128.         return false;
  129.     }
  130.  
  131.     public boolean lostFocus( Event evt, Object what ) {
  132.         focus = false;
  133.     // pass it back
  134.         return false;
  135.     }
  136.  
  137.     /**
  138.      * Tell me all about the image ;-)
  139.      * (inspired from Component.java)
  140.      */
  141.     public boolean imageUpdate(Image img, int flags,
  142.                                int x, int y, int w, int h) {
  143.         int rate = -1;
  144.  
  145.         if ((flags & (FRAMEBITS|ALLBITS)) != 0) {
  146.             rate = 0;
  147.         } else if ((flags & SOMEBITS) != 0) {
  148.         rate = 100;
  149.         }
  150.         if (rate >= 0) {
  151.             repaint(rate, 0, 0, size().width, size().height);
  152.         }
  153.         return (flags & (ALLBITS|ABORT)) == 0;
  154.     }
  155.     public void setImageSize() {
  156.         if (image != null) {
  157.             int h = image.getHeight(this);
  158.             int w = image.getWidth(this);
  159.             if ((h>0)&&(w>0)) {
  160.                 imagesizex = w;
  161.                 imagesizey = h;
  162.             }
  163.         }
  164.     }        
  165.     public Dimension preferredSize() {
  166.         setImageSize();
  167.         Dimension d = new Dimension(imagesizex,imagesizey);
  168.         return d;
  169.     }
  170. }
  171.  
  172.