2012-11-25 74 views
4

我試圖在j2me圖像幻燈片。以下是我的圖片幻燈片代碼。但它會執行圖像不幻燈片並不顯示。J2ME圖片項目幻燈片

如果我點擊播放按鈕,圖像將從圖像0.jpg自動幻燈片顯示,直到圖像7.jpg。

此變量爲滑動

String[] Foto={"/0.jpg", "/1.jpg","/2.jpg", "/3.jpg","/4.jpg", "/5.jpg","/6.jpg", "/7.jpg"}; 
Image[] img = new Image[8]; 

和該循環用於顯示圖像..

protected void paint(Graphics g) { 
    try { 

     g.setColor(255, 0, 0); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     g.setColor(255,0, 0); 
     g.drawString(text,getWidth()/2, getHeight()/2, 
     Graphics.TOP | Graphics.HCENTER); 
     g.setColor(0, 255, 0); 
     g.setColor(123,0,255); 
     g.drawRect(74,74, 100,100); 
     g.drawRect(72,72, 104,104); 

        for (int i=0;i<8;i++) 
       if (i==1) 
       if (i==2) 
      img[i]=Image.createImage(Foto[i]); 
      g.drawImage(img [1],124,124, Graphics.VCENTER | Graphics.HCENTER); 
      g.drawImage(img [2],124,124, Graphics.VCENTER | Graphics.HCENTER); 
      g.drawImage(img [1],124,124, Graphics.VCENTER | Graphics.HCENTER); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    }} 
+0

嘗試把所有這些之外在繪畫功能之外。 \t for(int i = 0; i <8; i ++){ \t \t img [i] = Image.createImage(Foto [i]); \t} \t 並刪除相同的油漆行內。 –

回答

0

你在塗料()的循環,這將在畫布上作畫逐個圖像。嘗試使用全局變量currentIndex = 0

protected void paint(Graphics g) { 
try { 

    g.setColor(255, 0, 0); 
    g.fillRect(0, 0, getWidth(), getHeight()); 
    g.setColor(255,0, 0); 
    g.drawString(text,getWidth()/2, getHeight()/2, 
    Graphics.TOP | Graphics.HCENTER); 
    g.setColor(0, 255, 0); 
    g.setColor(123,0,255); 
    g.drawRect(74,74, 100,100); 
    g.drawRect(72,72, 104,104); 


    img = Image.createImage(Foto[currentIndex]); 
    g.drawImage(img,124,124, Graphics.VCENTER | Graphics.HCENTER); 

} catch (IOException ex) { 
    ex.printStackTrace(); 
}} 

的onclick您的按鈕,你可以嘗試

currentIndex++; 
repaint(); 

如果你想有一個動畫,你可以改變x或y,當你重新繪製圖像