Repository navigation
Expand file tree
/
Copy pathMainGUI.java
More file actions
132 lines (116 loc) · 2.82 KB
/
Copy pathMainGUI.java
File metadata and controls
132 lines (116 loc) · 2.82 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
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;
public class MainGUI extends JFrame {
private TowersModel hanoi;
private JMenuBar bar;
private JMenu fileMenu;
private JMenuItem exitItem;
private int x;
private int intTime;
private int from;
private int to;
private int aux;
public MainGUI() {
//
bar = new JMenuBar();
fileMenu = new JMenu("file");
exitItem = new JMenuItem("exit");
exitItem.addActionListener(new ExitItemListener());
fileMenu.add(exitItem);
bar.add(fileMenu);
setJMenuBar(bar);
//
//for frame
setSize(900, 600);
setTitle("Towers Of Hanoi");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
String in = JOptionPane.showInputDialog("How many rings: ");
if(in == null)
System.exit(1);
this.x = Integer.parseInt(in);
in = JOptionPane.showInputDialog("seconds between moves:(hint .5 for half sec)");
double time = Double.parseDouble(in);
if(in == null)
System.exit(1);
in = JOptionPane.showInputDialog("\"From\" Peg:(1-3)");
if(in == null)
System.exit(1);
from = Integer.parseInt(in);
if(from > 3 || from < 1)
System.exit(1);
in = JOptionPane.showInputDialog("\"to\" Peg:(1-3)");
if(in == null)
System.exit(1);
to = Integer.parseInt(in);
if(to > 3 || to < 1)
System.exit(1);
in = JOptionPane.showInputDialog("\"Aux\" Peg:(1-3)");
if(in == null)
System.exit(1);
aux = Integer.parseInt(in);
if(aux > 3 || aux < 1)
System.exit(1);
this.intTime = (int) (time * 1000);
if(x > 14)
x = 14;
if(x <= 0)
x = 1;
hanoi = new TowersModel(this, x, intTime,calcPeg(from));
setLocationRelativeTo(null);
//setResizable(false);
setVisible(true);
}
public void startAnimation(){
hanoi.towers(x,calcPeg(from),calcPeg(to),calcPeg(aux));
}
public void reverseAnimation(){
hanoi.towers(x,calcPeg(to),calcPeg(from),calcPeg(aux));
}
public static void main(String[] args) {
boolean done = false;
while(!done){
MainGUI m = new MainGUI();
try {
Thread.sleep(m.getIntTime());
} catch (InterruptedException e) {
System.err.println(e);
System.exit(1);
}
m.startAnimation();
try {
Thread.sleep(m.getIntTime());
} catch (InterruptedException e) {
System.err.println(e);
System.exit(1);
}
}
}
//
private class ExitItemListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
exitItemActionPerformed(evt);
}
}
private void exitItemActionPerformed(ActionEvent evet){
System.exit(0);
}
//
public int getIntTime() {
return intTime;
}
public int calcPeg(int peg){
if(peg == 1){
return TowersComponent.P_ONE_BASE;
}
if(peg == 2){
return TowersComponent.P_TWO_BASE;
}
if(peg == 3)
return TowersComponent.P_THREE_BASE;
return TowersComponent.P_ONE_BASE;
}
}