2016-05-16 73 views
0

很簡單,我無法繪製此圖像。`Graphics.drawImage()`不會繪製

public class RenderMap extends JPanel { 

    static BufferedImage brick; 
    static BufferedImage groundb; 

    public static void main(String[] args) { 
     JFrame window = new JFrame("Super Mario"); 
     RenderMap content = new RenderMap(); 
     window.setContentPane(content); 
     window.setBackground(Color.WHITE); 
     window.setSize(1200, 800); 
     window.setLocation(100,0); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setResizable(false); 
     window.setVisible(true); 

     try { 
      brick = ImageIO.read(new File("SuperMario/brick.png")); 
     } catch (IOException e) { 
     } 

     try { 
      URL url = new URL("SuperMario/brick.png"); 
      brick = ImageIO.read(url); 
     } catch (IOException e) { 
     } 
     try { 
      groundb = ImageIO.read(new File("SuperMario/ground.png")); 
     } catch (IOException e) { 
     } 
     try { 
      URL url = new URL("SuperMario/ground.png"); 
      groundb = ImageIO.read(url); 
     } catch (IOException e) { 
     } 
    } 

    public Ground ground; 

    public RenderMap() {} 

    public void paintComponent(Graphics g) { 

     if(ground == null) { 
      ground = new Ground(); 
     } 
     ground.draw(g); 
    } 

    public class Ground implements ImageObserver { 

     Ground(){} 

     void draw(Graphics g) { 
      g.drawImage(groundb, 0, 0, 1200, 800, this); 
      g.fillOval(8, 8, 16, 16); 
     } 

     @Override 
     public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { 
      // TODO Auto-generated method stub 
      return false; 
     } 
    } 
} 

這繪製了橢圓形,但不是圖像。我試圖改變到另一個圖像(即在另一個程序中運行),它仍然無法工作,所以我知道這不是圖像的問題。

+0

夥計,這看起來像你從學校做的功課!無論如何,你可以檢查'.read()'圖像時是否拋出'IOException'。 「catch」塊都是空白的,所以你不知道它是否成功加載圖像文件。 –

+0

@lanthe:我不認爲它是家庭作業 - 該文件夾命名爲'SuperMario' :) @chris:嘗試'在你的catch塊中的e.printStackTrace()'作爲lanthe建議 - 之後,在這裏發佈輸出。 – chris

+0

@lanthe:是的,這是一個學校項目! –

回答

0

懷疑是圖像未正確加載。

java.net.MalformedURLException: no protocol: SuperMario/brick.png 
at java.net.URL.<init>(URL.java:586) 
at java.net.URL.<init>(URL.java:483) 
at java.net.URL.<init>(URL.java:432) 
at RenderMap.main(RenderMap.java:40) 

java.net.MalformedURLException: no protocol: SuperMario/ground.png 
at java.net.URL.<init>(URL.java:586) 
at java.net.URL.<init>(URL.java:483) 
at java.net.URL.<init>(URL.java:432) 
at RenderMap.main(RenderMap.java:51) 

由於您傳入了無效的URL,因此無法通過URL調用函數調用。正確的網址格式應該像您如何通過網絡瀏覽器訪問它們,例如:http://someplace.com/SuperMarioFolder/brick.png

要加載圖像,只能選擇一種方式來閱讀它們。無論是:

  1. URL - 取出File塊,使文件通過完整的URL訪問。

    try { 
        URL url = new URL("http://www.proper-url.com/SuperMario/ground.png"); 
        groundb = ImageIO.read(url); 
    } catch (IOException e) { } 
    
  2. File - 刪除URL塊。這將只允許程序訪問本地文件。

    try { 
        groundb = ImageIO.read(new File("SuperMario/ground.png")); 
    } catch (IOException e) { 
    } 
    

對於項目的宗旨,相信選項#2就足夠了。

繼續前進,您可能想要使用IDE的調試功能逐步執行代碼執行。如果你不使用它,你可能想要IntelliJEclipse

+0

@lanthe:現在我刪除了URL,但它仍然無法運行。 PS在我添加到e.printStackTrace()後不回覆任何錯誤。嘗試{ \t \t \t \t brick = ImageIO.read(new File(「SuperMario/brick.png」)); \t \t \t}趕上(IOException的E1){ \t \t \t \t // TODO自動生成的catch程序塊 \t \t \t \t e1.printStackTrace(); \t \t \t} 嘗試{ \t \t \t \t groundb = ImageIO.read(新文件( 「SuperMario/ground.png」)); \t \t \t}趕上(IOException的發送){ \t \t \t \t // TODO自動生成的catch程序塊 \t \t \t \t e.printStackTrace(); \t \t \t} –

+0

如果沒有拋出IOException,那就沒問題---這意味着它可以讀取文件。無論如何,你會嘗試將文件路徑更改爲像C:/ test/SuperMario/ground.png這樣的完整文件路徑嗎? –

+0

@lanthe謝謝,我發現了問題並解決了它,我忘了在paintComponent方法中添加重繪():p –