-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project1.java
320 lines (227 loc) · 7.88 KB
/
Project1.java
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.*;
//Benjamin Marquis
//CMSC 330 6381
//Project 1
//This project takes input from file and converts into desired GUI
public class Project1 {
static JFrame myFrame;
public static String text;
public static void main(String[] args) throws Exception {
try {
File myFile = new File("input.txt");
Scanner myScanner = new Scanner(myFile);
String line = new String();
String input = "";
boolean myBool = false;
line=myScanner.nextLine();
if(line.startsWith("Window")) {
input = line.substring(7);
while(!myBool){
line = myScanner.nextLine();
if(line.equals("End.")) {
myBool = true;
}
else {
input = input+"\n"+line;
}
//System.out.println(input);
}
windowMethod(input);
}
else {
System.out.println("incorrect input file");
}
}
catch(FileNotFoundException e){
System.out.println("File not found");
}
}
public static void windowMethod(String input){
text = input;
Scanner myScanner = new Scanner(text);
String title = myScanner.next();
text = text.substring(title.length());
//System.out.println("-----------------");
//System.out.println(text);
title = title.substring(1, title.length()-1);
String size1 = myScanner.next();
text = text.substring(size1.length());
size1 = size1.substring(1, size1.length()-1);
String size2 = myScanner.next();
text = text.substring(size2.length()+2);
size2 = size2.substring(0, size2.length()-1);
//System.out.println("Size2: "+size2);
myFrame = new JFrame(title);
myFrame.setSize(Integer.parseInt(size1),Integer.parseInt(size2));
//System.out.println("------2-----------");
//System.out.println(text);
String token = myScanner.next();
text = text.substring(token.length()+1);
//System.out.println("---------------layout:-------------");
//System.out.println(token);
//token = myScanner.next();
//text = text.substring(token.length()+1);
//System.out.println(token);
if(token.equalsIgnoreCase("layout")) {
//System.out.println("Here");
myFrame.setLayout(layoutMethod(text));
token = myScanner.next();
text = text.substring(token.length()+1);
}
else {
System.out.println("Incorrect input file. 1");
}
System.out.println("--------------3---------");
//System.out.println(text);
while(myScanner.hasNext()) {
myFrame.add(widgetMethod(text));
//String line = myScanner.nextLine();
//line = myScanner.nextLine();
//text = text.substring(line.length()+1);
System.out.println(text);
}
myFrame.setVisible(true);
}
public static Component widgetMethod(String text) {
Scanner myScanner = new Scanner(text);
String widgetType = myScanner.next();
System.out.println("WidgetType: " + widgetType);
String token = widgetType;
text = text.substring(token.length()+2);
if(widgetType.equalsIgnoreCase("button")) {
widgetMethod(text);
return buttonMethod(text);
}
else if(widgetType.equalsIgnoreCase("label")) {
widgetMethod(text);
return labelMethod(text);
}
else if(widgetType.equalsIgnoreCase("textfield")) {
System.out.println("adding textfield");
//widgetMethod(text);
return textFieldMethod(text);
}
else if(widgetType.equalsIgnoreCase("radio_button")) {
widgetMethod(text);
return radioButtonMethod(text);
}
else if(widgetType.equalsIgnoreCase("End;")) {
return null;
}
//System.out.println(text);
return widgetMethod(text);
}
public static JButton buttonMethod(String text) {
Scanner myScanner = new Scanner(text);
String buttonLabel = myScanner.next();
String token = buttonLabel;
text = text.substring(token.length()+1);
JButton myButton = new JButton(buttonLabel);
return myButton;
}
public static JLabel labelMethod(String text) {
Scanner myScanner = new Scanner(text);
String labelText = myScanner.next();
String token = labelText;
text = text.substring(token.length()+1);
JLabel myLabel = new JLabel(labelText);
return myLabel;
}
public static JTextField textFieldMethod(String text) {
Scanner myScanner = new Scanner(text);
String fieldLengthString = myScanner.next();
fieldLengthString = fieldLengthString.substring(0, fieldLengthString.length()-1);
System.out.println("Field length string: " +fieldLengthString);
int fieldLength = Integer.parseInt(fieldLengthString);
String token = fieldLengthString;
text = text.substring(token.length()+1);
//token = myScanner.nextLine();
//text=text.substring(token.length()+1);
JTextField myField = new JTextField(fieldLength);
return myField;
}
public static JRadioButton radioButtonMethod(String text) {
//make recursive
Scanner myScanner = new Scanner(text);
String radioButtonLabel = myScanner.next();
String token = radioButtonLabel;
text = text.substring(token.length()+1);
JRadioButton myRadioButton = new JRadioButton(radioButtonLabel);
if(!myScanner.next().equals("radio_button")){
return myRadioButton;
}
else {
return radioButtonMethod(text);
}
}
public static JPanel panelMethod(String text) {
Scanner myScanner = new Scanner(text);
String panelLayout = myScanner.next();
String token = panelLayout;
text = text.substring(token.length()+1);
JPanel myPanel = new JPanel(layoutMethod(text));
return myPanel;
}
Scanner myScanner = new Scanner(text);
//System.out.println(text);
String layout = myScanner.next();
System.out.println("layout: " + layout);
String token = layout;
text = text.substring(token.length()+1);
LayoutManager myLayout;
//System.out.println("layout: "+layout);
if(layout.startsWith("Flow")) {
System.out.println("Here 2");
myLayout = new FlowLayout();
//text=text.substring(layout.length());
System.out.println("returning layout");
return myLayout;
}
else if(layout.startsWith("Grid")) {
String passString1 = layout.substring(4);
passString1 = passString1.substring(1, passString1.length()-1);
System.out.println("pass String1:"+passString1);
int myPass1 = Integer.parseInt(passString1);
boolean moreThan2 = false;
String passString2 = myScanner.next();
token = passString2;
text = text.substring(token.length()+1);
if(passString2.endsWith(",")) {
moreThan2=true;
}
passString2 = passString2.substring(0, passString2.length()-1);
//System.out.println("pass String2:"+passString2);
int myPass2 = Integer.parseInt(passString1);
myLayout = new GridLayout(myPass1,myPass2);
if(moreThan2) {
String myHgapString = myScanner.next();
token = myHgapString;
text = text.substring(token.length()+1);
myHgapString = myHgapString.substring(0, myHgapString.length()-1);
int myHgap = Integer.parseInt(myHgapString);
((GridLayout) myLayout).setHgap(myHgap);
//System.out.println("myHgap " + myHgap);
String myVgapString = myScanner.next();
token = myVgapString;
text = text.substring(token.length()+1);
myVgapString = myVgapString.substring(0, myVgapString.length()-2);
int myVgap = Integer.parseInt(myVgapString);
((GridLayout) myLayout).setVgap(myVgap);
//System.out.println("myVgap " + myVgap);
}
return myLayout;
}
else {
System.out.println("2Incorrect input file.");
}
//return myLayout;
return null;
}
}