-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.java
More file actions
36 lines (26 loc) · 1.11 KB
/
Copy pathSnakeGame.java
File metadata and controls
36 lines (26 loc) · 1.11 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
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
public class SnakeGame {
public static void main(String[] argss) {
// Create the main window frame
JFrame frame = new JFrame("Classic Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the game panel, where all the action happens
GamePanel gamePanel = new GamePanel();
frame.add(gamePanel);
// Automatically adjust the window size to fit the panel's preferred size
frame.pack();
// Prevent resizing, as it could mess up the grid
frame.setResizable(false);
// Center the window on the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int x = (dim.width - frame.getWidth()) / 2;
int y = (dim.height - frame.getHeight()) / 2;
frame.setLocation(x, y);
// Make the window visible
frame.setVisible(true);
// Ensure the game panel has focus to receive key events
gamePanel.requestFocusInWindow();
}
}