Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tilemap not drawing after implementing LoadFromFile [SOLVED]  (Read 2106 times)
0 Members and 1 Guest are viewing this topic.
Offline civicdude95

Senior Newbie


Medals: 1



« Posted 2011-03-29 14:05:35 »

Hey everyone this is my first post and I must say that I like the community that I have seen here and look forward to contributing to other people's success.

I'm making a 2D tile-based (small scale) rpg and cannot for the life of me figure out why my tilemap is not displaying. I just implemented loading maps from files and I know that before this the map was loading and displaying just fine I believe I have narrowed the possibilities down to my images being null when I go to draw them but I cannot seem to figure out why that is. Here is the code for my entire tilemap class with the null image

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
import java.util.*;
import java.awt.*;

import java.io.File;

import javax.swing.*;

public class TileMap{

  ArrayList<Image> textures = new ArrayList<Image>();
 
  int[][] map;
 
  static int tileWidth = 32;
  static int tileHeight = 32;
 
  static int tileMapWidth;
  static int tileMapHeight;
 
  public TileMap(){}    
   public TileMap(int width, int height){
      map = new int[height][width];
      for (int x = 0; x < width; x++)
          for (int y = 0; y < height; y++)
              map[y][x] = -1;

      tileMapWidth = map[0].length;
      tileMapHeight = map.length;
   }  
  public TileMap(int[][] newMap){
      this.map = newMap.clone();
 
      tileMapWidth = map[0].length;
      tileMapHeight = map.length;
  }  
 
  public static TileMap LoadFromFile(String fileName){

    TileMap tileMap;
    ArrayList<ArrayList<Integer>> tempLayout = new ArrayList<ArrayList<Integer>>();
    ArrayList<String> textureList = new ArrayList<String>();
    Scanner input;
    File file;
   
    try{
        file = new File(fileName);
        input = new Scanner(file);
    }catch(Exception e){
        file = null;
        System.out.println("File is null");
        input = null;
        System.out.println("Input is null");
    }
 
    boolean readingTextures = false;
    boolean readingLayout = false;
           
    while(input.hasNext()){
        String line = input.nextLine().trim();
       
        if (line.isEmpty())
            continue;
       
        if (line.contains("[Textures]")){
          readingTextures = true;
          readingLayout = false;
        }
        else if (line.contains("[Layout]")){
          readingLayout = true;
          readingTextures = false;
        }
        else if (readingTextures){
          textureList.add(line);
        }
        else if (readingLayout){
          ArrayList row = new ArrayList();
         
          String[] cells = line.split(" ");
         
          for (String c : cells){
            if (!c.isEmpty())
                row.add(Integer.parseInt(c));
          }
          tempLayout.add(row);
        }          
    }
   
    int width = tempLayout.get(1).size();
    int height = tempLayout.size();
   
    System.out.println(width);
    System.out.println(height);
   
    tileMap = new TileMap(width, height);
   
    for (int y = 0; y < height; y++){
        for (int x = 0; x < width; x++){
            try{
                tileMap.setCellIndex(x, y, tempLayout.get(y).get(x));
            }catch(NullPointerException e){
                System.out.println("SetCellIndex() has returned a NullPointerException");
            }
        }
    }
   
    String[] textureArray = new String[textureList.size()];
    for (int i = 0; i < textureList.size(); i++){
        textureArray[i] = textureList.get(i);
    }
   
    tileMap.LoadTextures(textureArray);

    return tileMap;    
  }  
  public void LoadTextures(String ... fileNames){
      for (int i = 0; i < fileNames.length; i++){
          try{
            textures.add(new ImageIcon(Game.SRC_PATH + fileNames[i]).getImage());
          }catch(Exception e){
            System.out.println("File not added.");
          }
      }
     
  }
 
  public int getCellIndex(int x, int y){
    return map[y][x];
  }  
  public void setCellIndex(int x, int y, int cellIndex){
    map[y][x] = cellIndex;
  }
 
  public static int getWidthInPixels(){
    return tileMapWidth * tileWidth;
  }  
  public static int getHeightInPixels(){
    return tileMapHeight * tileHeight;
  }
 
