2013-03-25 138 views
1

我試圖創建一個程序,在隨機位置顯示兩個圖像並每秒更改一次。然而,我的問題是當圖像重新繪製時,我仍然可以在前一個位置看到圖像。使用J2ME清除畫布

我正在使用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

回答

5

您清除畫布是這樣的:

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

所以剛纔繪製的圖像前插入這些行。