2013-04-04 33 views
0
import java.awt.*; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
public class Images extends JFrame { 
    public static void main(String[] args) { 
    DisplayMode dm = new DisplayMode(800,600,32, DisplayMode.REFRESH_RATE_UNKNOWN); 
    Images I = new Images(); 
    I.run(dm); 
} 

private Screen s; 
private Image bg; 
private Image pic; 
private boolean nLoaded; 
Animation a; 

    public void run(DisplayMode dm) 
    { 

     nLoaded = false; 
     s = new Screen(); 
     try{ 

       s.Setfullscreen(dm, this); 
       LoadPics(); 
       MovieLoop(); 
       try{ 
       Thread.sleep(50000);  
       }catch(Exception ex){} 
     }finally{ 
      s.restoreScreen(); 
     } 

    } 

    public void MovieLoop(){ 
     long startingtime = System.currentTimeMillis(); 
     long cumTime = startingtime; 

     while(cumTime-startingtime < 5000) 
     { 
      long timepassed = System.currentTimeMillis() - cumTime; 
      cumTime += timepassed; 

      a.update(timepassed); 

      Graphics g = s.getFullScreenWindow().getGraphics(); 
      draw(g); 
      g.dispose(); 

      try{ 
      Thread.sleep(20); 
      }catch(Exception ex){} 

     } 
    } 

    public void draw(Graphics g) 
    { 
     g.drawImage(bg, 0,0, null); 
     g.drawImage(a.getImage(),0,0,null); } 


    //Create Load Pictures 
    public void LoadPics() 
    { 
     bg = new ImageIcon("C:\\Gamepics\\BackgroundImage.jpg").getImage(); 
     pic = new ImageIcon("C:\\Gamepics\\SmileyIcon3.png").getImage(); 
     nLoaded = true; 
     repaint(); 


    } 


    public void paint(Graphics g){ 
     if(g instanceof Graphics2D){ 
      Graphics2D g2 = (Graphics2D)g; 
      g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 
     } 

     if(nLoaded) 
     { 
      g.drawImage(bg, 0, 0, null); 
      g.drawImage(pic, 300,300, null); 
     } 
    } 
} 

我不明白我做錯了什麼,我忽視了一切最好的東西。我只是練習一個簡單的動畫,並不斷得到3個空指針異常。 我研究了最好的我可以和顯然NullPointerExceptions在Java中與試圖獲得空數組的大小?編譯器沒有將我的任何列表標記爲問題,所以我有點困惑。任何幫助將不勝感激。所有的錯誤都被註釋掉了。有三個人,他們是在影像類動畫示例中的Java Lang空指針異常?

錯誤:

Exception in thread "main" java.lang.NullPointerException 
at Images.MovieLoop(Images.java:45) 
at Images.run(Images.java:26) 
at Images.main(Images.java:8) 

動畫類

import java.awt.Image; 
import java.util.*; 
public class Animation { 

private ArrayList scenes; 
private int sceneindex; 
private long movietime; 
private long totaltime; 
//CONSTRUCTOR 
public Animation(){ 
    scenes = new ArrayList(); 
    totaltime =0; 
    start(); 

} 
//Add scene to array list and sets time for each scene 
//For example. If you have 3 scenes you would add t to total time three times. So if you had 
//3 Scenes, one for 1s, one for 2s, one for 3s. Total time would be 6s. and you would call addscene 3 times 
public synchronized void addScene(Image i, long t) 
{ 
    totaltime+=t; 
    scenes.add(new OneScene(i, totaltime)); 

} 
//start animation from beggininign inignngingingnig 
public synchronized void start(){ 
movietime = 0; 
sceneindex = 0; 
} 
//change scenes 


//movie time is the sum of all the time passed from last update 
//If you have more than one scene. timepassed gets added to movietime. 
//if movietime is greater than or equal to total time(ie. animation is complete) restart the animation 
public synchronized void update(long timepassed) 
{ 
    if(scenes.size() > 1){ 
     movietime += timepassed; 
     if(movietime >= totaltime) 
     { 
      movietime = 0; 
      sceneindex = 0; 

     } 
     while(movietime > getScene(sceneindex).endTime) 
     { 
      sceneindex++; 
     } 
    } 
} 

public synchronized Image getImage(){ 
    if(scenes.size() == 0){ 
     return null;} 
    else{ 
     return getScene(sceneindex).pic; 
    } 
} 
//Getscene 
private OneScene getScene(int x){ 
    return (OneScene)scenes.get(x); 
} 
//Scenes are gonna be 
private class OneScene{ 
    Image pic; 
    long endTime; 

    public OneScene(Image pic, long endTime) 
    { 
     this.pic = pic; 
       this.endTime = endTime; 

    } 
} 
} 

我包括動畫類,因爲編譯器是突出這三個方法調用作爲問題來源

a.update(timepassed); 
    MovieLoop(); 
    I.run(dm); 
+2

向我們顯示您的錯誤信息 – 2013-04-04 23:28:19

+2

歡迎來到StackOverflow!爲了讓其他人發現錯誤,如果您添加錯誤消息的堆棧跟蹤,這將有所幫助。 – GameDroids 2013-04-04 23:31:38

回答

1

請注:這是一個很長的評論

讓我們先從Graphics g = s.getFullScreenWindow().getGraphics(); - getGraphics是不是一個好主意,這樣可以返回null

你應該永遠不要嘗試和更新其他任何線程的任何UI組件,你不應該以這種方式直接繪製它,而應該使用paintComponent

您不應該創建自己創建的任何Graphics上下文的任何dispose,這樣可以防止繪製其他組件。

您應該避免重寫paint,尤其是頂級容器,如果沒有其他原因,它不是雙緩衝(頂級容器),並且您還將繪製其他任何子組件。

查看Performing Custom Painting瞭解更多詳情。

您應該嘗試使用ImageIO而不是ImageIconImageIO如果無法讀取文件,將會拋出異常,其中ImageIcon只是默默地失敗,沒有什麼幫助。

+0

「異常在線程 「主」 顯示java.lang.NullPointerException \t在Images.MovieLoop(Images.java:45) \t在Images.run(Images.java:26) \t在Images.main(Images.java: 8) ' – moonbeamer2234 2013-04-05 00:35:17

+0

我猜這就是'Graphics g = s.getFullScreenWindow()。getGraphics();' – MadProgrammer 2013-04-05 00:37:14

+0

實際上它給出了這3行的錯誤: – moonbeamer2234 2013-04-05 01:01:04