summaryrefslogtreecommitdiff
path: root/java/gjt/EtchedRectangle.java
blob: b8026d42f8d402c6b352a42e90f49e026164b7b6 (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
package gjt;

import java.awt.*;

/**
 * A DrawnRectangle that draws an etched border.<p>
 * 
 * Drawn etched in by default, drawing style used by paint() is 
 * controlled by etchedIn() and etchedOut().  Note that 
 * etchedIn() and etchedOut() do not result in anything being 
 * painted, but only set the state for the next call to paint(). 
 * To set the state and paint in one operation, use 
 * paintEtchedIn() and paintEtchedOut().<p>
 * 
 * Although it is permissible to set the thickness of 
 * EtchedRectangles, they tend to loose the etching effect 
 * if thickness is greater than 4.<p>
 *
 * The current state of the rectangle may be obtained by 
 * calling isEtchedIn().
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     DrawnRectangle
 * @see     ThreeDRectangle
 * @see     gjt.test.DrawnRectangleTest
 */
public class EtchedRectangle extends DrawnRectangle {
    protected static Etching _defaultEtching = Etching.IN;
    private Etching etching;

    public EtchedRectangle(Component drawInto) {
        this(drawInto, _defaultEtching, 
             _defaultThickness, 0, 0, 0, 0);
    }
    public EtchedRectangle(Component drawInto, int thickness) {
        this(drawInto, _defaultEtching, thickness, 0, 0, 0, 0);
    }
    public EtchedRectangle(Component drawInto, int x, int y, 
                                               int w, int h) {
        this(drawInto, _defaultEtching, 
             _defaultThickness, x, y, w, h);
    }
    public EtchedRectangle(Component drawInto, int thickness,
                                               int x, int y, 
                                               int w, int h) {
        this(drawInto, _defaultEtching, thickness, x, y, w, h);
    }
    public EtchedRectangle(Component drawInto, Etching etching, 
                           int thickness, int x, int y, 
                           int w, int h) {
        super(drawInto, thickness, x, y, w, h);
        this.etching = etching;
    }
    public void    etchedIn  () { etching = Etching.IN;        }
    public void    etchedOut () { etching = Etching.OUT;       }
    public boolean isEtchedIn() { return etching == Etching.IN;}

    public void paint() {
        if(etching == Etching.IN) paintEtchedIn();
        else                      paintEtchedOut();
    }
    public void paintEtchedIn() {
        Graphics g = drawInto.getGraphics();
        if(g != null)
            paintEtched(g, getLineColor(), brighter());

        etchedIn();
    }
    public void paintEtchedOut() {
        Graphics g = drawInto.getGraphics();
        if(g != null)
            paintEtched(g, brighter(), getLineColor());

        etchedOut();
    }
    public String paramString() { 
        return super.paramString() + "," + etching;
    }
    private void paintEtched(Graphics g, 
                             Color topLeft, 
                             Color bottomRight) {
        int  thickness = getThickness();
        int  w = width  - thickness;
        int  h = height - thickness;

        g.setColor(topLeft);
        for(int i=0; i < thickness/2; ++i) 
            g.drawRect(x+i, y+i, w, h);

        g.setColor(bottomRight);

        for(int i=0; i < thickness/2; ++i) 
            g.drawRect(x+(thickness/2)+i, 
                       y+(thickness/2)+i, w, h);
    }
}