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

import java.awt.*;
import gjt.rubberband.*;

/**
 * An extension of gjt.rubberband.RubberbandPanel which serves
 * as a panel used for drawing simple shapes (lines, rectangles,
 * and ellipses).  The shapes may be filled (except for lines,
 * of course), and the color of the shapes may be specified.<p>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     gjt.rubberband.RubberbandPanel
 * @see     gjt.rubberband.RubberbandEllipse
 * @see     gjt.rubberband.RubberbandLine
 * @see     gjt.rubberband.RubberbandRectangle
 * @see     gjt.test.RubberbandTest
 * @see     gjt.test.ToolbarTest
 */
public class DrawingPanel extends RubberbandPanel {
    private Rubberband rbLine, rbRect, rbEllipse;
    private Color   color;
    private boolean fill;

    public DrawingPanel() {
        rbLine    = new RubberbandLine     (this);
        rbRect    = new RubberbandRectangle(this);
        rbEllipse = new RubberbandEllipse  (this);

        setRubberband(rbLine);
    }
    public void drawLines     () { setRubberband(rbLine);    } 
    public void drawRectangles() { setRubberband(rbRect);    } 
    public void drawEllipses  () { setRubberband(rbEllipse); } 
    
    public void  setColor(Color color) { this.color = color; }
    public Color getColor()            { return color;       }

    public void    setFill(boolean b) { fill = b;    }
    public boolean getFill()          { return fill; }

    public boolean mouseUp(Event event, int x, int y) {
        Rubberband rb = getRubberband();
        Graphics   g  = getGraphics();

        super.mouseUp(event, x, y);
        g.setColor(color);

        if(rb == rbLine)         drawLine     (rb, g);
        else if(rb == rbRect)    drawRectangle(rb, g);
        else if(rb == rbEllipse) drawEllipse  (rb, g);

        return true;
    }
    protected void drawLine(Rubberband rb, Graphics g) {
        Point anchor = rb.getAnchor(), end = rb.getEnd();
        g.drawLine(anchor.x, anchor.y, end.x, end.y);
    }
    protected void drawRectangle(Rubberband rb, Graphics g) {
        Rectangle r = rb.bounds();

        if(fill) g.fillRect(r.x, r.y, r.width, r.height);
        else     g.drawRect(r.x, r.y, r.width, r.height);
    }
    protected void drawEllipse(Rubberband rb, Graphics g) {
        Rectangle r = rb.bounds();

        if(fill) g.fillArc(r.x, r.y, r.width, r.height, 0, 360);
        else     g.drawArc(r.x, r.y, r.width, r.height, 0, 360);
    }
}