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

import java.awt.*;

/**
 * A DrawnRectangle which draws in 3D.<p>
 *
 * Drawn raised by default, drawing style used by paint() is 
 * controlled by raise() and inset().  Note that raise() and 
 * inset() 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 paintRaised() and paintInset().
 * <p>
 *
 * The current state of the rectangle may be obtained by 
 * calling isRaised().<p>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     DrawnRectangle
 * @see     EtchedRectangle
 * @see     gjt.test.DrawnRectangleTest
 */
public class ThreeDRectangle extends DrawnRectangle {
    protected static ThreeDBorderStyle 
                      _defaultState = ThreeDBorderStyle.RAISED;

    private ThreeDBorderStyle state;

    public ThreeDRectangle(Component drawInto) {
        this(drawInto, _defaultState, 
             _defaultThickness, 0, 0, 0, 0);
    }
    public ThreeDRectangle(Component drawInto, int thickness) {
        this(drawInto, _defaultState, thickness, 0, 0, 0, 0);
    }
    public ThreeDRectangle(Component drawInto, 
                           int x, int y, int w, int h) {
        this(drawInto, 
             _defaultState, _defaultThickness, x, y, w, h);
    }
    public ThreeDRectangle(Component drawInto, int thickness,
                                               int x, int y, 
                                               int w, int h) {
        this(drawInto, _defaultState, thickness, x, y, w, h);
    }
    public ThreeDRectangle(Component drawInto, 
                           ThreeDBorderStyle state, 
                           int thickness, int x, int y, 
                           int w, int h) {
        super(drawInto, thickness, x, y, w, h);
        this.state = state;
    }
    public void paint() {
        if(state == ThreeDBorderStyle.RAISED) paintRaised();
        else                                  paintInset ();
    }
    public void raise() { state = ThreeDBorderStyle.RAISED; }
    public void inset() { state = ThreeDBorderStyle.INSET;  }

    public boolean isRaised() { 
        return state == ThreeDBorderStyle.RAISED; 
    }
    public String paramString() {
        return super.paramString() + "," + state;
    }
    public void paintRaised() {
        Graphics g = drawInto.getGraphics();

        if(g != null) {
            raise               ();
            drawTopLeftLines    (g, brighter());
            drawBottomRightLines(g, getLineColor());
        }
    }    
    public void paintInset() {
        Graphics g = drawInto.getGraphics();

        if(g != null) {
            inset               ();
            drawTopLeftLines    (g, getLineColor());
            drawBottomRightLines(g, brighter());
        }
    }
    private void drawTopLeftLines(Graphics g, Color color) {
        int thick = getThickness();
        g.setColor(color);

        for(int i=0; i < thick; ++i) {
            g.drawLine(x+i, y+i,   x + width-(i+1), y+i);
            g.drawLine(x+i, y+i+1, x+i, y + height-(i+1));  
        }
    }
    private void drawBottomRightLines(Graphics g, Color color) {
        int thick = getThickness();
        g.setColor(color);

        for(int i=1; i <= thick; ++i) {
            g.drawLine(x+i-1, y + height-i, 
                       x + width-i, y + height-i);
            g.drawLine(x + width-i, y+i-1,        
                       x + width-i, y + height-i);
        }
    }
}