home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / os2 / edm2 / 0506 / grinding5.java < prev    next >
Encoding:
Java Source  |  1997-06-01  |  14.8 KB  |  435 lines

  1. package GUITools;
  2. import java.AWT.*;
  3. import java.AWT.event.*;
  4. import java.AWT.image.*;
  5.  
  6. /**
  7.  * Image button class written by Shai Almog 1996.
  8.  * The sorce is in the public domain and may be modified and used freely.
  9.  * It is not a requirment but I would consider it good manners if you credit me
  10.  * and EDM/2 (www.edm2.com) in applications in which this code was used ;-)
  11.  * The ImageButton class is a class that presents a button with an image on it.
  12.  * It has several features which are not common with most ImageButtons:
  13.  * 1. It's fully compatible with java.AWT.Button since it derives from it.
  14.  * 2. It has 3 modes Image/Text (regular button)/Text and Image.
  15.  * 3. It has 2 types regular and toggle (toggle stays pressed).
  16.  * 4. It can be constructed with 1 to 3 images. If you give it only one image it
  17.  *  will use the class ImageButtonDrawingEngine to draw the up and down states.
  18.  * 5. It has a disabled mode.
  19. **/
  20. public class ImageButton extends Button implements ImageObserver, MouseListener
  21. {
  22.     public ImageButton()
  23.     {
  24.       initImageButton();
  25.     }
  26.  
  27.     /**
  28.      * This constructor takes the label of the button as a parameter.
  29.     **/
  30.     public ImageButton(String caption)
  31.     {
  32.       initImageButton();
  33.       setCaption(caption);
  34.     }
  35.  
  36.     /**
  37.      * This constructor takes an Image object as a parameter.
  38.     **/
  39.     public ImageButton(Image imageInstance)
  40.     {
  41.       initImageButton();
  42.       buttonUp = imageInstance; // Setting the images for the buttons.
  43.       buttonDown = buttonUp;    // They are all identical since there is only one picture.
  44.       buttonDisabled = buttonUp;
  45.       waitForImage(buttonUp);   // If the image did not finish loading then wait for it.
  46.     }
  47.  
  48.     /**
  49.      * This constructor takes both the label and the Image as a parameters.
  50.     **/
  51.     public ImageButton(String caption, Image imageInstance)
  52.     {
  53.       initImageButton();
  54.       setCaption(caption);
  55.       buttonUp = imageInstance; // Setting the images for the buttons.
  56.       buttonDown = buttonUp;    // They are all identical since there is only one picture.
  57.       buttonDisabled = buttonUp;
  58.       waitForImage(buttonUp);   // If the image did not finish loading then wait for it.
  59.     }
  60.  
  61.     /**
  62.      * This constructor takes both the label and the image name as a parameters.
  63.     **/
  64.     public ImageButton(String caption, String imageName)
  65.     {
  66.       Toolkit Tk = getToolkit();
  67.       buttonUp = Tk.getImage(imageName); // Loading the image from disk.
  68.       initImageButton();
  69.       setCaption(caption);
  70.       buttonDown = buttonUp; // Setting the images for the buttons.
  71.       buttonDisabled = buttonUp; // They are all identical since there is only one picture.
  72.       waitForImage(buttonUp); // If the image did not finish loading then wait for it.
  73.     }
  74.  
  75.     /**
  76.      * This constructor takes both the label and three image name's as a parameters.
  77.      * These are used for button up/down/disabled states.
  78.     **/
  79.     public ImageButton(String caption, String imageNameButtonUp, String
  80.                        imageNameButtonDown, String imageNameButtonDisabled)
  81.     {
  82.  
  83.       Toolkit Tk = getToolkit();
  84.       buttonUp = Tk.getImage(imageNameButtonUp); // Loading the image's from disk.
  85.       buttonDown = Tk.getImage(imageNameButtonDown);
  86.       buttonDisabled = Tk.getImage(imageNameButtonDisabled);
  87.       initImageButton();
  88.       setCaption(caption);
  89.       waitForImage(buttonUp);// If the image did not finish loading then wait for it.
  90.         // The reason I only wait for button up is that this class is designed
  91.         // to run localy so there will not be a significant delay.
  92.     }
  93.  
  94.     public void setUpImage(Image imageInstance) // Sets the image displayed
  95.     { // when the button is up.
  96.       buttonUp = imageInstance;
  97.     }
  98.  
  99.     public void setDownImage(Image imageInstance)  // Sets the image displayed
  100.     { // when the button is down.
  101.       buttonDown = imageInstance;
  102.     }
  103.  
  104.     public void setDisabledImage(Image imageInstance)  // Sets the image displayed
  105.     { // when the button is disabled.
  106.       buttonDisabled = imageInstance;
  107.     }
  108.  
  109.     public void setToggleOn() // Turns the toggle mode on.
  110.     {
  111.       buttonMode = BUTTON_MODE_TOGGLE;
  112.     }
  113.  
  114.     public void setToggleOff() // Turns the toggle mode off.
  115.     {
  116.       buttonMode = BUTTON_MODE_NORMAL;
  117.       repaint();
  118.     }
  119.  
  120.     public void setTextX(int x) // Sets the x in which the text on an Image & Text button shows.
  121.     {
  122.       textOnImageButtonX = x;
  123.       repaint();
  124.     }
  125.  
  126.     public void setTextY(int y) // Sets the y in which the text on an Image & Text button shows.
  127.     {
  128.       textOnImageButtonX = y;
  129.       repaint();
  130.     }
  131.  
  132.     public void setImageMode() // Sets the button to show as an image button.
  133.     {
  134.       buttonType = BUTTON_TYPE_IMAGE;
  135.       repaint();
  136.     }
  137.  
  138.     public void setTextMode() // Sets the button to show as a regular button.
  139.     {
  140.       buttonType = BUTTON_TYPE_TEXT;
  141.       repaint();
  142.     }
  143.  
  144.     /**
  145.      *  Sets the button to show as an image button with text too.
  146.     **/
  147.     public void setTextAndImageMode()
  148.     {
  149.       buttonType = BUTTON_TYPE_TEXT_AND_IMAGE;
  150.       repaint();
  151.     }
  152.  
  153.     public void setColorOfTextOnButton(Color colorOfTextOnButton) // This will set the color
  154.     { // of the text on the button in case this is an image button with text too.
  155.           this.colorOfTextOnButton = colorOfTextOnButton;
  156.     }
  157.  
  158.     /**
  159.      * Toggles the button bettwean disabled and enabled mode.
  160.     **/
  161.     public void setEnabled(boolean mode)
  162.     {
  163.       super.setEnabled(mode);
  164.       if (mode)
  165.       {
  166.         buttonState = BUTTON_STATE_UP;
  167.       }
  168.       else
  169.       {
  170.         buttonState = BUTTON_STATE_DISABLED;
  171.       }
  172.     }
  173.  
  174.     public void setCaption(String caption)
  175.     {
  176.       this.caption = caption;
  177.     }
  178.  
  179.     public void paint(Graphics g)
  180.     {
  181.       if (BUTTON_TYPE_TEXT == buttonType)
  182.       { // Using the default Java button.
  183.         super.paint(g);
  184.         return;
  185.       }
  186.       switch (buttonState)
  187.       {
  188.         case BUTTON_STATE_UP:
  189.         {
  190.             drawButtonUp(g); // Draws the button up.
  191.             if(BUTTON_TYPE_TEXT_AND_IMAGE == buttonType)
  192.             {
  193.               g.setColor(colorOfTextOnButton);
  194.               // Set the text color to the buttons text color.
  195.               g.drawString(caption,textOnImageButtonX,
  196.                           textOnImageButtonY);
  197.               // Draw the string on the button.
  198.             }
  199.             break;
  200.         }
  201.         case BUTTON_STATE_DOWN :
  202.         {
  203.             drawButtonDown(g); // Draws the button down.
  204.             if(BUTTON_TYPE_TEXT_AND_IMAGE == buttonType)
  205.             {
  206.               g.setColor(colorOfTextOnButton);
  207.               g.drawString(caption,textOnImageButtonX,
  208.                            textOnImageButtonY);
  209.             }
  210.             break;
  211.         }
  212.         case BUTTON_STATE_DISABLED :
  213.         {
  214.             drawButtonDisabled(g); // Draws the button disabled.
  215.             if(BUTTON_TYPE_TEXT_AND_IMAGE == buttonType)
  216.             {
  217.               g.setColor(colorOfTextOnButton);
  218.               g.drawString(caption,textOnImageButtonX,
  219.                            textOnImageButtonY);
  220.             }
  221.             break;
  222.         }
  223.       }
  224.  
  225.     }
  226.  
  227.     /**
  228.      * This method is part of the ImageObserver interface, it's called by Image
  229.      * producers to inform us of new information regarding the image.
  230.      * I use this method to wait for the image to compleately load and is sets
  231.      * imageReady to true the moment the image is ready.
  232.     **/
  233.     public boolean ImageUpdate(Image i,int flags, int x,int y,int height, int width)
  234.     {
  235.       imageReady = imageReady || ((flags & ALLBITS) == 0);
  236.       return(true);
  237.     }
  238.  
  239.     /**
  240.      * This action will cause the button to be pressed.
  241.      * The reason I did not use the action event was that it does not send events
  242.      * for both press and release, we need to know both so we can draw the button
  243.      * correctly.
  244.     **/
  245.     public void mousePressed(MouseEvent e)
  246.     {
  247.       if (buttonMode != BUTTON_MODE_TOGGLE)
  248.         if(buttonState == BUTTON_STATE_UP)
  249.         {
  250.             buttonState = BUTTON_STATE_DOWN;
  251.             repaint();
  252.         }
  253.     }
  254.  
  255.     public void mouseClicked(MouseEvent e)
  256.     {
  257.       // This method is needed by the mouse listener interface.
  258.     }
  259.  
  260.     /**
  261.      * This action will cause the button to be released.
  262.      * The reason I did not use the action event was that it does not send events
  263.      * for both press and release, we need to know both so we can draw the button
  264.      * correctly.
  265.     **/
  266.     public void mouseReleased(MouseEvent e)
  267.     {
  268.       if (buttonMode != BUTTON_MODE_TOGGLE)
  269.       {
  270.         if(buttonState == BUTTON_STATE_DOWN)
  271.         {
  272.             buttonState = BUTTON_STATE_UP;
  273.             repaint();
  274.         }
  275.       }
  276.       else
  277.       {
  278.         if(buttonState == BUTTON_STATE_DOWN)
  279.         {
  280.             buttonState = BUTTON_STATE_UP;
  281.         }
  282.         else
  283.         {
  284.             buttonState = BUTTON_STATE_DOWN;
  285.         }
  286.         repaint();
  287.       }
  288.     }
  289.  
  290.     public void mouseEntered(MouseEvent e)
  291.     {
  292.    // This methods is needed by the mouse listener interface.
  293.     }
  294.  
  295.     public void mouseExited(MouseEvent e)
  296.     {
  297.    // This methods is needed by the mouse listener interface.
  298.     }
  299.  
  300.     /**
  301.      * This method draws the button down.
  302.     **/
  303.     private void drawButtonDown(Graphics g)
  304.     {
  305.       Dimension buttonSize = getSize();
  306.       if (buttonDown == buttonUp) // If the same image is used for all the buttons.
  307.       { // Then call the ImageButtonDrawingEngine to redraw the frame.
  308.         ImageButtonDrawingEngine.drawButtonDown(g);
  309.         g.drawImage(buttonDown
  310.            , 0 + ImageButtonDrawingEngine.X_SPACE
  311.            , 0 + ImageButtonDrawingEngine.Y_SPACE
  312.            , buttonSize.width + ImageButtonDrawingEngine.X_SPACE
  313.            , buttonSize.height + ImageButtonDrawingEngine.Y_SPACE
  314.            , this);
  315.       }
  316.       // Otherwize simply draw the correct image.
  317.       else g.drawImage(buttonDown,0,0,buttonSize.width,buttonSize.height,this);
  318.     }
  319.  
  320.     /**
  321.      * This method draws the button down.
  322.     **/
  323.     private void drawButtonUp(Graphics g)
  324.     {
  325.       Dimension buttonSize = getSize();
  326.       if (buttonDown == buttonUp) // If the same image is used for all the buttons.
  327.       { // Then call the ImageButtonDrawingEngine to redraw the frame.
  328.         ImageButtonDrawingEngine.drawButtonUp(g);
  329.         g.drawImage(buttonUp
  330.            , 0 + ImageButtonDrawingEngine.X_SPACE
  331.            , 0 +ImageButtonDrawingEngine.Y_SPACE
  332.            , buttonSize.width + ImageButtonDrawingEngine.X_SPACE
  333.            , buttonSize.height + ImageButtonDrawingEngine.Y_SPACE
  334.            , this);
  335.       }
  336.       // Otherwize simply draw the correct image.
  337.       else g.drawImage(buttonUp,0,0,buttonSize.width,buttonSize.height,this);
  338.     }
  339.  
  340.     /**
  341.      * This method draws the button down.
  342.     **/
  343.     private void drawButtonDisabled(Graphics g)
  344.     {
  345.       Dimension buttonSize = getSize();
  346.       if (buttonDown == buttonUp) // If the same image is used for all the buttons.
  347.       { // Then call the ImageButtonDrawingEngine to redraw the frame.
  348.         ImageButtonDrawingEngine.drawButtonDisabled(g);
  349.         g.drawImage(buttonDisabled
  350.            , 0 + ImageButtonDrawingEngine.X_SPACE
  351.            , 0 + ImageButtonDrawingEngine.Y_SPACE
  352.            , buttonSize.width + ImageButtonDrawingEngine.X_SPACE
  353.            , buttonSize.height + ImageButtonDrawingEngine.Y_SPACE
  354.            , this);
  355.       }
  356.       // Otherwise simply draw the correct image.
  357.       else g.drawImage(buttonDisabled,0,0,buttonSize.width,buttonSize.height,this);
  358.     }
  359.  
  360.     /**
  361.    * This method is closely related to the ImageUpdate method.
  362.    * This method waits for ImageUpdate to set a switch that the image is ready,
  363.    * thus we can know that the image is in fact in memory. This is due to the
  364.    * Image producer - consumer - observer concept.
  365.     **/
  366.     private void waitForImage(Image imageToWaitFor)
  367.     {
  368.       Dimension buttonSize = getSize();
  369.       Toolkit Tk = getToolkit();
  370.       if (!Tk.prepareImage(buttonUp,buttonSize.width,buttonSize.height,this))
  371.         while (!imageReady);
  372.       repaint();
  373.     }
  374.  
  375.     /**
  376.      * This method initializes the button and is called by all the constructors.
  377.     **/
  378.     private void initImageButton()
  379.     {
  380.       addMouseListener(this); // Listen to mouse related events.
  381.     }
  382.  
  383.     private static final int BUTTON_TYPE_IMAGE = 0; // These are reuqired
  384.     private static final int BUTTON_TYPE_TEXT = 1;  // to toggle the button
  385.     private static final int BUTTON_TYPE_TEXT_AND_IMAGE = 2; // to image
  386.     private int buttonType = BUTTON_TYPE_IMAGE;// and text modes.
  387.     private int textOnImageButtonX; // These determin where the text in a
  388.     private int textOnImageButtonY; // BUTTON_TYPE_TEXT_AND_IMAGE will sit.
  389.     private Color colorOfTextOnButton = Color.black;
  390.  
  391.     private static final int BUTTON_MODE_NORMAL = 0; // These are required to
  392.     private static final int BUTTON_MODE_TOGGLE = 1; // toggle the button to toggle
  393.     private int buttonMode = BUTTON_MODE_NORMAL; // mode where it stays pressed.
  394.  
  395.     private static final int BUTTON_STATE_UP = 0; // These constants reflect the
  396.     private static final int BUTTON_STATE_DOWN = 1; // state of the button.
  397.     private static final int BUTTON_STATE_DISABLED = 2;
  398.     private int buttonState = BUTTON_STATE_UP; // This variable contains one
  399.                                                // of the three constants above.
  400.  
  401.     private boolean imageReady = false; // This variable is used by the ImageUpdate method.
  402.  
  403.     private Image buttonUp,// The Image of the button when it's up.
  404.       buttonDown, // The Image of the button when it's down.
  405.       buttonDisabled; // The Image of the button when it's disbaled.
  406.     private String caption; // The text on the button.
  407. }
  408.  
  409.  
  410. /**
  411.   * This class was built with the purpose of separating the engine that draws
  412.   * the button pressed and normal from the rest of the code so it can be
  413.   * replaced easily.
  414.   */
  415. class ImageButtonDrawingEngine
  416. {
  417.         public static void drawButtonDown(Graphics g)
  418.         {
  419.                Rectangle bounds = g.getClipBounds();
  420.                g.draw3DRect(0,0,bounds.width,bounds.height,false);
  421.         }
  422.         public static void drawButtonUp(Graphics g)
  423.         {
  424.                Rectangle bounds = g.getClipBounds();
  425.                g.draw3DRect(0,0,bounds.width,bounds.height,false);
  426.         }
  427.         public static void drawButtonDisabled(Graphics g)
  428.         {
  429.                Rectangle bounds = g.getClipBounds();
  430.                g.draw3DRect(0,0,bounds.width,bounds.height,false);
  431.         }
  432.         public static int X_SPACE = 2;
  433.         public static int Y_SPACE = 2;
  434. }
  435.