  public void Draw(Graphics2D g, Camera camera){
                 
      int tileMapWidth = map[0].length;
      int tileMapHeight = map.length;
   
      for (int x = 0; x < tileMapWidth; x++){
          for (int y = 0; y < tileMapHeight; y++){
              int index = map[y][x];
             
              if (index == -1)
                  continue;
             
/////////////////////// NULL IMAGE RIGHT HERE \\\\\\\\\\\\\\\\\\\\\\\\\
              Image image = textures.get(index);
                                         
              try{
                g.drawImage(image,
                            x * tileWidth - (int)camera.Position.X,
                            y * tileHeight - (int)camera.Position.Y,
                            Color.WHITE,
                            null);
              }catch(NullPointerException n){
                System.out.println("Draw Image threw a NullPointerException");
              }
          }
      }
  }
}


If anyone can help me spot the problem I would be very greatful. I thank you for taking the time to read over my code and here is a link to my entire project (zip file) with images and the tilemap text file.
https://docs.google.com/leaf?id=0B-7lPg3Z0pepZTU5NDNlNTAtOGNiMS00ZGExLWIxZjAtNWYxZGJlMjQyYTRj&hl=en&authkey=CIW-nLMK

Thanks everyone!

Read about simple game development.
Download your free copy of Prospectus
Offline kevglass

JGO Kernel


Medals: 191
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2011-03-29 14:38:33 »

Do you get any output on system out while you're running the program?

Kev

Offline kevglass

JGO Kernel


Medals: 191
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2011-03-29 14:40:45 »

It might be to do with:

1  
2  
3  
{
            textures.add(new ImageIcon(Game.SRC_PATH + fileNames[i]).getImage());
}


You'd be best to add some debug telling you what that string resolves to, and whether it's actually accessible. Also you might consider using ImageIO.read() instead of the ImageIcon().getImage();

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline civicdude95

Senior Newbie


Medals: 1



« Reply #3 - Posted 2011-03-29 15:20:45 »

You'd be best to add some debug telling you what that string resolves to, and whether it's actually accessible. Also you might consider using ImageIO.read() instead of the ImageIcon().getImage();

Kev

The only output that I'm getting is a window with the black background that set in my main java class constructor. If I remove that line of code, all I get is a window with the generic grey canvas that java makes

Man, I've added and removed so many debug lines it's disgusting. Let me tell you a few things that I know based on a bunch of System.out.println() - type debugging:
The textureArray that is passed to LoadTextures() contains all six of the textures filenames.
The tilemap layout matches the layout from the text file.
The image is null when the g.DrawImage() is called.


What's the advantage of using ImageIO.read() instead of ImageIcon().getImage()?


Read about simple game development.
Download your free copy of Prospectus
Offline kevglass

JGO Kernel


Medals: 191
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #4 - Posted 2011-03-29 15:26:37 »

ImageIO is immediate loading, not sure ImageIcon works that way, might be asynchronous.

What console output do you get when you run the program I meant, i.e. what are the System.out saying?

Try changing this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void LoadTextures(String ... fileNames){
      for (int i = 0; i < fileNames.length; i++){
          try{
            textures.add(new ImageIcon(Game.SRC_PATH + fileNames[i]).getImage());
          }catch(Exception e){
            System.out.println("File not added.");
          }
      }
       
  }


to this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public void LoadTextures(String ... fileNames){
      for (int i = 0; i < fileNames.length; i++){
          try{
              String location = Game.SRC_PATH + fileNames[i];
              System.out.println("Loading: "+location);    
              Image image = new ImageIcon(location).getImage();
              System.out.println("Loaded image: "+image);
            textures.add(image);
          }catch(Exception e){
            System.out.println("File not added.");
          }
      }
       
  }


And let us know what the console output is - might help diagnose whats going on there?

Kev


Offline civicdude95

Senior Newbie


Medals: 1



« Reply #5 - Posted 2011-03-29 15:45:30 »

Ok I changed the LoadTextures() to what you suggested and I'm still getting the black screen in a window and the console output is:
Loading: "images/grass0.png"
Loaded image: sun.awt.image.ToolkitImage@bbe0f0a
Loading: "images/grass1.png"
Loaded image: sun.awt.image.ToolkitImage@6acd5f8b
Loading: "images/grass2.png"
Loaded image: sun.awt.image.ToolkitImage@513bd574
Loading: "images/dirt0.png"
Loaded image: sun.awt.image.ToolkitImage@2f3adc56
Loading: "images/dirt1.png"
Loaded image: sun.awt.image.ToolkitImage@4157aa53
Loading: "images/dirt2.png"
Loaded image: sun.awt.image.ToolkitImage@626f50a8
Loading: "images/dirt3.png"
Loaded image: sun.awt.image.ToolkitImage@7a187814

