1

ランダムな場所に2つの画像を表示し、毎秒変更するプログラムを作成しようとしています。ただし、私の問題は、画像を再描画しても、前の位置の画像が表示されることです。

必要に応じて、WTK2.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 に答える