-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathPanel.java
More file actions
285 lines (254 loc) · 8.27 KB
/
ShortestPathPanel.java
File metadata and controls
285 lines (254 loc) · 8.27 KB
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class ShortestPathPanel extends JPanel{
ShortestPath sp = new ShortestPath();
//labels1
private JLabel jlbInput1 = new JLabel("Source:");
private JLabel jlbInput2 = new JLabel("Destination:");
//the text field
private JTextField jtfInput1 = new JTextField(3);
private JTextField jtfInput2 = new JTextField(3);
//text area
private JTextArea jta = new JTextArea();
//all buttons
private JButton jbtB_Ford = new JButton("B.Ford");
private JButton jbtDijk = new JButton("Dijkstra");
private JButton jbtInput = new JButton("Run");
private JButton jbtExit = new JButton("Exit");
//panels
private PaintRoute jspPanel = new PaintRoute();
private JPanel jplControl = new JPanel(new BorderLayout(5,5));
private JPanel jplButton1 = new JPanel(new GridLayout(2,2,5,5));
private JPanel jplButton2 = new JPanel(new GridLayout(2,2,5,5));
private JPanel jplText = new JPanel(new BorderLayout());
//some important variable
boolean initialized1 = false;
boolean initialized2 = false;
boolean found = false;
//messages
String message1 = "Input Source and\nDest then choose\nthe alg.";
String message2 = "Click Execute\nbutton to run\nthe program";
public ShortestPathPanel(){
//input area
jplButton1.add(jlbInput1);
jplButton1.add(jtfInput1);
jplButton1.add(jlbInput2);
jplButton1.add(jtfInput2);
jplButton1.setBorder(new TitledBorder("Input"));
//button area
jplButton2.add(jbtB_Ford);
jplButton2.add(jbtDijk);
jbtInput.setBackground(Color.BLUE);
jbtInput.setForeground(Color.WHITE);
jplButton2.add(jbtInput);
jplButton2.add(jbtExit);
jplButton2.setBorder(new TitledBorder("Control"));
//text area
jplText.add(jta, BorderLayout.CENTER);
jta.setPreferredSize(new Dimension(100,70));
jta.setText(message1);
jplText.setBorder(new TitledBorder("Txt Area"));
jplControl.add(jplButton1, BorderLayout.WEST);
jplControl.add(jplButton2, BorderLayout.CENTER);
jplControl.add(jplText, BorderLayout.EAST);
jplControl.setBorder(new TitledBorder(""));
jspPanel.setBorder(new TitledBorder("Display"));
jspPanel.setBackground(Color.BLACK);
this.setLayout(new BorderLayout());
this.add(jspPanel, BorderLayout.CENTER);
this.add(jplControl, BorderLayout.SOUTH);
jbtB_Ford.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int key1 = Integer.parseInt(jtfInput1.getText())-1;
int key2 = Integer.parseInt(jtfInput2.getText())-1;
if (key1 < 0 || key2 < 0 ||
key1 > 6 || key2 > 6 ||
key1 == key2)
JOptionPane.showMessageDialog(null,
"Input must be diffirent positive integers at most 6!");
else{
sp.setSource(key1);
//System.out.println("Source isA:" + key1);
sp.setDest(key2);
try {
sp.initialize1();
initialized1 = true;
initialized2 = false;
jta.setText(message2);
jspPanel.repaint();
} catch (IOException e1) {}
}
}
});
jbtDijk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int key1 = Integer.parseInt(jtfInput1.getText())-1;
int key2 = Integer.parseInt(jtfInput2.getText())-1;
if (key1 < 0 || key2 < 0 ||
key1 > 6 || key2 > 6 ||
key1 == key2)
JOptionPane.showMessageDialog(null,
"Input must be diffirent positive integers at most 6!");
else{
sp.setSource(key1);
//System.out.println("Source isB:" + key1);
sp.setDest(key2);
try {
sp.initialize2();
initialized2 = true;
initialized1 = false;
jta.setText(message2);
jspPanel.repaint();
} catch (IOException e1) {}
}
}
});
jbtInput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//bell-ford
if (initialized1){
try {
if(sp.search1()){
found = true;
jspPanel.repaint();
jta.setText(message1);
}
} catch (IOException e1) {}
}
//Dijkstra
if (initialized2){
try {
sp.search2();
found = true;
jspPanel.repaint();
jta.setText(message1);
} catch (IOException e1) {}
}
}
});
jbtExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
});
}
class PaintRoute extends JPanel{
private int radius = 15;
private Map<Integer, int[]> vIndex = new HashMap<Integer, int[]>();
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);
//draw the nodes
int centerX = getWidth()/2;
int centerY = getHeight()/2;
drawNodes(g, centerX, centerY, radius);
//store the nodes' coordinates
coordinateMap(centerX, centerY);
if (initialized1 || initialized2){
for (int i = 0; i < 6; i++){
for(int j = 0 ; j < 6 ; j++){
if (sp.getMatrix()[i][j] != 100){
paintArrow(g, vIndex.get(i)[0], vIndex.get(i)[1],
vIndex.get(j)[0], vIndex.get(j)[1]);
}
}
}
}
if (found){
drawPath(g, sp.getPath());
found = false;
}
}
private void drawPath(Graphics g, Stack stk){
g.setColor(Color.WHITE);
int x = (Integer) stk.pop();
int y = (Integer) stk.pop();
x = x -1;
y = y -1;
paintArrow(g, vIndex.get(x)[0], vIndex.get(x)[1],
vIndex.get(y)[0], vIndex.get(y)[1]);
while(!stk.isEmpty()){
x = y;
y = (Integer) stk.pop();
y = y - 1;
paintArrow(g, vIndex.get(x)[0], vIndex.get(x)[1],
vIndex.get(y)[0], vIndex.get(y)[1]);
}
g.setColor(Color.BLUE);
}
private void coordinateMap(int centerX, int centerY){
/*
* node1 (centerX-150, centerY)
* node2 (centerX-70, centerY-90)
* node3 (centerX-70, centerY+90)
* node4 (centerX+70, centerY-90)
* node5 (centerX+70, centerY+90)
* node6 (centerX+150, centerY)
*/
int[] node1 = {centerX-150, centerY};
int[] node2 = {centerX-70, centerY-90};
int[] node3 = {centerX-70, centerY+90};
int[] node4 = {centerX+70, centerY-90};
int[] node5 = {centerX+70, centerY+90};
int[] node6 = {centerX+150, centerY};
vIndex.put(0, node1);
vIndex.put(1, node2);
vIndex.put(2, node3);
vIndex.put(3, node4);
vIndex.put(4, node5);
vIndex.put(5, node6);
}
private void paintArrow(Graphics g, int x0, int y0, int x1,int y1){
int deltaX = x1 - x0;
int deltaY = y1 - y0;
double frac = 0.05;
g.drawLine(x0,y0,x1,y1);
g.drawLine(x0 + (int)((1-frac)*deltaX + frac*deltaY),
y0 + (int)((1-frac)*deltaY - frac*deltaX),
x1, y1);
g.drawLine(x0 + (int)((1-frac)*deltaX - frac*deltaY),
y0 + (int)((1-frac)*deltaY + frac*deltaX),
x1, y1);
}
private void drawNodes(Graphics g , int centerX, int centerY, int radius){
//node1
g.drawOval(centerX-150-2*radius, centerY-radius, 2*radius, 2*radius);
g.drawString("1", centerX-152-radius, centerY+4);
//node2
g.drawOval(centerX-70-radius, centerY-90-2*radius, 2*radius, 2*radius);
g.drawString("2", centerX-70-2, centerY-90-radius+4);
//node3
g.drawOval(centerX-70-radius, centerY+90, 2*radius, 2*radius);
g.drawString("3", centerX-70-2, centerY+90+radius+4);
//node4
g.drawOval(centerX+70-radius, centerY-90-2*radius, 2*radius, 2*radius);
g.drawString("4", centerX+70-2, centerY-90-radius+4);
//node5
g.drawOval(centerX+70-radius, centerY+90, 2*radius, 2*radius);
g.drawString("5", centerX+70-2, centerY+90+radius+4);
//node6
g.drawOval(centerX+150, centerY-radius, 2*radius, 2*radius);
g.drawString("6", centerX+148+radius, centerY+4);
}
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.add(new ShortestPathPanel());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("ShortestPath");
frame.setSize(500, 500);
frame.setVisible(true);
}
}