which makes me think that the images loaded fine.
I even added block in my Draw() that says
1  
2  
3  
if (image == null){
   System.out.println("Image is null");
}

Which did not show up in the output.

And I even removed the call to set the background to black (in my main class) which did make the window go back to that greyish color.

I am very confused by this bug

Read about simple game development.
Download your free copy of Prospectus
Offline kevglass

JGO Kernel


Medals: 191
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #6 - Posted 2011-03-29 16:14:59 »

I'm confused too, you're not alone! Smiley

Can you update this:

1  
2  
/////////////////////// NULL IMAGE RIGHT HERE \\\\\\\\\\\\\\\\\\\\\\\\\
              Image image = textures.get(index);


to

1  
2  
              Image image = textures.get(index);
              System.out.println("Image for index: "+index+" is "+image);


And post the output, thinking theres got to be something else going on but just to confirm.

Kev

Offline kevglass

JGO Kernel


Medals: 191
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2011-03-29 16:17:32 »

Oh, I guess it'd be good to know what position your camera has also.

Kev

Offline civicdude95

Senior Newbie


Medals: 1



« Reply #8 - Posted 2011-03-29 16:23:38 »

Sure, well it prints out that line for every cell in the map (which it should) and here is some of the output:

Image for index: 0 is sun.awt.image.ToolkitImage@bbe0f0a
Image for index: 0 is sun.awt.image.ToolkitImage@bbe0f0a
Image for index: 0 is sun.awt.image.ToolkitImage@bbe0f0a
Image for index: 2 is sun.awt.image.ToolkitImage@513bd574
Image for index: 1 is sun.awt.image.ToolkitImage@6acd5f8b
Image for index: 1 is sun.awt.image.ToolkitImage@6acd5f8b
Image for index: 1 is sun.awt.image.ToolkitImage@6acd5f8b
Image for index: 1 is sun.awt.image.ToolkitImage@6acd5f8b
Image for index: 0 is sun.awt.image.ToolkitImage@bbe0f0a
Image for index: 0 is sun.awt.image.ToolkitImage@bbe0f0a

I also put a System.out.println("Got here"); after my call to g.DrawImage(); in the try{} block and it was outputting infinitely (which it should because I'm always calling the draw method from main).

I also added in a System.out for the x and y of camera and it is x = 0 and y = 0 at the beginning and is updating when I press my arrow keys (like it should be)

Read about simple game development.
Download your free copy of Prospectus
Offline Addictman

Senior Duke


Medals: 3
Projects: 1


Java games rock!


« Reply #9 - Posted 2011-03-29 17:25:46 »

I guess you looked at the NullpointerException itself, to rule out that it's your Graphics or Camera objects that are null? Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline civicdude95

Senior Newbie


Medals: 1



« Reply #10 - Posted 2011-03-29 17:49:58 »

Yes, well, it's not throwing the exception so I'm assuming that they aren't null. I tried moving the drawImage() call outside of the try{} block and it still didn't work

Read about simple game development.
Download your free copy of Prospectus
Offline Addictman

Senior Duke


Medals: 3
Projects: 1


Java games rock!


« Reply #11 - Posted 2011-03-29 18:12:14 »

Hi.

I got it working Smiley

In your layers.txt file, replace "images/grass0.png" with just images/grass0.png

I also changed image loading to use ImageIO, but I'm not sure if that part mattered at all. The good thing about ImageIO though is that it threw an exception which made me look into the layers.txt.

So now I got mountains'n grass!
Offline civicdude95

Senior Newbie


Medals: 1



« Reply #12 - Posted 2011-03-29 18:19:44 »

Oh my gosh! Thank you so much! I can't believe it was something that simple. Lips Sealed
I guess I should have thought to look there as well as in my code. Thanks very much for your help, now I can move on to bigger and better things!

Were you able to move around Addictman?

Now whenever I System.out the camera x and y it still updates those when I use the arrow keys but it does not redraw the map. Very perplexing to me.

Read about simple game development.
Download your free copy of Prospectus
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

DarkCart (16 views)
2014-10-31 21:44:48

DarkCart (21 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (31 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!