KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > swing > support > ArrowIcon


1 /**
2  * Copyright (c) 2003 held jointly by the individual authors.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; with out even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * > http://www.gnu.org/copyleft/lesser.html
19  * > http://www.opensource.org/licenses/lgpl-license.php
20  */

21 package net.mlw.vlh.swing.support;
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Graphics JavaDoc;
26
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.SwingConstants JavaDoc;
29
30 /**
31  * @author Matthew L. Wilson
32  * @version $Revision: 1.1 $ $Date: 2005/03/16 12:16:23 $
33  */

34 public class ArrowIcon implements Icon JavaDoc
35 {
36    private int width = 4;
37
38    private int height = 8;
39
40    private int numberOfArrows;
41
42    private int[] xPoints = new int[4];
43
44    private int[] yPoints = new int[4];
45
46    public ArrowIcon(int direction)
47    {
48       this(direction, 1);
49    }
50
51    public ArrowIcon(int direction, int numberOfArrows)
52    {
53       this.numberOfArrows = numberOfArrows;
54       if (direction == SwingConstants.LEFT)
55       {
56          xPoints[0] = width;
57          yPoints[0] = -1;
58          xPoints[1] = width;
59          yPoints[1] = height;
60          xPoints[2] = 0;
61          yPoints[2] = height / 2;
62          xPoints[3] = 0;
63          yPoints[3] = height / 2 - 1;
64       }
65       else if (direction == SwingConstants.RIGHT)
66       {
67          xPoints[0] = 0;
68          yPoints[0] = -1;
69          xPoints[1] = 0;
70          yPoints[1] = height;
71          xPoints[2] = width;
72          yPoints[2] = height / 2;
73          xPoints[3] = width;
74          yPoints[3] = height / 2 - 1;
75       }
76       else
77       {
78          throw new RuntimeException JavaDoc("Valid directions: SwingConstants.LEFT, SwingConstants.RIGHT.");
79       }
80    }
81
82    public int getIconHeight()
83    {
84       return height;
85    }
86
87    public int getIconWidth()
88    {
89       return (width * numberOfArrows);
90    }
91
92    public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y)
93    {
94       int length = xPoints.length;
95       
96       for (int number = 0; number < numberOfArrows; number++)
97       {
98          int adjustedXPoints[] = new int[length];
99          int adjustedYPoints[] = new int[length];
100
101          for (int i = 0; i < length; i++)
102          {
103             adjustedXPoints[i] = xPoints[i] + x + (number*width);
104             adjustedYPoints[i] = yPoints[i] + y;
105          }
106
107          if (c.isEnabled())
108          {
109             g.setColor(Color.black);
110          }
111          else
112          {
113             g.setColor(Color.gray);
114          }
115
116          g.fillPolygon(adjustedXPoints, adjustedYPoints, length);
117       }
118    }
119
120 }
Popular Tags