KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > icon > SortArrowIcon


1 /*
2  * $Id: SortArrowIcon.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.icon;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Graphics JavaDoc;
13
14 import javax.swing.Icon JavaDoc;
15
16 public class SortArrowIcon implements Icon JavaDoc {
17     private boolean ascending = true;
18     //REMIND(aim): support more configurable sizes
19
private int width = 8;
20     private int height = 8;
21
22     public SortArrowIcon(boolean ascending) {
23         this.ascending = ascending;
24     }
25
26     public int getIconWidth() {
27         return width;
28     }
29
30     public int getIconHeight() {
31         return height;
32     }
33
34     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
35         Color JavaDoc base = c.getBackground();
36         Color JavaDoc shadow = base.darker().darker();
37         Color JavaDoc highlight = Color.white;
38
39         if (ascending) {
40             g.setColor(shadow);
41             int y1 = height-1;
42             for(int x1=0; x1 < width/2 ; x1++) {
43                 g.drawLine(x + x1, y + y1, x + x1, y + y1 - 1);
44                 y1 -= ((x1+1 == (width/2)-1)? 1 : 2);
45             }
46             g.setColor(highlight);
47             y1 = height-1;
48             for (int x1 = width-1; x1 >= width / 2; x1--) {
49                 g.drawLine(x + x1, y + y1, x + x1, y + y1 - 1);
50                 y1 -= ( (x1 - 1 == (width / 2)) ? 1 : 2);
51             }
52             g.drawLine(x + 1, y + height-1, x + width - 1, y + height-1);
53         } else {
54             // descending
55
g.setColor(shadow);
56             int y1 = 1;
57             for (int x1 = 0; x1 < width/2 ; x1++) {
58                 g.drawLine(x + x1, y + y1, x + x1, y + y1 + 1);
59                 y1 += (x1+1 == (width/2-1))? 1 : 2;
60             }
61             g.setColor(highlight);
62             y1 = 1;
63             for (int x1 = width - 1; x1 >= width/2; x1--) {
64                 g.drawLine(x + x1, y + y1, x + x1, y + y1 + 1);
65                 y1 += (x1-1 == width/2)? 1 : 2;
66             }
67             g.setColor(shadow);
68             g.drawLine(x + 1, y + 1, x + width - 1, y + 1);
69         }
70     }
71 }
72
Popular Tags