-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabyrinthGUI.java
123 lines (113 loc) · 4.45 KB
/
LabyrinthGUI.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
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
public class LabyrinthGUI {
private JFrame frame;
private GameManager gameArea;
private Database database;
private JFrame highscoresFrame;
private JMenuBar menuBar;
private JMenu menu;
/**
* Creates the main frame, creates a menu, and creates a secondary frame for high scores which is initially invisible.
*/
public LabyrinthGUI() {
frame = new JFrame("Labyrinth");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
gameArea = new GameManager();
database = gameArea.getDatabase();
//database.emptyTheTable();
updateHighscoresFrame();
} catch (SQLException ex) {
Logger.getLogger(LabyrinthGUI.class.getName()).log(Level.SEVERE, null, ex);
}
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
menu = new JMenu("Menu");
JMenuItem highscores = new JMenuItem("Highscores");
highscores.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
gameArea.pause();
updateHighscoresFrame();
highscoresFrame.setVisible(true);
}
});
menu.add(highscores);
JMenuItem pause = new JMenuItem("Pause");
pause.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
gameArea.pause();
}
});
menu.add(pause);
JMenuItem restart = new JMenuItem("Restart");
restart.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
gameArea.restart(0);
gameArea.restartTimer();
}
});
menu.add(restart);
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(exit);
menuBar.add(menu);
gameArea.setFocusable(true);
frame.getContentPane().add(gameArea, BorderLayout.CENTER);
frame.getContentPane().add(gameArea.getTimer(), BorderLayout.SOUTH);
frame.setPreferredSize(new Dimension(775, 650));
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
/**
* Creates a new frame which contains a table of the top 10 high scores.
*/
private void updateHighscoresFrame(){
try {
highscoresFrame = new JFrame("Highscores");
JTable table = new JTable(database.getDataMatrix(),database.getColumnNamesArray());
table.setEnabled(false);
table.setRowHeight(50);
JScrollPane sp = new JScrollPane(table);
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(0).setPreferredWidth(50);
columnModel.getColumn(1).setPreferredWidth(230);
columnModel.getColumn(2).setPreferredWidth(120);
columnModel.getColumn(3).setPreferredWidth(200);
DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
cellRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
columnModel.getColumn(0).setCellRenderer(cellRenderer);
columnModel.getColumn(1).setCellRenderer(cellRenderer);
columnModel.getColumn(2).setCellRenderer(cellRenderer);
columnModel.getColumn(3).setCellRenderer(cellRenderer);
highscoresFrame.add(sp);
highscoresFrame.setSize(new Dimension(600, 400));
highscoresFrame.setLocationRelativeTo(null);
} catch (SQLException ex) {
Logger.getLogger(LabyrinthGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}