HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Extending the AWT"
Vol. 2, Issue 7, p. 52

	

Listing 1: Listing code. 

/*-------------------------- File : ImageButton.java ----------*/ 
/** 
 * we have to import the awt package in  
 * order to have the necesary classes 
 * to build our component 
 */ 
import java.awt.*; 

public class ImageButton extends Canvas 
{ 
  /** 
   * the constants for 
   * representation of the two states 
   */ 
  final int normalState = 0; 
  final int mouseDown = 1; 
   
  /** 
   * the attribute where the state 
   * is stored 
   */ 
  int state; 
   
  /** 
   * where the image will be stored 
   */ 
  Image image; 
   
  /** 
   * It constructs an ImageButton 
   * by storing the corresponding image 
   * and setting the initial state 
   */ 
  public ImageButton(Image i) 
    { 
      state = normalState; 
      image = i; 
    } 
   
  /** 
   * Looks at the state attribute and 
   * makes the corresponding paints 
   */ 
  public void paint(Graphics g) 
    { 
      g.drawImage(image,3,3,size().width-3,size().height-3,this); 
      g.setColor((state==normalState)? 
   Color.white:Color.gray); 
      Dimension d = size(); 
      g.fillRect(0,0,d.width,3); 
      g.fillRect(0,0,3,d.height); 
      g.setColor((state!=normalState)? 
   Color.white:Color.gray); 
      g.fillRect(d.width-3,3,3,d.height-3); 
      g.fillRect(3,d.height-3,d.width-3,3); 
    } 
   
  /** 
   * manages the significant event mouseDown 
   */ 
  public boolean mouseDown(Event e,int x,int y) 
    { 
      state=mouseDown; 
      repaint(); 
      return true; 
    } 
   
  /** 
   * manages the significant event mouseUp 
   */ 
  public boolean mouseUp(Event e,int x,int y) 
    { 
      state=normalState; 
      postEvent(new Event(this,Event.ACTION_EVENT,null)); 
      repaint(); 
      return true; 
    } 
   
  /** 
   * return the preferred size 
   */ 
  public Dimension preferredSize() 
    { 
      /* 
       * waits until the image is completely load 
       */ 
      try  
 { 
   while (image.getWidth(this)==-1) Thread.sleep(5); 
 } 
      catch (InterruptedException intex) 
 { 
 } 
      return new Dimension(image.getWidth(this)+6,image.getHeight(this)+6); 
    } 

  /** 
   * return the minimum size (in this case 
   * the same as preferred size) 
   */ 
  public Dimension minimumSize() 
    { 
      /* 
       * waits until the image is completely load 
       */ 
      try  
 { 
   while (image.getWidth(this)==-1) Thread.sleep(5); 
 } 
      catch (InterruptedException intex) 
 { 
 } 
      return new Dimension(image.getWidth(this)+6,image.getHeight(this)+6); 
    } 
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.