1

我正在尝试创建一个程序,该程序在随机位置显示两个图像并每秒更改它们。然而,我的问题是当图像被重绘时,我仍然可以在以前的位置看到图像。

如果相关的话,我正在使用 WTK 2.52。

public void run()
{

    while(true)
    {
        dVert = rand.nextInt(136);
        dHorz = rand.nextInt(120);
        rVert = 136 + rand.nextInt(136);
        rHorz = 120 + rand.nextInt(120);

        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            System.out.println("Error");
        }
        repaint();
    }
}

public void paint(Graphics g)
{
    int width = this.getWidth() / 2;
    int height = this.getHeight() / 2;
    g.drawImage(imgR, rVert, rHorz, g.TOP | g.LEFT);
    g.drawImage(imgD,dVert,dHorz,g.TOP | g.LEFT);

    //g.drawString(disp, width, 50, Graphics.TOP | Graphics.HCENTER);
    //g.drawString(centerMsg,width, height, Graphics.TOP | Graphics.HCENTER);
    System.out.println(width);
    System.out.println(height);
}

完整代码

4

1 回答 1

5

你像这样清除画布:

g.setColor(0x000000); // Whatever color you wish to clear with
g.setClip(0,0,getWidth(),getHeight());
g.fillRect(0,0,getWidth(),getHeight());

因此,只需在绘制图像之前插入这些线条。

于 2013-03-26T05:23:29.797 回答