Search code examples
eclipselibgdxpixmap

Using Eclipse and libgdx, it cant 'find' and 'load' my png image


i am new to computer programming, and i am following a Android Game Development. I'm on Tutorial Android Game Development in Java - Part 3: Processing Input, and in the first five minutes he has you put a png file into the assets/data folder in the "Tutorial-android" directory. I did so, and followed him. He successfully loaded the picture of mario, i cant. i copied his exact code,

package com.me.tutorial;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Tutorial implements ApplicationListener {
;
SpriteBatch batch;
Texture mario;

Vector2 position;
@Override
public void create() {      
batch = new SpriteBatch();
mario = new Texture(Gdx.files.internal("mario.png"));


position = new Vector2(50,50);
}

@Override
public void dispose() {
}

@Override
public void render() {      
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


batch.begin();
batch.draw(mario, position.x, position.y);
batch.end();
}

i got these errors

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException:           Couldn't load file: mario.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at    com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:142)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:112)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:104)
at com.me.tutorial.Tutorial.create(Tutorial.java:19)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:115)    

Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: mario.png (Internal) at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:134) at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:218) at com.badlogic.gdx.graphics.Pixmap. (Pixmap.java:137) ... 8 more

i looked and i cannot find a way to solve this. please help me, Thank you.


Solution

  • The Gdx.files.internal("path") looks into the sourcefolders in your jar. On Android this sourcefolder is, like @BennX said the asset folder and as much as i know it has to be that folder. On Desktop you can have your own folder, but you need to mark it as source folder in Eclipse. So if your Projectstructure is like: asset/textures/mario.png you have to call Gdx.files.internal("textures/mario.png");.