home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 25 / nopv25.iso / 040A / IBUTTON.ZIP / ImageButton / Source / ImageButton.java < prev    next >
Encoding:
Java Source  |  1997-07-01  |  8.2 KB  |  240 lines

  1. // File ImageButton.java jlouie 6.97
  2. // Based on FancyButtonBean from "Presenting Java Beans" by Michael Morrison
  3. // SAMS Net and GrayFilter from "Java in a Nutshell" David Flanagan O'Reilly
  4. // A JDK1.1/BDK1.0 release version of Michael Morrisons FancyButtonBean
  5. // that supports a gif image.
  6. // Compile GrayFilter.java and then ImageButton.java and ImageButtonBeanInfo.java
  7. // using: javac -d . .\ImageButton\Source\GrayFilter.java
  8. // javac -d . .\ImageButton\Source\ImageButton.java
  9. // javac -d . .\ImageButton\Source\ImageButtonBeanInfo.java
  10. // Test the component using: java jlouie.beans.ImageButton
  11. // Jar the bean using: 
  12. // jar cfm .\imagebutton.jar jlouie\beans\ImageButton.mf 
  13. // jlouie\beans\*.class jlouie\beans\*.gif
  14. // Where default.gif is in the folder jlouie\beans along with the compiled class files,
  15. // the manifest file and the BeanInfo file.
  16. // Place the jar file in the BDK jars folder
  17. // Test the component with the BeanBox using: c:\bdk\beanbox>run
  18. // Add the ImageButton to the form.
  19. // Set the isSticky property to true!
  20. // Have fun with Java.
  21.  
  22. package jlouie.beans;
  23.  
  24. import java.awt.*;
  25. import java.awt.image.*;
  26. import java.awt.event.*;
  27. import java.beans.*;
  28. import java.io.*;
  29. import java.net.*;
  30.  
  31. public class ImageButton extends Canvas implements Serializable {
  32.       
  33.       private final static String DEFAULT_FILE= "default.gif";
  34.       private final static Dimension DEFAULT_DIMENSION= new Dimension(30,10);
  35.  
  36.       private  Image image= null;
  37.       private  Image gray= null;
  38.       private  boolean isSticky= false;
  39.       private  boolean isDown= false;
  40.       private  transient boolean isFocus= false;
  41.       private  String imageName= DEFAULT_FILE;
  42.       private  transient ActionListener actionListener= null;
  43.  
  44.       public static void main(String[] args) {
  45.              Frame f= new Frame();
  46.              f.addNotify();
  47.              f.setLayout(new GridLayout(1,2));
  48.              ImageButton ib= new ImageButton(DEFAULT_FILE,true);
  49.              f.add(ib);
  50.              f.add(new ImageButton());
  51.              f.setSize(200,200);
  52.              f.show();
  53.       }
  54.            
  55.       public ImageButton(String imageName, boolean isSticky) {
  56.             this.isSticky= isSticky;
  57.             image= loadImage(imageName);
  58.             sizeToFit();
  59.             ImageFilter filter= new GrayFilter();
  60.             ImageProducer producer= new FilteredImageSource(image.getSource(), filter);
  61.             gray= this.createImage(producer);
  62.             enableEvents(AWTEvent.FOCUS_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK |
  63.                   AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
  64.       }
  65.       
  66.       public ImageButton(String imageName) {
  67.             this(imageName, false);
  68.       }
  69.       
  70.       public ImageButton() {
  71.             this(DEFAULT_FILE, false);     
  72.       }
  73.       
  74.       private void sizeToFit() {
  75.             Dimension d= getPreferredSize();
  76.             setSize(d.width, d.height);
  77.             Component p= getParent();
  78.             if (p != null) {
  79.                   p.invalidate();
  80.                   p.doLayout();
  81.             }
  82.       }
  83.       
  84.       public String getImageName() {
  85.             return imageName;
  86.       }
  87.       
  88.       public void setImageName(String imageName) {
  89.             if (imageName != null) {
  90.                   this.imageName= imageName;
  91.                   image= loadImage(imageName);
  92.                   sizeToFit();
  93.             }
  94.       }
  95.       
  96.       public boolean isSticky() {
  97.          return isSticky;
  98.       }
  99.       
  100.       public void setSticky(boolean isSticky) {
  101.          this.isSticky= isSticky;
  102.       }
  103.       
  104.       public boolean isFocus() {
  105.          return isFocus;
  106.       }
  107.       
  108.       public boolean isDown() {     // returns false is not a sticky button
  109.          return (isSticky && isDown);
  110.       }
  111.       
  112.       public synchronized void AddActionListener(ActionListener l) {
  113.             actionListener= AWTEventMulticaster.add(actionListener, l);
  114.       }
  115.       
  116.       public synchronized void removeActionListener(ActionListener l) {
  117.             actionListener= AWTEventMulticaster.remove(actionListener, l);
  118.       }
  119.       
  120.       protected void processActionEvent(ActionEvent e) {
  121.             if (actionListener != null) {
  122.                   actionListener.actionPerformed(e);
  123.             }
  124.       }
  125.       
  126.       protected void processFocusEvent(FocusEvent e) {
  127.             switch(e.getID()) {
  128.                   case FocusEvent.FOCUS_GAINED:
  129.                         isFocus = true;
  130.                         update(getGraphics());
  131.                         break;
  132.                   case FocusEvent.FOCUS_LOST:
  133.                         isFocus = false;
  134.                         update(getGraphics());
  135.                         break;    
  136.             }
  137.             super.processFocusEvent(e);
  138.       }
  139.       
  140.       protected void processMouseEvent(MouseEvent e) {
  141.             switch(e.getID()) {
  142.                   case MouseEvent.MOUSE_PRESSED:
  143.                        isDown= !isDown;
  144.                        update(getGraphics());
  145.                        break;
  146.                   case MouseEvent.MOUSE_RELEASED:
  147.                        if (isDown && !isSticky) {
  148.                           processActionEvent(new ActionEvent(this,
  149.                               ActionEvent.ACTION_PERFORMED, null));
  150.                           isDown= false;
  151.                           update(getGraphics());
  152.                        }
  153.             }
  154.             super.processMouseEvent(e);
  155.       }
  156.       
  157.       protected void processKeyEvent(KeyEvent e) {
  158.             if ((e.getKeyCode() == KeyEvent.VK_ENTER) ||
  159.                    (e.getKeyChar() == KeyEvent.VK_SPACE)) {
  160.                 if (isSticky) {
  161.                    isDown= !isDown;
  162.                    update(getGraphics());
  163.                 }
  164.                 else {
  165.                     isDown= true;
  166.                     update(getGraphics());
  167.                     processActionEvent(new ActionEvent(this,
  168.                         ActionEvent.ACTION_PERFORMED, null));
  169.                     isDown= false;
  170.                     update(getGraphics());
  171.                 }        
  172.             }
  173.             super.processKeyEvent(e);
  174.       }
  175.       
  176.       protected void processMouseMotionEvent(MouseEvent e) {
  177.             if (e.getID() == MouseEvent.MOUSE_DRAGGED && !isSticky) {
  178.                   Point p= e.getPoint();
  179.                   if ((p.x<0 || p.x>getSize().width) ||
  180.                         (p.y<0 || p.y>getSize().height)) {
  181.                         if (isDown) {
  182.                               isDown= false;
  183.                               update(getGraphics());
  184.                         }     
  185.                   }
  186.                   else if (!isDown) {
  187.                      isDown= true;
  188.                      update(getGraphics());
  189.                   }
  190.             }
  191.             super.processMouseMotionEvent(e);
  192.       }
  193.       
  194.       public Image loadImage(String name) {
  195.          try {
  196.             URL url = this.getClass().getResource(name);
  197.             Object o= url.getContent();
  198.             if (o instanceof ImageProducer) {
  199.                return createImage((ImageProducer)o);
  200.             }
  201.             else return null;
  202.          }                
  203.          catch (Exception e) {
  204.             System.out.println(e.toString());
  205.             return null;
  206.          }
  207.       }  
  208.       
  209.       public Dimension preferredSize() {
  210.             if (image != null) {
  211.                   return new Dimension(image.getWidth(null), image.getHeight(null));
  212.             }
  213.             else return DEFAULT_DIMENSION;
  214.       }
  215.       
  216.       public void update(Graphics g) {
  217.             paint(g);
  218.       }
  219.    
  220.       public synchronized void paint(Graphics g) {
  221.             Dimension d= preferredSize();
  222.             if ((image != null) && (gray != null)) {
  223.                   if (isDown) {
  224.                      g.drawImage(gray,0,0,this);
  225.                   }
  226.                   else g.drawImage(image,0,0,this);
  227.             }
  228.             else g.drawString("ImageButton",10,10);
  229.             if (isFocus) {
  230.                g.setColor(Color.black);
  231.             }
  232.             else g.setColor(Color.darkGray);
  233.             g.draw3DRect(0,0,d.width-1,d.height-1,!isDown);
  234.       }
  235.       
  236. }     
  237.  
  238.  
  239.  
  240.