/Users/lyon/j4p/src/ip/apurva/RotationDialog.java

1    package ip.apurva; 
2     
3    import java.awt.*; 
4    import java.awt.event.ActionEvent; 
5    import java.awt.event.ActionListener; 
6     
7     
8    public class RotationDialog extends 
9                            Dialog 
10           implements ActionListener { 
11    
12       private TextField fields[]; 
13       private Label labels[]; 
14       public Button cancelButton = new Button( 
15               "Cancel"); 
16       public Button setButton = new Button("Set"); 
17       public Button Image1 = new Button("Image 1"); 
18       public Button Image2 = new Button("Image 2"); 
19       public Button Play = new Button("Play"); 
20    
21       private int fieldSize; 
22    
23       private static int colSpace = 5; 
24       private static int rowSpace = 10; 
25       private static int colNum = 2; 
26    
27       private int rowNum; 
28    
29       private Panel inputPanel = new Panel(); 
30    
31       public RotationDialog(Frame frame, 
32                         String title, 
33                         String prompts[], 
34                         String defaults[], 
35                         int _fieldSize) { 
36    
37           super(frame, title, false); 
38           initialize(prompts, 
39                      defaults, 
40                      _fieldSize); 
41           pack(); 
42           show(); 
43       } 
44    
45       private void initialize(String prompts[], 
46                               String defaults[], 
47                               int _fieldSize) { 
48    
49           fieldSize = _fieldSize; 
50           rowNum = prompts.length; 
51           labels = new Label[rowNum]; 
52           fields = new TextField[rowNum]; 
53    
54           inputPanel.setLayout( 
55                   new 
56                           GridLayout(rowNum, 
57                                      colNum, 
58                                      colSpace, 
59                                      rowSpace)); 
60           for (int i = 0; i < rowNum; i++) { 
61               labels[i] = new Label(prompts[i]); 
62               if (defaults == null) 
63                   fields[i] = 
64                   new TextField(fieldSize); 
65               else 
66                   fields[i] = 
67                   new TextField(defaults[i], 
68                                 fieldSize); 
69               inputPanel.add(labels[i]); 
70               inputPanel.add(fields[i]); 
71           } 
72           add("Center", inputPanel); 
73    
74    
75           buttonPanel(); 
76    
77       } 
78    
79    
80       private void buttonPanel() { 
81           Panel p2 = new Panel(); 
82           p2.setLayout( 
83                   new FlowLayout(FlowLayout.RIGHT)); 
84           p2.add(Image1); 
85           p2.add(Image2); 
86           p2.add(setButton); 
87           p2.add(Play); 
88           p2.add(cancelButton); 
89           cancelButton.addActionListener(this); 
90           setButton.addActionListener(this); 
91           Image1.addActionListener(this); 
92           Image2.addActionListener(this); 
93           Play.addActionListener(this); 
94           add("South", p2); 
95    
96           pack(); 
97           show(); 
98       } 
99    
100      public void printUserInput() { 
101          String userInput[] = getUserInput(); 
102          for (int i = 0; i < fields.length; i++) { 
103              userInput[i] = fields[i].getText(); 
104              System.out.println(userInput[i]); 
105          } 
106      } 
107   
108      public String[] getUserInput() { 
109          String userInput[] = new String[fields.length]; 
110          for (int i = 0; i < fields.length; i++) 
111              userInput[i] = fields[i].getText(); 
112          return userInput; 
113      } 
114   
115      public static void main(String args[]) { 
116          String title = "Rotation Dialog"; 
117          int fieldSize = 6; 
118   
119          String prompts[] = { 
120              "X (degs):", 
121              "Y (degs):", 
122              "Z (degs):" 
123          }; 
124   
125          String defaults[] = { 
126              "1.0", 
127              "2.0", 
128              "3.0" 
129          }; 
130   
131          new 
132                  RotationDialog(new Frame(), 
133                             title, 
134                             prompts, 
135                             defaults, 
136                             fieldSize); 
137      } 
138   
139      public void actionPerformed(ActionEvent e) { 
140          Button b = (Button) e.getSource(); 
141          if (b == cancelButton) setVisible(false); 
142      } 
143   
144  } 
145