-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuFrame.java
102 lines (84 loc) · 2.96 KB
/
SudokuFrame.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
package sudoku;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class SudokuFrame extends JFrame {
private JPanel buttonSelectionPanel;
private SudokuPanel sPanel;
public SudokuFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Sudoku");
this.setMinimumSize(new Dimension(800,600));
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("Game");
JMenu newGame = new JMenu("New Game");
JMenuItem sixBySixGame = new JMenuItem("6 By 6 Game");
sixBySixGame.addActionListener(new NewGameListener(SudokuPuzzleType.SIXBYSIX,30));
JMenuItem nineByNineGame = new JMenuItem("9 By 9 Game");
nineByNineGame.addActionListener(new NewGameListener(SudokuPuzzleType.NINEBYNINE,26));
JMenuItem twelveByTwelveGame = new JMenuItem("12 By 12 Game");
twelveByTwelveGame.addActionListener(new NewGameListener(SudokuPuzzleType.TWELVEBYTWELVE,20));
newGame.add(sixBySixGame);
newGame.add(nineByNineGame);
newGame.add(twelveByTwelveGame);
//newGame.add(sixteenBySizteenGame);
file.add(newGame);
menuBar.add(file);
this.setJMenuBar(menuBar);
JPanel windowPanel = new JPanel();
windowPanel.setLayout(new FlowLayout());
windowPanel.setPreferredSize(new Dimension(800,600));
buttonSelectionPanel = new JPanel();
buttonSelectionPanel.setPreferredSize(new Dimension(150,500));
sPanel = new SudokuPanel();
windowPanel.add(sPanel);
windowPanel.add(buttonSelectionPanel);
this.add(windowPanel);
rebuildInterface(SudokuPuzzleType.NINEBYNINE, 26);
}
public void rebuildInterface(SudokuPuzzleType puzzleType,int fontSize) {
SudokuPuzzle generatedPuzzle = new SudokuGenerator().generateRandomSudoku(puzzleType);
sPanel.newSudokuPuzzle(generatedPuzzle);
sPanel.setFontSize(fontSize);
buttonSelectionPanel.removeAll();
for(String value : generatedPuzzle.getValidValues()) {
JButton b = new JButton(value);
b.setPreferredSize(new Dimension(50,50));
b.addActionListener(sPanel.new NumActionListener());
buttonSelectionPanel.add(b);
}
sPanel.repaint();
buttonSelectionPanel.revalidate();
buttonSelectionPanel.repaint();
}
private class NewGameListener implements ActionListener {
private SudokuPuzzleType puzzleType;
private int fontSize;
public NewGameListener(SudokuPuzzleType puzzleType,int fontSize) {
this.puzzleType = puzzleType;
this.fontSize = fontSize;
}
@Override
public void actionPerformed(ActionEvent e) {
rebuildInterface(puzzleType,fontSize);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SudokuFrame frame = new SudokuFrame();
frame.setVisible(true);
}
});
}
}