-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePanel.java
163 lines (135 loc) · 3.83 KB
/
GamePanel.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Aaryan Prakash
// 4/13/18
// GamePanel.java
// This contains the user interface to play the game.
// This holds the left and right sides of the game interface.
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class GamePanel extends JPanel
{
private GameHolder holder; // variable for panel that holds cards
private CardLayout cards; // variable for CardLayout
private Game currentGame; // represents current game
private ImageViewer imageOptions; // for viewing images to help user
private Dice diceViewer; // variable for dice
private SettingsPanel settings; // variable for settings panel
// name for the back button
private final String BACK_NAME = new String("Back");
// adds back button, left, and right sides to the panel
public GamePanel(GameHolder holderIn, CardLayout cardsIn)
{
holder = holderIn;
cards = cardsIn;
setLayout(new BorderLayout(0, 0));
JButton back = new JButton(BACK_NAME);
back.addActionListener(new BackHandler());
add(back, BorderLayout.NORTH);
currentGame = new Game(this);
diceViewer = new Dice(currentGame);
imageOptions = new ImageViewer();
settings = new SettingsPanel(currentGame);
RightSide right = new RightSide();
add(right, BorderLayout.EAST);
LeftSide left = new LeftSide();
add(left, BorderLayout.CENTER);
addMouseListener(new RollDiceListener());
addKeyListener(new RollDiceListener());
}
// creates a new game to play
public void createNewGame()
{
currentGame.reset();
}
// returns instance of Game
public Game getGame()
{
return currentGame;
}
// goes back to start page
public void goToStart()
{
cards.show(holder, holder.getStartName());
}
// returns dice panel instance
public Dice getDice()
{
return diceViewer;
}
// ActionListener for back button, goes to start page if clicked
class BackHandler implements ActionListener
{
// goes to start page if back button is clicked
public void actionPerformed(ActionEvent evt)
{
WarnBackFrame wbf = new WarnBackFrame(holder, cards);
wbf.createFrame();
}
}
// class for the right part of the JPanel
class RightSide extends JPanel
{
// adds image viewer panel and dice roller to panel
public RightSide()
{
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
setPreferredSize(new Dimension(200, 1000));
setBackground(Color.WHITE);
add(imageOptions);
add(diceViewer);
add(settings);
}
}
// class for the left part of the JPanel
class LeftSide extends JPanel
{
// adds game board and alert box to the panel
public LeftSide()
{
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
add(currentGame.getBoard());
add(currentGame.getAlertBox());
}
}
// MouseListener and KeyListener for this class
class RollDiceListener implements MouseListener, KeyListener
{
// gets focus into current panel
public void mousePressed(MouseEvent evt)
{
requestFocusInWindow();
}
// unused MouseListener methods
public void mouseClicked(MouseEvent evt) {}
public void mouseReleased(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}
// rolls the dice if user presses up
public void keyPressed(KeyEvent evt)
{
int keyCode = evt.getKeyCode();
if (keyCode == KeyEvent.VK_UP)
{
diceViewer.roll();
}
else if (keyCode == KeyEvent.VK_SPACE)
{
currentGame.reset();
}
}
// unused KeyListener methods
public void keyTyped(KeyEvent evt) {}
public void keyReleased(KeyEvent evt) {}
}
}