home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / ed8n1t2i / imagebutton.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.9 KB  |  122 lines

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // ImageButton.java      v 1.00 b1
  5. // Written by:           I. van Rienen / E-mail ivr@bart.nl
  6. // URL:                  http://www/bart.nl/~ivr
  7. // Initial release:
  8. // Released in public domain:
  9. //
  10. // ---- Description ----
  11. // Java class containing methods for creating and maintaining an ImageButton
  12. //
  13. // This program and the Java source is in the public domain.
  14. // Permission to use, copy, modify, and distribute this software
  15. // and its documentation for NON-COMMERCIAL purposes and
  16. // without fee is hereby granted.
  17. //
  18. //    Copyright 1996
  19. //
  20. //    Iwan van Rienen
  21. //    Joan Maetsuyckerstr. 145
  22. //    2593 ZG  The Hague
  23. //    The Netherlands
  24. //
  25. // I am not responsible for any bugs in this program and
  26. // possible damage to hard- or software when using this program.
  27. //****************************************************************************
  28. import java.awt.*;
  29. import java.awt.image.*;
  30. import java.util.Vector;
  31.  
  32. class ImageButton {
  33.     protected int Xpos;
  34.     protected boolean selected;
  35.     protected boolean pressed;
  36.     protected boolean enabled;
  37.     protected String Name;
  38.  
  39. //----------------------------------------------------------------------------
  40. // Construct a new ImageButton with the specified name.
  41. //----------------------------------------------------------------------------
  42.     public ImageButton (String name, int x) {
  43.         Xpos = x;
  44.         selected = false;
  45.         pressed = false;
  46.         enabled = false;
  47.         Name = name;
  48.     }
  49.  
  50. //----------------------------------------------------------------------------
  51. // return the name of this ImageButton.
  52. //----------------------------------------------------------------------------
  53.     public String getName () {
  54.         return Name;
  55.     }
  56.  
  57. //----------------------------------------------------------------------------
  58. // Check if this button is pressed
  59. //----------------------------------------------------------------------------
  60.     public boolean CheckIfPressed (int x, int y) {
  61.         if (y < 0 || y > 24) return false;
  62.         x -= Xpos;
  63.         if (x < 0 || x > 24) return false;
  64.         if (enabled) {
  65.             pressed = true;
  66.             return true;
  67.         }
  68.         return false;
  69.     }
  70.  
  71. //----------------------------------------------------------------------------
  72. // Draw this button
  73. //----------------------------------------------------------------------------
  74.     public void Draw(Graphics g) {
  75.         int Yoffset;
  76.  
  77.         if (!enabled) {
  78.             Yoffset = 24;
  79.         } else if (pressed) {
  80.             Yoffset = 72;
  81.         } else if (selected) {
  82.             Yoffset = 96;
  83.         } else {
  84.             Yoffset = 48;
  85.         }
  86.         g.copyArea (Xpos, Yoffset, 24, 24, 0, -Yoffset);
  87.     }
  88.  
  89. //----------------------------------------------------------------------------
  90. // Select the button
  91. //----------------------------------------------------------------------------
  92.     public void Select() {
  93.         if (enabled) {
  94.             pressed = false;
  95.             selected = true;
  96.         }
  97.     }
  98.  
  99. //----------------------------------------------------------------------------
  100. // Unselect the button
  101. //----------------------------------------------------------------------------
  102.     public void Unselect() {
  103.         pressed = false;
  104.         selected = false;
  105.     }
  106.  
  107. //----------------------------------------------------------------------------
  108. // Enable the button
  109. //----------------------------------------------------------------------------
  110. public void Enable() {
  111.         pressed = false;
  112.         enabled = true;
  113.     }
  114.  
  115. //----------------------------------------------------------------------------
  116. // Disable the button
  117. //----------------------------------------------------------------------------
  118.     public void Disable() {
  119.         pressed = false;
  120.         enabled = false;
  121.     }
  122. }