-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameFrame2.java
More file actions
151 lines (122 loc) · 5.71 KB
/
Copy pathGameFrame2.java
File metadata and controls
151 lines (122 loc) · 5.71 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
/**
* This template can be used as reference or a starting point
* for your final summative project
* @author Mangat
**/
//Graphics &GUI imports
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
//Keyboard imports
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
//Mouse imports
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class GameFrame2 extends JFrame {
/****************** CLASS VARIABLES********************/
/** The variables can be accessed across all methods ***/
/** You will need to add some variables here ***/
/********************************************************/
static double x, y;
static GameAreaPanel gamePanel;
/*************************************************************************
******* GameFrame - Setups up the Window and Starts displaying it *****
************************* DO NOT MODIFY *******************************/
GameFrame2() {
super("My Game");
// Set the frame to full screen
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1024,768);
gamePanel = new GameAreaPanel();
this.add(new GameAreaPanel());
MyKeyListener keyListener = new MyKeyListener();
this.addKeyListener(keyListener);
MyMouseListener mouseListener = new MyMouseListener();
this.addMouseListener(mouseListener);
this.requestFocusInWindow(); //make sure the frame has focus
//this.setVisible(true);
Thread t = new Thread(new Runnable() {
public void run() { animate(); }}); //start the gameLoop
t.start();
} //End of Constructor
/****** End of GameFrame *********************************/
/**************************** Main Method ************************/
/** ******************* DO NOT MODIFY *********/
public static void main(String[] args) {
System.out.println("?>?");
EventQueue.invokeLater(() -> {
GameFrame2 x = new GameFrame2();
x.setVisible(true);
});
}
/****** end of Main *********************************/
/************************** Animate - Gameloop************************/
/** This section is where the games state is updated. *****/
/** You will need to have most of your game logic in here **/
/*********************************************************************/
public void animate() {
while(true){
this.x = (Math.random()*1024); //update coords
this.y = (Math.random()*768);
try{ Thread.sleep(500);} catch (Exception exc){} //delay
this.repaint(); //update the screen
}
}
/****** End of Animate *********************************/
//Inner class - JPanel
private class GameAreaPanel extends JPanel {
/************************** PaintComponenet ************************/
/** This section is where the screen is drawn **/
/** You will need to draw all your items in this method **/
/*******************************************************************/
public void paintComponent(Graphics g) {
super.paintComponent(g); //required
setDoubleBuffered(true);
g.setColor(Color.BLUE); //There are many graphics commands that Java can use
g.fillRect((int)x, (int)y, 50, 50); //notice the x,y variables that we control from our animate method
}
}
/****** End of paintComponent *********************************/
/***************************** Key Listener ************************/
/** This section is where keyboard input is handled **/
/** You will add code to respond to key presses **/
/*******************************************************************/
private class MyKeyListener implements KeyListener {
public void keyPressed(KeyEvent e) {
//System.out.println("keyPressed="+KeyEvent.getKeyText(e.getKeyCode()));
if (KeyEvent.getKeyText(e.getKeyCode()).equals("D")) { //If 'D' is pressed
System.out.println("YIKES D KEY!");
} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { //If ESC is pressed
System.out.println("YIKES ESCAPE KEY!"); //close frame & quit
System.exit(0);
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
/****** Key Listener *********************************/
/**************************** Mouse Listener ************************/
/** This section is where mouse input is handled **/
/** You may have to add code to respond to mouse clicks **/
/********************************************************************/
private class MyMouseListener implements MouseListener {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked");
System.out.println("X:"+e.getX() + " y:"+e.getY());
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
/****** Mouse Listener *********************************/
}