/[classpath]/classpath/javax/swing/plaf/basic/BasicArrowButton.java
ViewVC logotype

Diff of /classpath/javax/swing/plaf/basic/BasicArrowButton.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by mark, Sun Sep 5 11:31:06 2004 UTC revision 1.7 by mkoch, Sun Sep 26 15:21:13 2004 UTC
# Line 40  package javax.swing.plaf.basic; Line 40  package javax.swing.plaf.basic;
40  import java.awt.Color;  import java.awt.Color;
41  import java.awt.Component;  import java.awt.Component;
42  import java.awt.Dimension;  import java.awt.Dimension;
 import java.awt.Insets;  
43  import java.awt.Graphics;  import java.awt.Graphics;
44    import java.awt.Insets;
45  import java.awt.Polygon;  import java.awt.Polygon;
46  import java.awt.Rectangle;  import java.awt.Rectangle;
 import javax.swing.border.Border;  
47  import javax.swing.Icon;  import javax.swing.Icon;
48  import javax.swing.JButton;  import javax.swing.JButton;
49  import javax.swing.SwingConstants;  import javax.swing.SwingConstants;
50    import javax.swing.border.Border;
51    
52    
53  /**  /**
# Line 55  import javax.swing.SwingConstants; Line 55  import javax.swing.SwingConstants;
55   */   */
56  public class BasicArrowButton extends JButton implements SwingConstants  public class BasicArrowButton extends JButton implements SwingConstants
57  {  {
58    /**    /** The default size of the Arrow buttons. */
59     * A private helper class that draws icons.    private static int defaultSize = 10;
    */  
   private class arrowIcon implements Icon  
   {  
     /** The polygon that describes the icon. */  
     private Polygon arrow;  
   
     /** The size of the icon. */  
     private int size = 10;  
   
     /**  
      * Creates a new arrowIcon object using the given arrow polygon.  
      *  
      * @param arrow The polygon that describes the arrow.  
      */  
     public arrowIcon(Polygon arrow)  
     {  
       this.arrow = arrow;  
     }  
   
     /**  
      * Returns the height of the icon.  
      *  
      * @return The height of the icon.  
      */  
     public int getIconHeight()  
     {  
       return size;  
     }  
   
     /**  
      * Returns the width of the icon.  
      *  
      * @return The width of the icon.  
      */  
     public int getIconWidth()  
     {  
       return size;  
     }  
   
     /**  
      * Sets the size of the icon.  
      *  
      * @param size The size of the icon.  
      */  
     public void setSize(int size)  
     {  
       this.size = size;  
     }  
   
     /**  
      * Sets the arrow polygon.  
      *  
      * @param arrow The arrow polygon.  
      */  
     public void setArrow(Polygon arrow)  
     {  
       this.arrow = arrow;  
     }  
   
     /**  
      * Paints the icon.  
      *  
      * @param c The Component to paint for.  
      * @param g The Graphics object to draw with.  
      * @param x The X coordinate to draw at.  
      * @param y The Y coordinate to draw at.  
      */  
     public void paintIcon(Component c, Graphics g, int x, int y)  
     {  
       Color arrowColor;  
       if (c.isEnabled())  
         arrowColor = darkShadow;  
       else  
         arrowColor = shadow;  
   
       paintIconImpl(g, x, y, arrowColor);  
     }  
   
     /**  
      * This method does the actual painting work.  
      *  
      * @param g The Graphics object to paint with.  
      * @param x The x coordinate to paint at.  
      * @param y The y coordinate to paint at.  
      * @param arrowColor The color to paint the arrow with.  
      */  
     public void paintIconImpl(Graphics g, int x, int y, Color arrowColor)  
     {  
       g.translate(x, y);  
   
       Color saved = g.getColor();  
   
       g.setColor(arrowColor);  
60    
61        g.fillPolygon(arrow);    /** The Polygon that points up. */
62      private static Polygon upIcon = new Polygon(new int[] { 0, 5, 9 },
63        g.setColor(saved);                                                new int[] { 7, 2, 7 }, 3);
64        g.translate(-x, -y);  
65      }    /** The Polygon that points down. */
66    }    private static Polygon downIcon = new Polygon(new int[] { 1, 5, 9 },
67                                                    new int[] { 3, 7, 3 }, 3);
68    
69      /** The Polygon that points left. */
70      private static Polygon leftIcon = new Polygon(new int[] { 7, 3, 7 },
71                                                    new int[] { 1, 5, 9 }, 3);
72    
73      /** The Polygon that points right. */
74      private static Polygon rightIcon = new Polygon(new int[] { 3, 7, 3 },
75                                                     new int[] { 1, 5, 9 }, 3);
76    
77    /** The direction to point in. */    /** The direction to point in. */
78    protected int direction;    protected int direction;
79    
80    /** The color the arrow is painted in if disabled and the bottom and    /**
81     * right edges of the button. */     * The color the arrow is painted in if disabled and the bottom and right
82    private transient Color shadow = Color.gray;     * edges of the button.
83       */
84    /** The color the arrow is painted in if enabled and the bottom and    private transient Color shadow = Color.GRAY;
85     * right edges of the button. */  
86    private transient Color darkShadow = Color.BLACK;    /**
87       * The color the arrow is painted in if enabled and the bottom and right
88       * edges of the button.
89       */
90      private transient Color darkShadow = Color.DARK_GRAY;
91    
92    /** The top and left edges of the button. */    /** The top and left edges of the button. */
93    private transient Color highlight = Color.BLACK;    private transient Color highlight = Color.WHITE;
94      
95    /** The border around the ArrowButton. */    /** The border around the ArrowButton. */
96    private transient Border tmpBorder = new Border()    private transient Border buttonBorder = new Border()
   {  
     public Insets getBorderInsets(Component c)  
     {  
       return new Insets(0, 0, 0, 0);  
     }  
       
     public boolean isBorderOpaque()  
97      {      {
98        return false;        public Insets getBorderInsets(Component c)
99      }        {
100               return new Insets(2, 2, 2, 2);
101      public void paintBorder(Component c, Graphics g, int x, int y, int w, int h)        }
     {  
       Rectangle bounds = getBounds();  
102    
103        Color saved = g.getColor();        public boolean isBorderOpaque()
104        g.setColor(highlight);        {
105           return true;
106          }
107    
108        g.drawLine(bounds.x, bounds.y, bounds.x, bounds.y + bounds.height);        public void paintBorder(Component c, Graphics g, int x, int y, int w,
109        g.drawLine(bounds.x, bounds.y, bounds.x + bounds.width, bounds.y);                                int h)
110          {
111           System.out.println("PAINTING BORDER");
112           Color saved = g.getColor();
113           g.setColor(highlight);
114    
115        g.setColor(shadow);         g.drawLine(x + 1, y + 1, x + w - 1, y + 1);
116           g.drawLine(x + 1, y + 1, x + 1, y + h - 1);
117    
118        g.drawLine(bounds.x + 1, bounds.y + bounds.height - 1,         g.setColor(shadow);
                  bounds.x + bounds.width - 1, bounds.y + bounds.height - 1);  
       g.drawLine(bounds.x + bounds.width - 1, bounds.y + 1,  
                  bounds.x + bounds.width - 1, bounds.y + bounds.height - 1);  
119    
120        g.setColor(darkShadow);         g.drawLine(x + 1, y + h - 1, x + w - 1, y + h - 1);
121           g.drawLine(x + w - 1, y + 1, x + w - 1, y + h - 1);
122    
123        g.drawLine(bounds.x, bounds.y + bounds.height, bounds.x + bounds.width,         g.setColor(darkShadow);
                  bounds.y + bounds.height);  
       g.drawLine(bounds.x + bounds.width, bounds.y, bounds.x + bounds.width,  
                  bounds.y + bounds.height);  
124    
125        g.setColor(saved);         g.drawLine(x, y + h, x + w, y + h);
126      }         g.drawLine(x + w, y, x + w, y + h);
127    };  
128           g.setColor(saved);
129          }
130        };
131    
132    /**    /**
133     * Creates a new BasicArrowButton object.     * Creates a new BasicArrowButton object.
# Line 221  public class BasicArrowButton extends JB Line 137  public class BasicArrowButton extends JB
137    public BasicArrowButton(int direction)    public BasicArrowButton(int direction)
138    {    {
139      super();      super();
140        setBorder(buttonBorder);
141      setDirection(direction);      setDirection(direction);
     setBorder(tmpBorder);  
142    }    }
143    
144    /**    /**
145     * Creates a new BasicArrowButton object with the given colors and     * Creates a new BasicArrowButton object with the given colors and
146     * direction.     * direction.
147     *     *
148     * @param direction The direction to point in.     * @param direction The direction to point in.
# Line 246  public class BasicArrowButton extends JB Line 162  public class BasicArrowButton extends JB
162    }    }
163    
164    /**    /**
165       * This method returns whether the focus can traverse to this component.
166       *
167       * @return Whether the focus can traverse to this component.
168       */
169      public boolean isFocusTraversable()
170      {
171        return false;
172      }
173    
174      /**
175     * This method returns the direction of the arrow.     * This method returns the direction of the arrow.
176     *     *
177     * @return The direction of the arrow.     * @return The direction of the arrow.
# Line 262  public class BasicArrowButton extends JB Line 188  public class BasicArrowButton extends JB
188     */     */
189    public void setDirection(int dir)    public void setDirection(int dir)
190    {    {
     Polygon arrow = getArrow(dir, 10);  
     if (getIcon() == null)  
       setIcon(new arrowIcon(arrow));  
     else  
       ((arrowIcon) getIcon()).setArrow(arrow);  
191      this.direction = dir;      this.direction = dir;
192    }    }
193    
194    /**    /**
195     * This method paints the arrow button.     * This method paints the arrow button. The painting is delegated to the
196       * paintTriangle method.
197     *     *
198     * @param g The Graphics object to paint with.     * @param g The Graphics object to paint with.
199     */     */
200    public void paint(Graphics g)    public void paint(Graphics g)
201    {    {
202      super.paint(g);      super.paint(g);
203        Insets insets = getInsets();
204        Rectangle bounds = getBounds();
205        int x = insets.left
206                + (bounds.width - insets.left - insets.right - defaultSize) / 2;
207        int y = insets.top
208                + (bounds.height - insets.left - insets.right - defaultSize) / 2;
209        paintTriangle(g, x, y, defaultSize, direction, isEnabled());
210    }    }
211    
212    /**    /**
# Line 287  public class BasicArrowButton extends JB Line 216  public class BasicArrowButton extends JB
216     */     */
217    public Dimension getPreferredSize()    public Dimension getPreferredSize()
218    {    {
219      return new Dimension(getIcon().getIconWidth(), getIcon().getIconHeight());      Insets insets = getInsets();
220        int w = defaultSize + insets.left + insets.right;
221        int h = defaultSize + insets.top + insets.bottom;
222    
223        return new Dimension(w, h);
224    }    }
225    
226    /**    /**
# Line 311  public class BasicArrowButton extends JB Line 244  public class BasicArrowButton extends JB
244    }    }
245    
246    /**    /**
247     * The method paints a triangle with the given size and direction at     * The method paints a triangle with the given size and direction at the
248     * the given x and y coordinates.     * given x and y coordinates.
249     *     *
250     * @param g The Graphics object to paint with.     * @param g The Graphics object to paint with.
251     * @param x The x coordinate to paint at.     * @param x The x coordinate to paint at.
# Line 324  public class BasicArrowButton extends JB Line 257  public class BasicArrowButton extends JB
257    public void paintTriangle(Graphics g, int x, int y, int size, int direction,    public void paintTriangle(Graphics g, int x, int y, int size, int direction,
258                              boolean isEnabled)                              boolean isEnabled)
259    {    {
260      Polygon arrow = getArrow(direction, size);      Polygon arrow = null;
     arrowIcon arrowI = new arrowIcon(arrow);  
     arrowI.setSize(size);  
   
     Color arrowColor;  
     if (isEnabled())  
       arrowColor = darkShadow;  
     else  
       arrowColor = shadow;  
   
     arrowI.paintIconImpl(g, x, y, arrowColor);  
   }  
   
   /**  
    * This is a private helper that creates polygons for a given size  
    * and direction.  
    *  
    * @param direction The direction of the arrow.  
    * @param size The size of the arrow.  
    *  
    * @return A new arrow polygon.  
    */  
   private Polygon getArrow(int direction, int size)  
   {  
     Polygon arrow;  
     double dsize = (double) size;  
                   
                 int one = (int) (dsize * 1 / 10);  
     int two = (int) (dsize * 2 / 10);  
                 int five = (int) (dsize * 5 / 10);  
     int eight = (int) (dsize * 8 / 10);  
                   
261      switch (direction)      switch (direction)
262        {        {
263        case NORTH:        case NORTH:
264          arrow = new Polygon(new int[] { eight, five, one },         arrow = upIcon;
                             new int[] { eight, one, eight }, 3);  
265          break;          break;
266        case SOUTH:        case SOUTH:
267          arrow = new Polygon(new int[] { eight, five, two },         arrow = downIcon;
                             new int[] { two, eight, two }, 3);  
268          break;          break;
269        case EAST:        case EAST:
270        case RIGHT:        case RIGHT:
271          arrow = new Polygon(new int[] { two, eight, two },         arrow = rightIcon;
                             new int[] { two, five, eight }, 3);  
272          break;          break;
273        case WEST:        case WEST:
274        case LEFT:        case LEFT:
275          arrow = new Polygon(new int[] { eight, two, eight },         arrow = leftIcon;
                             new int[] { two, five, eight }, 3);  
276          break;          break;
       default:  
         throw new IllegalArgumentException("Invalid direction given.");  
277        }        }
278      return arrow;  
279        int[] xPoints = arrow.xpoints;
280        int[] yPoints = arrow.ypoints;
281        int x1;
282        int y1;
283        int x2;
284        int y2;
285        x1 = y1 = x2 = y2 = 0;
286    
287        if (size != defaultSize)
288          {
289           float scale = size * 1f / defaultSize;
290           for (int i = 0; i < 3; i++)
291             {
292               xPoints[i] *= scale;
293               yPoints[i] *= scale;
294             }
295          }
296        g.translate(x, y);
297    
298        switch (direction)
299          {
300          case NORTH:
301           x1 = xPoints[0] + 2;
302           y1 = yPoints[0];
303           y2 = y1;
304           x2 = xPoints[2] - 1;
305           break;
306          case SOUTH:
307           x1 = xPoints[1];
308           y1 = yPoints[1] + 1;
309           x2 = xPoints[2] - 1;
310           y2 = yPoints[2];
311           break;
312          case LEFT:
313          case WEST:
314           x1 = xPoints[0] + 1;
315           y1 = yPoints[0] + 1;
316           x2 = x1;
317           y2 = yPoints[2] + 1;
318           break;
319          case RIGHT:
320          case EAST:
321           x1 = xPoints[2];
322           y1 = yPoints[2] + 1;
323           x2 = xPoints[1] - 1;
324           y2 = yPoints[1] + 1;
325           break;
326          }
327        Color saved = g.getColor();
328    
329        if (isEnabled)
330          {
331           g.setColor(Color.DARK_GRAY);
332    
333           if (arrow != null)
334             g.fillPolygon(xPoints, yPoints, 3);
335          }
336        else
337          {
338           g.setColor(Color.GRAY);
339           g.fillPolygon(xPoints, yPoints, 3);
340           g.setColor(Color.WHITE);
341           g.drawLine(x1, y1, x2, y2);
342          }
343        g.setColor(saved);
344        g.translate(-x, -y);
345    }    }
346  }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26