summaryrefslogtreecommitdiff
path: root/java/gjt/DrawnRectangle.java
blob: e96fd8d36733e0d23e72abd9b6bb8f71e5cd60e1 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package gjt;

import java.awt.*;

/**
 * A Rectangle which draws itself inside of a Component.<p>
 * 
 * DrawnRectangles may have their thickness and line color set, 
 * and are capable of reporting their inner bounds (the area 
 * inside the lines).<p>
 *
 * Default thickness is 2.<p>
 *
 * If not set explicitly, the line color used is three shades 
 * darker than the background color of the Component being 
 * drawn into.<p>
 * 
 * DrawnRectangles may be clear()ed, which clears both the 
 * exterior (the lines) and the interior (the area inside of 
 * the lines) of the DrawnRectangle.<p>
 *
 * DrawnRectangles may also be fill()ed with a specified color 
 * by calling fill(Color), or by calling setFillColor(Color) 
 * followed by fill().<p>
 *
 * By default, the fill Color is the background color of the 
 * Component drawn into.<p>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     ThreeDRectangle
 * @see     EtchedRectangle
 * @see     Border
 * @see     EtchedBorder
 * @see     ThreeDBorder
 * @see     gjt.test.DrawnRectangleTest
 */
public class DrawnRectangle extends Rectangle {
    protected static int    _defaultThickness = 2;

    protected Component drawInto;
    private   int       thick;
    private   Color     lineColor, fillColor;

    public DrawnRectangle(Component drawInto) {
        this(drawInto, _defaultThickness, 0, 0, 0, 0);
    }
    public DrawnRectangle(Component drawInto, int thick) {
        this(drawInto, thick, 0, 0, 0, 0);
    }
    public DrawnRectangle(Component drawInto, int x, int y, 
                                              int w, int h) {
        this(drawInto, _defaultThickness, x, y, w, h);
    }
    public DrawnRectangle(Component drawInto, int thick,
                          int x, int y, int w, int h) {
        Assert.notNull(drawInto);
        Assert.notFalse(thick > 0);

        this.drawInto = drawInto;
        this.thick    = thick;
        reshape(x,y,w,h);
    }
    public Component component()          {return drawInto;    }
    public int  getThickness  ()          {return thick;       }
    public void setThickness  (int thick) {this.thick = thick; }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }
    public void setFillColor(Color fillColor) {
        this.fillColor = fillColor;
    }
    public void fill() {
        fill(getFillColor());      
    }
    public Color getLineColor() {
        if(lineColor == null)
            lineColor = 
            drawInto.getBackground().darker().darker().darker();
        return lineColor;
    }
    public Color getFillColor() {
        if(fillColor == null)
            fillColor = drawInto.getBackground();
        return fillColor;
    }
    public Rectangle getInnerBounds() {
        return new Rectangle(x+thick, y+thick, 
                             width-(thick*2), height-(thick*2));
    }
    public void paint() {
        Graphics g = drawInto.getGraphics();
        paintFlat(g, getLineColor()); 
    }
    private void paintFlat(Graphics g, Color color) {
        if(g != null) {
            g.setColor(color);
            for(int i=0; i < thick; ++i)
                g.drawRect(x+i, y+i, 
                           width-(i*2)-1, height-(i*2)-1);
        }
    }
    public void clearInterior() {
        fill(drawInto.getBackground());
    }
    public void clearExterior() {
        paintFlat(drawInto.getGraphics(), 
                  drawInto.getBackground());
    }
    public void clear() { 
        clearExterior(); 
        clearInterior(); 
    }
    public void fill(Color color) {
        Graphics g = drawInto.getGraphics();

        if(g != null) {
            Rectangle r = getInnerBounds();
            g.setColor(color);
            g.fillRect(r.x, r.y, r.width, r.height);
            setFillColor(color);
        }
    }
    public String toString() {
        return super.toString() + "[" + paramString() + "]";
    }
    public String paramString() {
        return "color=" + getLineColor() + ",thickness=" + 
                thick + ",fillColor=" + getFillColor();
    }
    protected Color brighter() {
        return 
    getLineColor().brighter().brighter().brighter().brighter();
    }
}