-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetrisWindow.java
113 lines (93 loc) · 3.2 KB
/
TetrisWindow.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
/*
* Name: Gabriella Toby and Adetola Aladekoba
* Purpose: The executable class that creates the window for the tetris game
* Date: 26/04/2024
*/
import javax.swing.*;
import java.awt.event.*;
public class TetrisWindow extends JFrame
{
private TetrisGame game;
private TetrisDisplay display;
private int win_width = 500;
private int win_height = 500;
private int game_rows= 20;
private int game_cols= 12;
private TetrisLeaderBoard leaderBoard;
public TetrisWindow()
{
game = new TetrisGame(game_rows,game_cols);
leaderBoard= new TetrisLeaderBoard(game);
display = new TetrisDisplay(game);
this.setTitle("Tetris Game Gabriella Toby and "
+ "Adetola Aladekoba");
this.setSize(win_width,win_height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(display);
initMenus();
this.setVisible(true);
}
public void initMenus()
{
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu gameMenu = new JMenu("Game");
menuBar.add(gameMenu);
JMenuItem saveGame = new JMenuItem("Save");
gameMenu.add(saveGame);
JMenuItem retrieveGame = new JMenuItem("Retrieve");
gameMenu.add(retrieveGame);
JMenuItem newGame = new JMenuItem("New");
gameMenu.add(newGame);
JMenu score = new JMenu("Score");
menuBar.add(score);
JMenuItem highScore = new JMenuItem("High Score");
score.add(highScore);
JMenuItem clearScore = new JMenuItem("Clear Score");
score.add(clearScore);
saveGame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
game.saveToFile();
return;
}
});
retrieveGame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
game.retrieveFromFile();
return;
}
});
highScore.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(null,
leaderBoard.scoreBoard(),
"High Scores", JOptionPane.INFORMATION_MESSAGE);
}
});
newGame.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
game.newGame();
}
});
clearScore.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
leaderBoard.checkAndUpdateScores();
}
});
this.setJMenuBar(menuBar);
}
public static void main(String[] args)
{
new TetrisWindow();
}
}