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

import java.awt.*;
import gjt.DrawingPanel;
import gjt.Separator;
import gjt.RowLayout;
import gjt.rubberband.*;

/**
 * A simple drawing applet that demonstrates the utility of 
 * the gjt.rubberband package.<p>
 *
 * Note that this unit test also serves as the unit test for
 * gjt.DrawingPanel.<p>
 *
 * @version 1.0, April 25, 1996
 * @author  David Geary
 * @see     gjt.test.UnitTest
 * @see     gjt.DrawingPanel
 * @see     gjt.rubberband.Rubberband
 * @see     gjt.rubberband.RubberbandLine
 * @see     gjt.rubberband.RubberbandRectangle
 * @see     gjt.rubberband.RubberbandEllipse
 * @see     gjt.rubberband.RubberbandPanel
 */
public class RubberbandTest extends UnitTest {
    public String title() { 
        return "Rubberband Test"; 
    }
    public Panel centerPanel() { 
        return new RubberbandTestPanel(); 
    }
}

class RubberbandTestPanel extends Panel {
    private DrawingPanel drawingPanel;
    private ChoicePanel  choicePanel;

    public RubberbandTestPanel() {
        drawingPanel = new DrawingPanel();
        choicePanel  = new ChoicePanel(drawingPanel);

        setLayout(new BorderLayout());
        add("North",  choicePanel);
        add("Center", drawingPanel);
    }
}

class ChoicePanel extends Panel {
    private DrawingPanel drawingPanel;
    private Color        color;
    private Checkbox     fillCheckbox = new Checkbox();

    public ChoicePanel(DrawingPanel drawingPanel) {
        Panel    choicePanel     = new Panel();
        Choice   geometricChoice = new Choice();
        Choice   colorChoice     = new Choice();

        this.drawingPanel = drawingPanel;
    
        geometricChoice.addItem("Lines");
        geometricChoice.addItem("Rectangles");
        geometricChoice.addItem("Ellipses");

        colorChoice.addItem("Black");
        colorChoice.addItem("Red");
        colorChoice.addItem("Blue");
        colorChoice.addItem("Gray");
        colorChoice.addItem("White");

        choicePanel.setLayout(new RowLayout(10));
        choicePanel.add(new Label("Shape:"));
        choicePanel.add(geometricChoice);
        choicePanel.add(new Label("Color:"));
        choicePanel.add(colorChoice);
        choicePanel.add(new Label("Fill:"));
        choicePanel.add(fillCheckbox);

        setLayout(new BorderLayout());
        add("Center", choicePanel);
        add("South",  new Separator());
    }
    public boolean action(Event event, Object what) {
        if(event.target instanceof Checkbox) {
            drawingPanel.setFill(fillCheckbox.getState());
        }
        else if(event.target instanceof Choice) {
            if(((String)what).equals("Lines")) {     
                fillCheckbox.setState(false);
                drawingPanel.drawLines();
            }
            else if(((String)what).equals("Rectangles")) {
                System.out.println("Rectangles");
                drawingPanel.drawRectangles();
            }
            else if(((String)what).equals("Ellipses"))   
                drawingPanel.drawEllipses  ();
            else if(((String)what).equals("Black")) 
                drawingPanel.setColor(Color.black);
            else if(((String)what).equals("Red"))   
                drawingPanel.setColor(Color.red);
            else if(((String)what).equals("Blue"))  
                drawingPanel.setColor(Color.blue);
            else if(((String)what).equals("Gray"))  
                drawingPanel.setColor(Color.gray);
            else if(((String)what).equals("White")) 
                drawingPanel.setColor(Color.white);
        }
        return true;
    }
    public Insets insets() { return new Insets(5,0,5,0); }
}