我正在练习渲染和绘制图形,我似乎无法找出为什么 eclipse 在大约 1/5 的时间里给我一个错误。
Exception in thread "Thread-3" java.lang.NullPointerException
at game.StartingPoint.run(StartingPoint.java:74)
at java.lang.Thread.run(Unknown Source)
是我的线程有问题吗?我怎样才能解决这个问题?
这是我的源代码。
起点.java:
package game;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
public class StartingPoint extends Applet implements Runnable {
Image i;
Graphics doubleG;
Ball b1;
Ball b2;
public StartingPoint() {
}
@Override
public void init() {
}
@Override
public void start() {
setSize(480, 360);
Thread thread = new Thread(this);
thread.start();
b1 = new Ball(40, 40);
b2 = new Ball(70, 200);
}
@Override
public void stop() {
}
@Override
public void destroy() {
}
@Override
public void update(Graphics g) {
if (i == null) {
i = createImage(this.getWidth(), this.getHeight());
doubleG = i.getGraphics();
}
doubleG.setColor(getBackground());
doubleG.fillRect(0, 0, this.getWidth(), this.getHeight());
doubleG.setColor(getForeground());
paint(doubleG);
g.drawImage(i, 0, 0, this);
}
@Override
public void paint(Graphics g) {
b1.paint(g);
b2.paint(g);
}
@Override
public void run() {
while (true) {
b1.update(this);
b2.update(this);
repaint();
try {
Thread.sleep(30);
} catch (Exception e) {
System.out.print("Error");
}
}
}
}
球.java:
package game;
import java.awt.Color;
import java.awt.Graphics;
public class Ball {
double gravity = 15.0;
double energyLoss = .65;
double outsideEnergy = .95;
double dt = .25;
double xFriction = .9;
int x = 40;
int y = 40;
double dx = 7.0;
double dy = 0.0;
int radius = 20;
public Ball() {
}
public Ball(int x, int y) {
this.x = x;
this.y = y;
}
public void update(StartingPoint sp) {
if (x <= 0 | x >= sp.getWidth()) {
dx = -dx;
}
if (y > sp.getHeight() - radius - 1) {
y = sp.getHeight() - radius - 1;
dy *= energyLoss;
dy = -dy;
dx *= outsideEnergy;
} else {
// velocity
dy = dy + gravity * dt;
// d=viT + 1/2(a)t^2
y += dy * dt + .5 * gravity * dt * dt;
}
x += dx;
}
public void paint(Graphics g) {
g.setColor(Color.green);
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
}
}