使用J2ME清除画布

时间:2013-03-25 22:50:46

标签: java-me midp lcdui

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

如果相关,我正在使用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);
}

Complete code

1 个答案:

答案 0 :(得分:5)

您可以像这样清除画布:

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

所以只需在绘制图像之前插入这些行。