1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package gjt;
import java.awt.*;
/**
* A separator that is drawn either vertically or horizontally
* depending upon how it is laid out. Can be drawn either
* etched-in or etched-out, with varying thicknesses. Both
* thickness and etching are settable at construction time
* only.<p>
*
* Default thickness is 2 pixels and default etching is
* Etching.IN. Note that thicknesses greater than 4 loose the
* etching effect.<p>
*
* @version 1.0, Apr 1 1996
* @author David Geary
* @see Etching
* @see gjt.test.SeparatorTest
*/
public class Separator extends Canvas {
static private Etching _defaultEtching = Etching.IN;
static private int _defaultThickness = 2;
private Etching etching;
private int thickness;
public Separator() {
this(_defaultThickness, _defaultEtching);
}
public Separator(int thickness) {
this(thickness, _defaultEtching);
}
public Separator(Etching etching) {
this(_defaultThickness, etching);
}
public Separator(int thickness, Etching etching) {
this.etching = etching;
this.thickness = thickness;
resize(thickness, thickness);
}
public Dimension minimumSize() {
return preferredSize();
}
public Dimension preferredSize() {
return new Dimension(thickness, thickness);
}
public void paint(Graphics g) {
Dimension size = size();
Color brighter = getBackground().brighter().brighter();
Color darker = getBackground().darker().darker();
if(etching == Etching.IN) {
if(size.width > size.height)
paintHorizontal(g, size, darker, brighter);
else
paintVertical(g, size, darker, brighter);
}
else {
if(size.width > size.height)
paintHorizontal(g, size, brighter, darker);
else
paintVertical(g, size, brighter, darker);
}
}
public String paramString() {
Dimension size = size();
Orientation orient = size.width > size.height ?
Orientation.HORIZONTAL :
Orientation.VERTICAL;
return super.paramString() + "thickness=" +
thickness + "," + etching + "," + orient;
}
private void paintHorizontal(Graphics g, Dimension size,
Color top, Color bottom) {
g.setColor(top);
g.fillRect(0, (size.height/2) - (thickness/2),
size.width, thickness/2);
g.setColor(bottom);
g.fillRect(0, size.height/2, size.width, thickness/2);
}
private void paintVertical(Graphics g, Dimension size,
Color left, Color right) {
g.setColor(left);
g.fillRect((size.width/2) - (thickness/2),
0, thickness/2, size.height);
g.setColor(right);
g.fillRect(size.width/2, 0, thickness/2, size.height);
}
}
|