KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > utils > gui > CustomEtchedBorder


1 package snow.utils.gui;
2
3 import java.awt.*;
4 import javax.swing.border.*;
5 import javax.swing.UIManager JavaDoc;
6
7
8
9 /**
10  * Like MatteBorder, but with two lines. ( dark and bright usually )
11  * The effect from the two lines is like an etched border, but one
12  * can specify :
13  * on_top,on_left,on_bottom and on_right define if it should be painted at these locations.
14  */

15
16 public final class CustomEtchedBorder extends EmptyBorder
17 {
18
19     protected Color lightColor;
20     protected Color darkColor;
21
22     private boolean on_top;
23     private boolean on_left;
24     private boolean on_bottom;
25     private boolean on_right;
26
27
28     boolean doUpdateColors = true; // on UI changes
29

30
31    /**
32     * Version with passed constant colors.
33     */

34     public CustomEtchedBorder( boolean on_top, boolean on_left, boolean on_bottom, boolean on_right,
35                                final Color lightColor,
36                                final Color darkColor )
37     {
38         super( on_top ? 2 : 0,
39                on_left ? 2 : 0,
40                on_bottom ? 2 : 0,
41                on_right ? 2 : 0 );
42         this.lightColor = lightColor;
43         this.darkColor = darkColor;
44         this.doUpdateColors = false; // dont change these colors on UI changes
45
this.on_top = on_top;
46         this.on_left = on_left;
47         this.on_bottom = on_bottom;
48         this.on_right = on_right;
49     }
50
51
52
53
54    /**
55     * Version with automatically calculated colors, which then follow UI changes.
56     */

57     public CustomEtchedBorder( boolean on_top, boolean on_left, boolean on_bottom, boolean on_right )
58     {
59         super( on_top ? 2 : 0,
60                on_left ? 2 : 0,
61                on_bottom ? 2 : 0,
62                on_right ? 2 : 0 );
63         this.lightColor = UIManager.getColor("Panel.background").brighter();
64         this.darkColor = UIManager.getColor("Panel.background").darker();
65         this.doUpdateColors = true; // follow on UI changes
66
this.on_top = on_top;
67         this.on_left = on_left;
68         this.on_bottom = on_bottom;
69         this.on_right = on_right;
70     }
71
72
73
74     /**
75      * Paints the matte border.
76      */

77     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
78     {
79       if( this.doUpdateColors )
80        {
81          this.lightColor = UIManager.getColor("Panel.background").brighter();
82          this.darkColor = UIManager.getColor("Panel.background").darker();
83        }
84
85       Insets insets = getBorderInsets(c);
86       Color oldColor = g.getColor();
87       g.translate(x, y);
88
89       g.setColor( this.darkColor );
90
91       if( this.on_top ) g.drawLine( 0,0,width-1,0 );
92       if( this.on_left ) g.drawLine( 0,0,0,height-1 );
93       if( this.on_bottom ) g.drawLine( 1,height-2,width-2,height-2 );
94       if( this.on_right ) g.drawLine( width-2,1,width-2,height-2 );
95
96       g.setColor( this.lightColor );
97
98       if( this.on_bottom ) g.drawLine( 1,height-1,width-1,height-1 );
99       if( this.on_right ) g.drawLine( width-1,1,width-1,height-1 );
100       if( this.on_top ) g.drawLine( 1,1,width-2,1 );
101       if( this.on_left ) g.drawLine( 1,1,1,height-2 );
102
103       g.translate(-x, -y);
104       g.setColor(oldColor);
105     }
106
107     /**
108      * Returns the insets of the border.
109      * @param c the component for which this border insets value applies
110      */

111     public Insets getBorderInsets(Component c) {
112         return getBorderInsets();
113     }
114
115     /**
116      * Reinitialize the insets parameter with this Border's current Insets.
117      * @param c the component for which this border insets value applies
118      * @param insets the object to be reinitialized
119      */

120     public Insets getBorderInsets(Component c, Insets insets) {
121         return computeInsets(insets);
122     }
123
124     /**
125      * Returns the insets of the border.
126      */

127     public Insets getBorderInsets() {
128         return computeInsets(new Insets(0,0,0,0));
129     }
130
131
132     protected Insets computeInsets(Insets insets)
133     {
134       insets.left = left;
135       insets.top = top;
136       insets.right = right;
137       insets.bottom = bottom;
138       return insets;
139     }
140
141
142     /**
143      * Returns whether or not the border is opaque.
144      */

145     public boolean isBorderOpaque()
146     {
147         return true;
148     }
149
150 }
Popular Tags