我正在创建一个包含两个移动对象的菜单。代码如下:
public void drawPageZero()
{
repaint.setData(repaint0.getData());
Graphics g = repaint.createGraphics();
if (xN < - napisBar.getWidth())
{
xN = 2 * napisBar.getWidth();
}
if (xN < 0 && xN >= -napisBar.getWidth())
{
xN2 = xN + napisBar.getWidth();
xN3 = xN + 2 *napisBar.getWidth();
}
else if (xN <= 2*napisBar.getWidth() && xN >= napisBar.getWidth())
{
xN2 = xN - 2*napisBar.getWidth();
xN3 = xN - napisBar.getWidth();
}
else if (xN >= 0 && xN < napisBar.getWidth())
{
xN2 = xN + napisBar.getWidth();
xN3 = xN - napisBar.getWidth();
}
g.drawImage(napisBar,xN,0,this);
g.drawImage(napisBar,xN2,0,this);
g.drawImage(napisBar,xN3,0,this);
if (xW < - walutyBar.getWidth())
{
xW = 2 * walutyBar.getWidth();
}
if (xW < 0 && xW >= -walutyBar.getWidth())
{
xW2 = xW + walutyBar.getWidth();
xW3 = xW + 2 *walutyBar.getWidth();
}
else if (xW <= 2*walutyBar.getWidth() && xW >= walutyBar.getWidth())
{
xW2 = xW - 2*walutyBar.getWidth();
xW3 = xW - walutyBar.getWidth();
}
else if (xW >= 0 && xW < walutyBar.getWidth())
{
xW2 = xW + walutyBar.getWidth();
xW3 = xW - walutyBar.getWidth();
}
g.drawImage(walutyBar,xW,400,this);
g.drawImage(walutyBar,xW2,400,this);
g.drawImage(walutyBar,xW3,400,this);
image = new ImageIcon(repaint.getScaledInstance(sizeX,sizeY, Image.SCALE_SMOOTH));
imageLabel.setIcon(image);
revalidate();
repaint();
timer = new Timer();
timer.schedule(new infoBar(), 1);
}
计时器:
class infoBar extends TimerTask
{
@Override
public void run() {
if (page ==0) {
xN -= 1;
xW -= 1;
timer.cancel();
timer.purge();
drawPageZero();
}
else
{
timer.cancel();
timer.purge();
}
}
}
两者&#34; walutyBar&#34;和&#34; infoBar&#34;是在程序启动时创建的。我需要每次重新加载背景,因为用图片覆盖它(这就是使用setData的原因)。我已经为定时器设置了1 ms,但对象的移动速度却慢得多。哪个进程占用了大部分CPU时间,有没有简单的方法来缩短它? (又称。使drawPageZero功能更快完成)。在此先感谢您的帮助 ! :)