Jump to content
  • Advertisement
Sign in to follow this  
VaderJ

[java] where do I put my resource files??

This topic is 4828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I am doing a tutorial I found on the internet. It is a remake of space invaders. Everything has gone fine so far, but now we are to the point of adding graphics and i can't seem to get it to work. Here is what the guy doing the tutorial says: The image we'll load is called bicho.gif and must be in a subdirectory called "res" (resources) beneath the main directory of our project (but not beneath the package directory). For example, if our class is located in c:\planetalia\curso\java\version04\Invaders.class, then the resource directory must be located in c:\planetalia\curso\java\res\): I am using NetBeans and this is my first time trying to do anything with graphics. I think the problem could be I am just not getting the file into the right place for the program to read it. Program compiles and runs fine, but no image shows up. Wich I guess isn't quite right because it is supposed to show an error if the image isn't found, but nothing shows up. Here is the code:
[source language="java"]
package version04;
2     /**
3      * Curso B?sico de desarrollo de Juegos en Java - Invaders
4      * 
5      * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n
6      * 
7      * http://www.planetalia.com
8      * 
9      */
10    
11    
12    import java.awt.Canvas;
13    import java.awt.Dimension;
14    import java.awt.Graphics;
15    import java.awt.event.WindowAdapter;
16    import java.awt.event.WindowEvent;
17    import java.awt.image.BufferedImage;
18    import java.net.URL;
19    
20    import javax.imageio.ImageIO;
21    import javax.swing.JFrame;
22    import javax.swing.JPanel;
23    
24    public class Invaders extends Canvas {
25      public static final int WIDTH = 800;
26      public static final int HEIGHT = 600;
27      
28      
29      public Invaders() {
30        JFrame ventana = new JFrame("Invaders");
31        JPanel panel = (JPanel)ventana.getContentPane();
32        panel.setBounds(0,0,WIDTH,HEIGHT);
33        panel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
34        panel.setLayout(null);
35        panel.add(this);
36        ventana.setBounds(0,0,WIDTH,HEIGHT);
37        ventana.setVisible(true);
38        ventana.addWindowListener( new WindowAdapter() {
39          public void windowClosing(WindowEvent e) {
40            System.exit(0);
41          }
42        });
43      }
44      
45      public BufferedImage loadImage(String nombre) {
46 URL url=null; 47 try { 48 url = getClass().getClassLoader().getResource(nombre); 49 return ImageIO.read(url); 50 } catch (Exception e) { 51 System.out.println("No se pudo cargar la imagen " + nombre +" de "+url); 52 System.out.println("El error fue : "+e.getClass().getName()+" "+e.getMessage()); 53 System.exit(0); 54 return null; 55 } 56 } 57 58

59      public void paint(Graphics g) {
60        BufferedImage bicho = loadImage("res/bicho.gif");
61 g.drawImage(bicho, 40, 40,this);

62      }
63      
64      public static void main(String[] args) {
65        Invaders inv = new Invaders();
66      }
67    }


If someone that knows NetBeans of maybe something wrong with code could help me out I would really appreciate it. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by VaderJ
The image we'll load is called bicho.gif and must be in a subdirectory called "res" (resources) beneath the main directory of our project (but not beneath the package directory). For example, if our class is located in c:\planetalia\curso\java\version04\Invaders.class, then the resource directory must be located in c:\planetalia\curso\java\res\):


Shouldn't it be in:

c:\planetalia\curso\java\version04\res


Since
loadImage("res/bicho.gif");
will look for a folder named "res" in the current directory where the .class is? I'd give that a try first to make sure that is not the problem, before debugging code. If that does not work, then I can't help you with the code, sorry. Never worked with Java NetBeans.

Share this post


Link to post
Share on other sites
I use:
url = getClass().getClassLoader().getResource(FILENAME);
And it works greate for me.

However I always have the graphics folder in the same folder as my classes...
c:\planetalia\curso\java\version04\res would be what you want.

Share this post


Link to post
Share on other sites
Thanks guys for your help. Well, it turned out to be a code problem lol.

In the tutorial the person who wrote it had put when creating the panel:

setBounds( 0, 0, WIDTH, HEIGHT );

I put panel.setBounds( 0, 0, WIDTH, HEIGHT ); thinking that it was an error. I am still learning so I thought that was how you would have to do it lol. Well anyways thanks again for your help. The program is now working niceley. :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!