我可以进行所有工作,但是每当我编译代码并选择留下痕迹时,球都会打印每个像素,从而留下一行。我想做的是每经过球的半径就画一个球。 这是我的代码:
public static void showBall(DrawingPanel panel, int[] ballPos, int ballSize, Color color, int trace){
Graphics g = panel.getGraphics();
if (trace == 0) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, panel.getWidth(), panel.getHeight());
}
g.setColor(color);
g.fillOval(ballPos[0], ballPos[1], ballSize, ballSize);
panel.sleep(1);
}
}
You have your "draw background" code inside an
if
block, but I think you have to redraw the background every time to avoid getting the effect you are seeing (to draw over the last frame). IE, remove theif(trace == 0) {
condition.也许这种影响是通过跟踪参数的设计来实现的。换句话说,也许当trace设置为0时,您所看到的行为是预期的。也许您想要设置trace == 1来获得该行为,在这种情况下,您可以将现有代码更改为:
If it was me, I'd probably make
trace
a boolean, and define a "default value" for trace by making an extra method like this:但这对于硬件分配来说可能是过高的,并且它们并不总是让您选择方法签名...