-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.java
More file actions
157 lines (143 loc) · 5.81 KB
/
Menu.java
File metadata and controls
157 lines (143 loc) · 5.81 KB
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
/**
* Menu.java
* Justin Granofsky & Jerry Yu
* 2018-05-24
* Version 1.0
* Handles creating the menu panel.
*/
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Menu extends JPanel implements ActionListener, MouseListener {
/**
* Main menu constructor to make the panel and add components to it.
*/
public Menu(){
// Set the layout of the main menu
setLayout(new GridBagLayout());
// Create constraints to handle position of the components.
GridBagConstraints gc = new GridBagConstraints();
// Set the starting constraints.
gc.gridx = 0;
gc.gridy = 0;
// Create insets to separate components.
gc.insets = new Insets(10, 0, 10, 0);
// Create a welcome message for the users.
JLabel msg = new JLabel("<html><center><div style=\"padding: 10px; border-bottom: 3px white solid; color: white;\"><h1 style=\"font-variant: small-caps;\">CANADIAN<br>CHECKERS</h1></div></center></html>");
// Add the message to the panel.
add(msg, gc);
// Create a button for playing the game.
JButton play = new JButton("<html><div style=\"border-bottom: 3px white solid; color: white;\">Play</div></html>");
// Set the font of the text on the button and size of text.
play.setFont(new Font("NewTimesRoman", Font.HANGING_BASELINE, 16));
/* Remove button formatting */
play.setBorderPainted(false);
play.setContentAreaFilled(false);
play.setFocusPainted(false);
/* ------------------------ */
// Add a mouse listener to listen for mouse hovering events.
play.addMouseListener(this);
// Add an action listener to listen for clicking.
play.addActionListener(this);
// Set the constraints for this component.
gc.gridy = 1;
// Add the button to the main menu panel.
add(play, gc);
// Create a button for users to close the game.
JButton exit = new JButton("<html><div style=\"border-bottom: 3px white solid; color: white;\">Exit</div></html>");
// Set the font of the text on the button
exit.setFont(new Font("NewTimesRoman", Font.HANGING_BASELINE, 16));
/* Remove button formatting */
exit.setBorderPainted(false);
exit.setContentAreaFilled(false);
exit.setFocusPainted(false);
/* ------------------------ */
// Add a mouse listen to check for hover events.
exit.addMouseListener(this);
// Add an action listener to check for click events.
exit.addActionListener(this);
// Set the constraints to position the button lower on the screen.
gc.gridy = 2;
// Add the button the main menu panel.
add(exit, gc);
// Add the credits to the main menu.
JLabel credits = new JLabel("<html><center><div style=\"padding: 10px; color: white;\"><h2 style=\"font-variant: small-caps;\">Created By<br>Justin Granofsky<br>Bill Wu</h2></div></center></html>");
gc.gridy = 3;
add(credits, gc);
}
/**
* Method to paint the background to the screen.
*/
@Override
protected void paintComponent(Graphics g){
try {
// Try to get the image from the file.
BufferedImage img = ImageIO.read(Menu.class.getResource("back.jpg"));
// Draw the image to the screen.
g.drawImage(img, 0, 0, 600, 600, Color.white, null);
}catch(IOException e){}
}
/**
* Method to get the clicked buttons and act on the clicks.
*/
@Override
public void actionPerformed(ActionEvent e) {
// Make sure that the source/component being clicked is a JButton.
if(e.getSource() instanceof JButton){
// Cast the source to a JButton.
JButton button = (JButton) e.getSource();
// Check if the button is the play button.
if(button.getText().contains("Play")){
// Create a variable to store the new game board.
Checkers.board = new Board();
// Set the users to see the checker board.
Checkers.setContent(Checkers.board);
}else{
// If the button is the exit, close the game.
System.exit(0);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
/**
* Method to change the colour of the button when hovered over.
*/
@Override
public void mouseEntered(MouseEvent e) {
// Make sure the source is a JButton
if(e.getSource() instanceof JButton){
// Cast the source to be a JButton
JButton button = (JButton) e.getSource();
// Change the colour of the button
button.setText(button.getText().replaceAll("white", "green"));
}
}
/**
* Method to change the colour of the button when it is no longer hovered over.
*/
@Override
public void mouseExited(MouseEvent e) {
// Check if the source is a JButton
if(e.getSource() instanceof JButton){
// Cast the source to a JButton
JButton button = (JButton) e.getSource();
// Change the colour of the JButton back to normal.
button.setText(button.getText().replaceAll("green", "white"));
}
}
}