-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainFrame.java
230 lines (202 loc) · 6.88 KB
/
MainFrame.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
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MainFrame {
private final int selectionCount = 5;
private int selection = 0;
private JFrame frame = new JFrame("Text2Graph");
private JRadioButton[] radioButton = new JRadioButton[selectionCount];
private JTextField textFieldLeft = new JTextField(10);
private JTextField textFieldRight = new JTextField(10);
private JTextArea textAreaIn = new JTextArea(
"Enter text here");
private JTextArea textAreaOut = new JTextArea();
private JScrollPane scrollPaneIn = new JScrollPane(
textAreaIn);
private final JScrollPane scrollPaneOut = new JScrollPane(
textAreaOut);
private JPanel groupPanel = new JPanel();
private JPanel imagePanel = new JPanel();
private ImageViewer imageViewer = new ImageViewer();
private ImageFrame imageFrame = null;
private Text2Graph t2g = new Text2Graph();
/**
* javadoc×¢ÊÍÄÚÈÝ
*
* @since 1.0
* @version 1.1
* @author xxx
*/
public void init() throws IOException {
try {
t2g.init();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] selectionText = { "Bridge Words", "New Text",
"Shortest Path", "Random Walk", "Show Graph" };
assert (selectionText.length == selectionCount);
JPanel radioPanel = new JPanel();
ButtonGroup radioGroup = new ButtonGroup();
for (int i = 0; i < selectionCount; i++) {
radioButton[i] = new JRadioButton(selectionText[i]);
radioButton[i]
.addItemListener(new RadioItemListener());
radioGroup.add(radioButton[i]);
radioPanel.add(radioButton[i]);
}
radioButton[0].setSelected(true);
frame.setLayout(new BorderLayout(1, 3));
frame.add(radioPanel, BorderLayout.NORTH);
JButton confirmButton = new JButton("OK");
JPanel buttonPanel = new JPanel();
buttonPanel.add(textFieldLeft);
buttonPanel.add(textFieldRight);
buttonPanel.add(confirmButton);
confirmButton
.addActionListener(new ButtonClickedListener());
textAreaIn.setLineWrap(true);
textAreaIn.setWrapStyleWord(true);
textAreaIn.setFont(new Font("Serif", Font.PLAIN, 20));
textAreaOut.setEditable(false);
textAreaOut.setLineWrap(true);
textAreaOut.setWrapStyleWord(true);
textAreaOut.setFont(new Font("Serif", Font.PLAIN, 20));
scrollPaneIn.setPreferredSize(new Dimension(400, 400));
scrollPaneIn.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPaneOut.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel textPanel = new JPanel();
textPanel.setLayout(new BorderLayout(1, 3));
textPanel.add(scrollPaneIn, BorderLayout.WEST);
textPanel.add(scrollPaneOut);
groupPanel.setLayout(new BorderLayout(1, 3));
groupPanel.add(buttonPanel, BorderLayout.NORTH);
groupPanel.add(textPanel);
t2g.outputGraph();
imageViewer.setImage(Configuration.JpgImagePath);
imagePanel.setLayout(new BorderLayout());
imagePanel.add(imageViewer);
imagePanel.setPreferredSize(new Dimension(800, 600));
frame.add(groupPanel);
frame.setSize(800, 400);
frame.setPreferredSize(new Dimension(800, 400));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class ButtonClickedListener
implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String textOut = "";
switch (selection) {
case 0:
textOut = t2g.queryBridgeWords(
textFieldLeft.getText().toLowerCase(),
textFieldRight.getText().toLowerCase());
break;
case 1:
textOut = t2g.generateNewText(
textAreaIn.getText().toLowerCase());
break;
case 2:
if (textFieldLeft.getText().equals("")) {
textOut = "Source word should not be empty!";
break;
}
textOut = t2g.calcShortestPath(
textFieldLeft.getText().toLowerCase(),
textFieldRight.getText().toLowerCase());
t2g.outputGraph();
if (imageFrame == null) {
imageFrame = new ImageFrame("Shortest Path");
}
imageFrame.setImage(Configuration.JpgImagePath);
imageFrame.setVisible(true);
break;
case 3:
textOut = t2g.randomWalk();
break;
default:
break;
}
textAreaOut.setText(textOut);
}
}
public class RadioItemListener implements ItemListener {
/**
* javadoc×¢ÊÍÄÚÈÝ
*
* @since 1.0
* @version 1.1
* @author xxx
*/
public void itemStateChanged(ItemEvent e) {
JRadioButton item = (JRadioButton) e.getSource();
textAreaOut.setText("");
if (item != radioButton[4]
&& imagePanel.isShowing()) {
frame.remove(imagePanel);
frame.add(groupPanel);
frame.repaint();
}
if (item == radioButton[1]
|| item == radioButton[3]) {
textFieldLeft.setVisible(false);
textFieldRight.setVisible(false);
} else {
textFieldLeft.setVisible(true);
textFieldRight.setVisible(true);
}
if (item == radioButton[0]) {
selection = 0;
scrollPaneIn.setVisible(false);
} else if (item == radioButton[1]) {
selection = 1;
scrollPaneIn.setVisible(true);
} else if (item == radioButton[2]) {
selection = 2;
scrollPaneIn.setVisible(false);
} else if (item == radioButton[3]) {
selection = 3;
scrollPaneIn.setVisible(false);
} else {
selection = 4;
frame.remove(groupPanel);
frame.add(imagePanel);
frame.repaint();
}
frame.pack();
}
}
class ImageFrame extends JFrame {
private ImageViewer imageViewer = new ImageViewer();
ImageFrame(String title) {
super(title);
imageViewer.setImage(Configuration.JpgImagePath);
getContentPane().add(imageViewer);
setPreferredSize(new Dimension(600, 400));
pack();
}
public void setImage(String path) {
imageViewer.setImage(path);
}
}
}