Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/adorphuye/othello/gui/ImageButton.java


1   package com.adorphuye.othello.gui;
2   
3   import java.awt.*;
4   import java.awt.event.*;
5   
6   public class ImageButton extends Canvas
7   {
8     private Image img = null;
9     private boolean toggle = true;
10    private String text = "";
11    private Color foreground = Color.black;
12  
13    /**
14     * @param i  */  
15    public ImageButton(Image i)
16    {
17      super();
18      setImage(i);
19      setSize(14,14);
20    }
21  
22    /**
23     * @param c  */  
24    public void setForeground(Color c)
25    {
26      foreground = c;
27    }
28  
29    /**
30     * @return  */  
31    public Color getForeground()
32    {
33      return foreground;
34    }
35    
36    /**
37     * @param b  */  
38    public void setToggle(boolean b)
39    {
40      toggle = b;
41      repaint();
42    }
43    
44    /**
45     * @return  */  
46    public boolean isToggle()
47    {
48      return toggle;
49    }
50    
51    /**
52     * @param s  */  
53    public void setText(String s)
54    {
55      text = s;
56      repaint();
57    }
58    
59    /**
60     * @return  */  
61    public String getText()
62    {
63      return text;
64    }
65    
66    /**
67     * @return  */  
68    public Dimension getMaximumSize()
69    {
70      if(getImage()==null)
71      {
72        return super.getMaximumSize();
73      }
74      return new Dimension(14,14);
75    }
76    
77    /**
78     * @return  */  
79    public Dimension getMinimumSize()
80    {
81      if(getImage()==null)
82      {
83        return super.getMinimumSize();
84      }
85      return getPreferredSize();
86    }
87    
88    /**
89     * @return  */  
90    public Dimension getPreferredSize()
91    {
92      if(getImage()==null)
93      {
94        return super.getPreferredSize();
95      }
96      
97      int height = getImage().getHeight(this)+2;
98      int width  = getImage().getWidth(this)+2;
99      return new Dimension(width,height);
100   }
101   
102   /**
103    * @param i  */  
104   public void setImage(Image i)
105   {
106     MediaTracker tg = new MediaTracker(this);
107     try
108     {
109       tg.addImage(i,0);
110       tg.waitForAll();
111     }
112     catch(InterruptedException e)
113     {
114       e.printStackTrace();
115     }
116     
117     img = i;
118     repaint();
119   }
120   
121   /**
122    * @return  */  
123   public Image getImage()
124   {
125     return img;
126   }
127   
128   /**
129    * @param graphics  */  
130   public void paint(java.awt.Graphics graphics)
131   {
132     
133     //super.paint(graphics);
134     if(getImage()!=null && isToggle())
135     {
136       FontMetrics fm = graphics.getFontMetrics();
137       int h = fm.getMaxAscent()+fm.getMaxDescent();
138       int w = fm.stringWidth(getText());
139       
140       graphics.drawImage(getImage(),1,1,getSize().width-2, getSize().height-2,this);
141       graphics.setColor(getForeground());
142       graphics.drawString(getText(),(getSize().width/2)-(w/2),getSize().height-fm.getMaxDescent());
143     }
144     //graphics.setColor(Color.black);
145     //graphics.drawRect(0,0,getSize().width-1, getSize().height-1);
146   }
147   
148   /**
149    * @param graphics  */  
150   public void update(java.awt.Graphics graphics)
151   {
152     paint(graphics);
153   }
154   
155 }