home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / ui / ImageButton.java < prev    next >
Text File  |  1996-12-30  |  4KB  |  151 lines

  1. /**                                                                     */
  2. /*              Copyright (C) 1996 ADI Ltd, Systems Group               */
  3. /*                      Engineering & Development                       */
  4. /*                          All rights reserved                         */
  5. /*                                                                      */
  6. /*              Written by      : Cameron Gillies                       */
  7. /*              Last Modified   : 21/10/96                              */
  8. /*                                                                      */
  9.  
  10. package ui;
  11.  
  12. import java.awt.*;
  13. import java.net.*;
  14. import ui.ImageLabel;
  15.  
  16. /**
  17.  * An image button (a 3D rect around an image.)  It greys itself out
  18.  * when disabled and inverts the 3D rect and moves its image when depressed.
  19.  */
  20. public class ImageButton extends ImageLabel
  21. {
  22.   private int lineWidth = 2;      // thickness of 3D line around button
  23.   private int pressMovement = 1;  // distance image moves when button pressed
  24.   protected boolean depressed;
  25.   private String buttonTitle;
  26.  
  27.   public ImageButton(String title, URL img, int x, int y)
  28.   {
  29.     super(img, x, y);
  30.     buttonTitle = new String(title);
  31.     depressed = false;
  32.     padWidth = lineWidth + 2;
  33.   }
  34.  
  35.   public void setPadWidth(int w)
  36.   {
  37.     super.setPadWidth(w + lineWidth);
  38.   }
  39.  
  40.   public int getPadWidth()
  41.   {
  42.     return(super.getPadWidth() - lineWidth);
  43.   }
  44.  
  45.   public void setLineWidth(int w)
  46.   {
  47.     int oldPadWidth = getPadWidth();
  48.     lineWidth = w;
  49.     setPadWidth(oldPadWidth);
  50.   }
  51.  
  52.   public int getLineWidth()
  53.   {
  54.     return(lineWidth);
  55.   }
  56.  
  57.   public void setPressMovement(int p)
  58.   {
  59.     if (p != pressMovement)
  60.     {
  61.       pressMovement = p;
  62.       if (depressed)
  63.       {
  64.                 Graphics g = getGraphics();
  65.                 if (g != null)
  66.                 {
  67.                     Dimension d = size();
  68.                     g.setColor(getBackground());
  69.                     g.fillRect(0, 0, d.width, d.height);
  70.                     repaint();
  71.                 }
  72.       }
  73.     }
  74.   }
  75.  
  76.   public int getPressMovement()
  77.   {
  78.     return(pressMovement);
  79.   }
  80.  
  81.     public void paint(Graphics g)
  82.     {
  83.         if (img != null && (imgWidth == -1 || imgHeight == -1))
  84.             {
  85.                 imgHeight = img.getHeight(this) ;
  86.                 imgWidth = img.getWidth(this) ;
  87.                 this.resize(imgWidth + padWidth * 2 , imgHeight + padWidth * 2 );
  88.             }
  89.  
  90.         if (imgWidth >= 0 && imgHeight >= 0)
  91.         {
  92.                 Dimension d = size();
  93.                 int x = (d.width - imgWidth) / 2;
  94.                 int y = (d.height - imgHeight) / 2;
  95.                 int offset = (depressed ? pressMovement : 0);
  96.  
  97.                 if (pressMovement != 0)
  98.                 {
  99.                     // clear the area needed to accomodate press movement
  100.                     g.setColor(getBackground());
  101.                     int m = (pressMovement < 0 ? -1 : 1);
  102.                     for (int i = 0; i < pressMovement * m; i++)
  103.                         g.drawRect(x + i * m + (m < 0 ? -1 : 0),
  104.                                 y + i * m + (m < 0 ? -1 : 0), imgWidth, imgHeight);
  105.                 }
  106.  
  107.                 // draw the image and the 3D border
  108.                 g.drawImage(img, x + offset, y + offset, getBackground(), this);
  109.                 g.setColor(getBackground());
  110.                 for (int i = 0; i < lineWidth; i++)
  111.                     g.draw3DRect(i, i,
  112.                                 d.width - i*2 - 1,
  113.                                 d.height - i*2 - 1,
  114.                                 !depressed);
  115.             }
  116.     }
  117.  
  118.     public boolean mouseDown(Event e, int x, int y)
  119.     {
  120.         depressed = true;
  121.         repaint();
  122.         return true;
  123.     }
  124.  
  125.     public boolean mouseDrag(Event e, int x, int y)
  126.     {
  127.         if (depressed != inside(x, y))
  128.         {
  129.             depressed = !depressed;
  130.             repaint();
  131.         }
  132.         return true;
  133.     }
  134.  
  135.     public boolean mouseUp(Event evt, int x, int y)
  136.     {
  137.         if (depressed)
  138.         {
  139.             action();
  140.             depressed = false;
  141.             repaint();
  142.         }
  143.         return true;
  144.     }
  145.  
  146.     public void action()
  147.     {
  148.         postEvent(new Event(this, Event.ACTION_EVENT, buttonTitle));
  149.     }
  150. }
  151.