为什么 clearRect() 方法不清除 canvas 上的区域?
Why does clearRect() method not clear an area on the canvas?
我写了一个 Java 小程序,它在循环的每次迭代中更新后在同一位置打印 canvas 上的文本。文本值发生变化并且输出是正确的但是我在循环中调用的 clearRect() 方法在每次打印之前清除文本区域不起作用并且文本得到覆盖。我最多可以看到 2-3 次迭代的输出,但之后就不可读了。该小程序实际上执行倒计时并以 HH : MM : SS 格式显示时间。下面是我的代码。请预测此问题的原因 and/or 更正它:
/* Only the section of the code I'm having problems in is included */
while (t>=0) {
//int t stores total time in seconds
int h=(t/3600);
int m=((t%3600)/60);
int s=((t%3600)%60);
str=h+" : "+m+" : "+s;
/* String str holds data to be displayed, i.e., time in HH : MM : SS format */
g.clearRect(20,200,150,30);
g.drawString(str,20,200);
try{
Thread.sleep(1000); //elapses 1 second
}
catch(Exception e) {}
t=t-1;
}
使用带有颜色的 drawRect 而不是 clearRect。就像@ControlAltDel 解释的那样。我不确定上面的代码是否位于您的绘图或 运行 中。我仍然建议使用 运行nable 方法而不是循环绘制它。类似于 this link。祝你好运
我写了一个 Java 小程序,它在循环的每次迭代中更新后在同一位置打印 canvas 上的文本。文本值发生变化并且输出是正确的但是我在循环中调用的 clearRect() 方法在每次打印之前清除文本区域不起作用并且文本得到覆盖。我最多可以看到 2-3 次迭代的输出,但之后就不可读了。该小程序实际上执行倒计时并以 HH : MM : SS 格式显示时间。下面是我的代码。请预测此问题的原因 and/or 更正它:
/* Only the section of the code I'm having problems in is included */
while (t>=0) {
//int t stores total time in seconds
int h=(t/3600);
int m=((t%3600)/60);
int s=((t%3600)%60);
str=h+" : "+m+" : "+s;
/* String str holds data to be displayed, i.e., time in HH : MM : SS format */
g.clearRect(20,200,150,30);
g.drawString(str,20,200);
try{
Thread.sleep(1000); //elapses 1 second
}
catch(Exception e) {}
t=t-1;
}
使用带有颜色的 drawRect 而不是 clearRect。就像@ControlAltDel 解释的那样。我不确定上面的代码是否位于您的绘图或 运行 中。我仍然建议使用 运行nable 方法而不是循环绘制它。类似于 this link。祝你好运