2014-06-17 57 views
0

我目前正在使用GUI进行基于文本的RPG,并且我似乎无法获取想要用于加载的图像。从项目中的文件夹中加载图像

class BackgroundPanel extends Panel 
{ 
    // The Image to store the background image in. 
    Image img; 
    public BackgroundPanel(String location) 
    { 
     // Loads the background image and stores in img object. 
     img = Toolkit.getDefaultToolkit().createImage(location); 
    } 

    public void paintComponent(Graphics g) 
    { 
     // Draws the img to the BackgroundPanel. 
     g.drawImage(img, 0, 0, null); 
     img.setAccelerationPriority(SCALE_FAST); 
    } 
} 

这是我用于面板本身的代码。我试图把图像文件放在我的项目的根目录中,但它似乎没有帮助。我在我打算用于所有图像的项目中创建了一个文件夹。

我不确定这里出现了什么问题。我知道我尝试过使用paint()而不是paintComponent(),但随后出于某种原因,按钮和其他组件将不会绘制,直到您将它们放在上面为止。

任何想法?

+0

它应该是'extends JPanel' – Braj

+0

将@Override注释添加到paintComponent方法。这应该有助于确定问题。从来没有任何理由为什么paintComponent应该公开 – MadProgrammer

回答

0

我已经在下面的链接中发布了它在相同的上下文中。请看看:


它在甲骨文的Java已经说明教程Loading Images Using getResource

注:不要忘记调用super.paintComponent()在覆盖paintComponent()方法。

+0

嗨Braj。目前我加载图像为'FileSystemResource res = new FileSystemResource(new File(“D:/Logo.jpg”));'是否可以从我的项目结构加载。像“文件(/ webappName /图像)'? –

+0

它是一个Web项目还是一个独立的摇摆项目?你在Applet中使用它吗?您是否尝试过我从其他帖子分享您的所有链接? – Braj

+0

这是一个春天的web应用程序项目。我看着你的帖子,它更加摇摆。你能帮我吗? –

相关问题