CQD (owner)

Revisions

gist: 143374 Download_button fork
public
Public Clone URL: git://gist.github.com/143374.git
Java2D Performance Test.java
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package idv.cqd.test;
 
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
 
@SuppressWarnings("serial")
public class GeneralTest extends Canvas {
Frame mf = new Frame();
protected static final int WIDTH = 200;
protected static final int HEIGHT = 200;
protected static final int PADDING = 10;
protected static final int LINES = 1000;
 
protected static final int BOXSIZE = 10;
protected static final float BOXSPEED = 132.2f; //pixel per sec
protected static final int WORLDUPDATETIME = 17; //millisec
 
protected float boxX = 0;
protected float boxY = 0;
 
protected boolean goingUp = false;
protected boolean goingDown = false;
protected boolean goingLeft = false;
protected boolean goingRight = false;
 
protected boolean gameRunning = true;
 
public void start() {
setSize(WIDTH, HEIGHT);
mf.setSize(WIDTH, WIDTH);
mf.setResizable(false);
 
mf.add(this);
mf.pack();
mf.setVisible(true);
 
mf.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
gameRunning = false;
System.exit(0);
}
});
 
KeyListener kl = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
goingUp = true;
break;
case KeyEvent.VK_DOWN:
goingDown = true;
break;
case KeyEvent.VK_LEFT:
goingLeft = true;
break;
case KeyEvent.VK_RIGHT:
goingRight = true;
break;
}
}
 
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
goingUp = false;
break;
case KeyEvent.VK_DOWN:
goingDown = false;
break;
case KeyEvent.VK_LEFT:
goingLeft = false;
break;
case KeyEvent.VK_RIGHT:
goingRight = false;
break;
}
}
 
};
 
mf.addKeyListener(kl);
this.addKeyListener(kl);
 
createBufferStrategy(3);
BufferStrategy startegy = getBufferStrategy();
Graphics g = startegy.getDrawGraphics();
 
WorldUpdater wu = new WorldUpdater();
wu.init(this);
wu.start();
 
while (gameRunning) {
long startTime = System.nanoTime();
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < LINES; i++) {
g.setColor(Color.WHITE);
g.drawLine(randX(), randY(), randX(), randY());
}
g.setColor(Color.RED);
g.fillRect((int)boxX-BOXSIZE/2, (int)boxY-BOXSIZE/2, BOXSIZE, BOXSIZE);
g.drawString("X:"+boxX+" Y:"+boxY, 10, 10);
long endTime = System.nanoTime();
g.setColor(Color.RED);
g.drawString("fps:"+(1000000000 / (endTime - startTime)), 10, HEIGHT-10);
 
startegy.show();
}
}
 
int randX() {
return (int) (Math.random() * (WIDTH - 2 * PADDING)) + PADDING;
}
 
int randY() {
return (int) (Math.random() * (HEIGHT - 2 * PADDING)) + PADDING;
}
 
public static void main(String[] args) throws Exception {
GeneralTest gt = new GeneralTest();
gt.start();
}
 
}
 
class WorldUpdater extends Thread {
GeneralTest world;
 
public void init(GeneralTest obj) {
world = obj;
}
 
@Override
public void run() {
while(true){
if (world.goingUp) {
world.boxY -= GeneralTest.BOXSPEED / 1000 * GeneralTest.WORLDUPDATETIME;
}
if (world.goingDown) {
world.boxY += GeneralTest.BOXSPEED / 1000 * GeneralTest.WORLDUPDATETIME;
}
if (world.goingLeft) {
world.boxX -= GeneralTest.BOXSPEED / 1000 * GeneralTest.WORLDUPDATETIME;
}
if (world.goingRight) {
world.boxX += GeneralTest.BOXSPEED / 1000 * GeneralTest.WORLDUPDATETIME;
}
if(world.boxX>GeneralTest.WIDTH)
world.boxX=GeneralTest.WIDTH;
if(world.boxX<0)
world.boxX=0;
if(world.boxY>GeneralTest.HEIGHT)
world.boxY=GeneralTest.HEIGHT;
if(world.boxY<0)
world.boxY=0;
 
try {
Thread.sleep(GeneralTest.WORLDUPDATETIME);
}
catch (InterruptedException e) {
}
}
}
}