/
Run.java
82 lines (71 loc) · 2.17 KB
/
Run.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
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.Timer;
public class Run implements ActionListener {
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem newGame, score, exit;
private HeartsGUI game;
private JFrame window;
static final Dimension windowSize = new Dimension(800,700);
private Timer t;
public Run()
{}
public void start(){
game = new HeartsGUI();
window = new JFrame(); //create new JFrame for game
window.setContentPane(game);
window.setVisible(true);
window.setTitle ("Hearts");
window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setPreferredSize(windowSize);
//initialize JMenu items
menuBar = new JMenuBar();
menu = new JMenu("Game");
menu.setMnemonic(KeyEvent.VK_G);
newGame = new JMenuItem("New Game", KeyEvent.VK_N);
newGame.addActionListener(this);
score = new JMenuItem("Score", KeyEvent.VK_S); //shows score
score.addActionListener(this);
exit = new JMenuItem("Quit", KeyEvent.VK_Q);
exit.addActionListener(this);
window.setJMenuBar(menuBar);
menuBar.add(menu);
menu.add(newGame);
menu.add(score);
menu.add(exit);
window.pack();
window.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) { //action performed changes depending on choice
JMenuItem source = (JMenuItem) e.getSource();
String text = source.getText(); //gets the choice
if (text.equals("New Game"))
newGame();
else if (text.equals("Score"))
game.showScore();
else if (text.equals("Quit")){
window.dispose();
}
}
private void newGame(){ //called when new game
OpenWindow playTimer = new OpenWindow();
t = new Timer(10, playTimer); //call newGame after createBox
game.createBox(); //creates loading frame
t.start();
}
class OpenWindow implements ActionListener{
public void actionPerformed(ActionEvent e) {
game.newGame(true);
t.stop();
game.closeBox(); //closes loading frame
}
}
}