2016-09-21 71 views
-2

我在學校有一個練習,你的意圖是改變代碼,使圖像移動。問題是,圖像不首先顯示,我不知道爲什麼。這裏是代碼:我的圖像沒有顯示,爲什麼?

public void exercise1e() { 
    Random rand = new Random(); 
    ImageIcon image = new ImageIcon("images/gubbe.jpg"); 
    PaintWindow_GU1.showWindow(600, 400, "P1", Color.WHITE); 
    PaintWindow_GU1.addSound("Skor", "sounds/trasiga_skor.mp3"); 
    PaintWindow_GU1.playSound("Skor"); 
    int width = PaintWindow_GU1.getBackgroundWidth(); 
    int height = PaintWindow_GU1.getBackgroundHeight(); 
    int dx = -2; 
    int dy = 1; 
    int x = 250; 
    int y = rand.nextInt(height-100); 
    PaintWindow_GU1.addIcon("Gubbe", image, 250, y, true); 


    while(true) { 
     PaintWindow_GU1.setIconXY("Gubbe",x,y); 
     PaintWindow_GU1.pause(20); 
     x += dx; 
     y += dy; 
     if(x<0) { 
      dx = -dx; 
     } 
    } 
} 
+3

Java不是JavaScript! – Li357

+0

您的程序永遠不會變爲空閒狀態,請使用計時器。 –

+0

試試這個例子:http://stackoverflow.com/questions/8333802/displaying-an-image-in-java-swing – esmoreno

回答

-1

您可能需要在您的工作區中創建一個圖像文件夾,以便它顯示出像Ivaro18解釋。

+1

這應該是評論。 –