這是一個簡單的反彈球 ,我會以能夠顯示的FPS程序運行如何在Eclipse中的Java Applet中每秒顯示幀數?
import java.awt.*;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class BallApplet extends Applet implements Runnable {
private int ballX, ballY;
private final int radius = 50;
public void start1() {
Thread th = new Thread(this);
th.start();
}
@Override
public void run() {
int dx = 2;
int dy = 2;
int speed = 2;
// This will reduce the load the applet has on the runtime
// system..
Thread.currentThread().setPriority(…
while (true) {
ballX += dx;
ballY += dy;
repaint();
if (ballX + radius > getWidth())
dx = -speed;
else if (ballX < 0)
dx = speed;
if (ballY + radius > getHeight())
dy = -speed;
else if (ballY < 0)
dy = speed;
try {
Thread.sleep(20);
} catch (InterruptedException ie) {
}
}
}
// set up BallApplet object
public void init() {
ballX = 0;
ballY = getHeight()/2;
}
// Drawing instructions…
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.fillOval(ballX - radius, ballY - radius, 2 * radius, 2 * radius);
}
// The standard Applet 「GO」 function…
public void start() {
Thread th = new Thread(this);
th.start();
}
}
由於洛希
請格式化您的代碼,謝謝! – home