-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationFrame.java
More file actions
148 lines (122 loc) · 4.29 KB
/
AnimationFrame.java
File metadata and controls
148 lines (122 loc) · 4.29 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
//AnimationFrame.java
//by Shanti Pothapragada, poth0018@umn.edu, Chad Myers, chadm@umn.edu
import java.awt.*;
import java.awt.event.*;
public abstract class AnimationFrame extends Frame implements Runnable {
private int fps=50;
public AnimationFrame(){
super();
setSize(800,800);
}
public AnimationFrame(int w, int h, String name){
super(name);
setSize(w,h);
}
/**
* Call this method when you're ready for the animation to start.
*/
public void start(){
super.setVisible(true);//shows the Frame.
//these lines are to enable listening for keyboard and window closing events:
//see the methods processKeyEvent and processWindowEvent below which are invoked
//upon receiving one of these events
enableEvents(AWTEvent.KEY_EVENT_MASK);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
thread=new Thread(this);
thread.start();//thread will call run() when Thread says it's our turn.
//run() is responsible for maintaining the framerate and calling
//action() and draw() each frame.
}
/**
* This method is called every frame to update the state of the animation frame.
*/
public abstract void action();
/**
* This method is called every frame to draw the animation frame.
*
* @param g a reference to a graphics object, for drawing
*/
public abstract void draw(Graphics g);
/**
* Returns the current frame rate.
*/
public int getFPS(){ return fps;}
/**
* This mutator method allows you to control the framerate.
* Setting FPS=1 means that the image will be updated 1 time per second.
* Recomended values are 12-60. Below 12 will look unnaturally choppy,
* most moniters cannot draw more than 50 times/second anyways.
* Actual rate may be lower if computer can't keep up.
* A lower rate may improve performance.
*/
public void setFPS(int newFPS){
if (newFPS!=0)//to prevent div by 0 errors.
fps=newFPS;
}
/**
* This method is invoked whenever the animation frame receives a keyboard event.
*/
protected void processKeyEvent(KeyEvent e)
{
int keyCode = e.getKeyCode();
//move if left key is pressed
if (keyCode == KeyEvent.VK_LEFT)
{
System.out.println("Left key pressed...");
}
else if (keyCode == KeyEvent.VK_RIGHT)
{
System.out.println("Right key pressed...");
}
}
//-----------------------------------------------------------------------------
//everything on your side of the abstraction barrier is above.
//in theory, you don't need to know about anything below here.
//some of it is obscure and/or poorly commented.
//It is based on code from "Animation in Java applets" in JavaWorld
//at http://www.javaworld.com/javaworld/jw-03-1996/jw-03-animation.html
//-----------------------------------------------------------------------------
private Image buffer;
private Thread thread;
public void run(){
//Run is in charge of maintaining the framerate.
//run implements runnable, and needs to be public.
//( Thread(this) needs this to be a runnable.)
//Remember the starting time
long tm = System.currentTimeMillis();
while(Thread.currentThread()==thread){//Main loop
action();
update(getGraphics());
// Delay depending on how far we are behind:
tm += 1000/fps;
try {
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
} catch (InterruptedException e) {
break;
}
}// end main loop.
}
public void paint(Graphics g){
//paint is called if part of the window is covered/uncovered/etc.
//need to overwrite the Frame method to redirect it to update.
update(g);
}
public void update(Graphics g){
//( overwriting Frame.update() )
//update hands a buffer image to draw(), then draws the buffer to screen.
//ensure the buffer is ready and draw to it:
if (buffer==null)
buffer=createImage(getWidth(), getHeight());//createImage is a method of the Frame class.
//call draw:
draw(buffer.getGraphics());
//draw buffer to screen:
g.drawImage(buffer,0,0,null);
}
public void processWindowEvent(WindowEvent e) {
if(e.getID() == WindowEvent.WINDOW_CLOSED) {
System.exit(0);
} else if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}