forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.java
More file actions
53 lines (41 loc) · 1.89 KB
/
GameWindow.java
File metadata and controls
53 lines (41 loc) · 1.89 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
package Dino_Game_java;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame; // More common to use JFrame for Swing apps
// Sets up the main game window and wires components together
public class GameWindow extends JFrame {
public static final int D_W = 1200; // Window width
public static final int D_H = 550; // Window height
private GameManager gameManager;
private GamePanel gamePanel;
private InputHandler inputHandler;
public GameWindow() {
super("Run Dino Run Refactored");
gamePanel = new GamePanel(null); // Panel created, GameManager will be set later
gameManager = new GameManager(gamePanel); // Pass panel for repaint calls
gamePanel.setGameManager(gameManager); // Now set manager for panel to use
inputHandler = new InputHandler(gameManager);
this.add(gamePanel);
this.addKeyListener(inputHandler); // Add input handler to the frame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(D_W, D_H);
this.pack(); // Adjusts window size to preferred size of components
this.setLocationRelativeTo(null); // Center on screen
this.setResizable(false);
this.setVisible(true);
gameManager.startGame(); // Start the game logic
}
// Setter in GamePanel to avoid chicken-and-egg problem with GameManager needing GamePanel for repaint
// and GamePanel needing GameManager for data.
// GamePanel.java needs:
// public void setGameManager(GameManager manager) { this.gameManager = manager; }
public static void main(String[] args) {
// Ensures Swing components are created on the Event Dispatch Thread
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GameWindow();
}
});
}
}