2016-05-16 86 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 –