This repository was archived by the owner on Dec 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicSquareApplet.java
More file actions
120 lines (92 loc) · 2.73 KB
/
MagicSquareApplet.java
File metadata and controls
120 lines (92 loc) · 2.73 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
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
//VS4E -- DO NOT REMOVE THIS LINE!
public class MagicSquareApplet extends JApplet implements ActionListener{
private JTextField testField;
private final int boardSize = 3;
private ArrayList<JTextField> fields = new ArrayList<JTextField>();
private ArrayList<Integer> values ;
private JLabel lbl;
JButton btn;
private static final long serialVersionUID = 1L;
public void init() {
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponents() {
setSize(320, 240);
drawBoard();
}
public void drawBoard() {
this.setSize(320,240);
JPanel parent = new JPanel(new GridLayout(3, 1));
JPanel board = new JPanel(new GridLayout(boardSize, boardSize));
for (int i = 0; i < Math.pow(boardSize, 2); i++) {
JTextField txField = new JTextField();
board.add(txField);
fields.add(txField);
}
btn = new JButton("Show result!");
btn.addActionListener(this);
lbl = new JLabel();
parent.add(board);
parent.add(lbl);
parent.add(btn);
this.add(parent);
this.setVisible(true);
try {
UIManager.setLookAndFeel("com.incors.plaf.kunststoff.KunststoffLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception ex) {}
}
private boolean testInput(String i) {
if (i.length() == 0) return false;
Pattern p = Pattern.compile("^[0-9]*$");
Matcher m = p.matcher(i);
boolean isMatch = false;
while (m.find()) {
isMatch = true;
}
return isMatch;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
values = new ArrayList<Integer>();
MagicSquare sq = new MagicSquare(boardSize);
for (JTextField f : fields) {
String input = f.getText();
if (testInput(input)) {
values.add(Integer.valueOf(f.getText()));
sq.addInt(Integer.valueOf(f.getText()));
} else {
lbl.setText("Invalid input. Try again");
}
}
if (sq.isMagic()) {
this.setBackground(Color.YELLOW);
lbl.setText("Congratulations. You made a magic square");
} else {
lbl.setText("Too bad. Try again" );
}
}
}