6 Replies Latest reply: Oct 28, 2009 10:48 AM by 795507 RSS

    Problem rendering tileset image

    795507
      I can't for the life of me figure out what is wrong with the following code:
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.IOException;
      import javax.imageio.ImageIO;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      
      public class CTFMain extends JPanel
      {
          BufferedImage tileset;
      
          CTFMain()
          {
              super();
              
              int width = 400;
           int height = 400;
      
           setPreferredSize(new Dimension(width, height));
      
              try
              {
                  //tileset = Toolkit.getDefaultToolkit().createImage("images\\EnvironmentTile.png");
                  tileset = ImageIO.read(new File("images\\EnvironmentTile.png"));
      
              }
              catch(IOException e)
              {
      
              }
          }
      
          public void tic()
          {
              repaint();
              System.out.println(tileset.toString());
          }
      
          @Override
          public void paintComponent(Graphics g)
          {
              g.setColor(Color.white);
              g.fillRect(0, 0, getWidth(), getHeight());
              g.setColor(Color.red);
              g.fill3DRect(20, 30, 150, 350, true);
              
              if(tileset == null)
              {
                  System.out.println("it's null");
              }
              else
              {
                  g.drawImage(tileset, 0, 0, this);
              }
          }
          
          public static void main(String[] args)
          {
              JFrame frame = new JFrame("Capture the Flag");
              CTFMain game = new CTFMain();
              
              frame.add(game);
              frame.pack();
              frame.setResizable(true);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
              
              while(true)
              {
                  game.tic();
      
                  try
                  {
                      Thread.sleep(1000/33);
                  }
                  catch(Exception e)
                  {
      
                  }
              }
          }
      }
      Whenever I run the program, tileset gets assigned a null value, even though the path file is there, so when it calls drawImage, it draws a blank picture. I've tried both the Toolkit.getImage and the Image.IO methods (as shown above). What am I doing wrong?
        • 1. Re: Problem rendering tileset image
          gimbal2
          well the file really does not exist, otherwise it would work. So the problem must lie in what the absolute path of the file is, as you are passing a relative path.

          The way I do it is to put the files in the classpath. Then you can load them through the classloader, with ClassLoader.getResourceAsStream(). When you have your resources on the classpath, you can even bundle them with your application in the same jar, and it will work the same on all systems. Or you could put them in a separate jar if you want.
          • 2. Re: Problem rendering tileset image
            795507
            Okay, now I am confused. I took the program onto a flash drive (just took the whole NetBeans project folder and copied it) and moved it to one of the computers in my programming class, where they only had an editor called jEdit (don't know if you've heard of it or not). I compiled it in the shell, planning on asking my teacher for help, but it worked! Now I am still on jEdit, taking the program to the next step, and haven't received any serious errors yet.

            Why would it work in the command shell, but not in NetBeans?
            • 3. Re: Problem rendering tileset image
              gimbal2
              did you run it FROM netbeans? In that case the working directory is different. You can manually set the working directory in the project properties, set it to where the files are that you are trying to load.
              • 4. Re: Problem rendering tileset image
                795507
                It says the directory is in the src folder in my project file. I already have everything in that folder. All I did to compile it in the shell was to add the batch file in that same folder.

                Plus, I'm not getting any NullPointerExceptions (or any exceptions), so doesn't that mean it can see the file? Just for some reason it isn't assigning it to tileset in NetBeans but it is in the shell?
                • 5. Re: Problem rendering tileset image
                  gimbal2
                  it is just designed this way, trying to render a null image simply does nothing.

                  Do the following: create some directory somewhere where you put the files of your application. Choose any directory, but make it one that is not part of the netbeans project. Now in the project properties simply make the working directory (I think you can find it under the run menu option, I don't have netbeans installed here at work) point to your newly created directory.

                  As an example: you use c:\project\game\data as your working directory. If you run the application and you load a file from 'tiles\tileset.png', then the full path on disc should be c:\project\game\data\tiles\tileset.png.
                  • 6. Re: Problem rendering tileset image
                    795507
                    Perfect. Worked like a charm. Thanks much.