Evan
Evan

Reputation: 2635

Image not found in LibGDX

SOLVED, sorry!

I'm following this tutorial here: http://www.gamefromscratch.com/post/2013/10/02/LibGDX-Tutorial-3-Basic-graphics.aspx

For some reason, when running the code provided in that URL, an error comes back telling me that the image cannot be found, which is easily understandable. However, I cannot figure out why this error is coming at me.

Here is the error I am getting, just in case:

> Exception in thread "LWJGL Application"
> com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file:
> /data/jet.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.mygdxgame.MyGdxGame.create(MyGdxGame.java:18)  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:
> \data\jet.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.<init>(Pixmap.java:137)  ... 8 more

Here is the code:

package com.me.mygdxgame;

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.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MyGdxGame implements ApplicationListener {
    private SpriteBatch batch;
    private Texture texture;
    private Sprite sprite;

    @Override
    public void create() {        
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("data/jet.png"));
        sprite = new Sprite(texture);
    }

    @Override
    public void dispose() {
        batch.dispose();
        texture.dispose();
    }

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

        batch.begin();
        sprite.draw(batch);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

Here is my directory to the image:

C:\Android\projects\my-gdx-game-android\assets\data

enter image description here

Here is the directory in Eclipse:

enter image description here

So it looks like the image is not included in the project... How do I import it?

Upvotes: 1

Views: 1669

Answers (3)

Mina Fawzy
Mina Fawzy

Reputation: 21472

First you can write this code

texture = new Texture(Gdx.files.internal("data/jet.png"));

like this (both right)

texture = new Texture("jet.png");

Second Make sure your file name dosn't have space before first letter (in asset folder) cause reading " jet.png" is different from "jet.png"

Third Clean & refresh your project

Clean 1- Project ---> Clean

Refresh Gradle 2- Right Click ---> Cradle ---> Refresh All

enter image description here

Make sure you import files by drag and copy don't link it

your game desktop project ----> assets Drag your files & make sure you choose Copy

enter image description here

Upvotes: 0

Jorgesys
Jorgesys

Reputation: 126563

Refresh your project, right click over the project > Refresh or F5, so you can load the "jet.png" resource.

If you load the image directly on your Eclipse´s project explorer you will have no problems, but if you load only in Windows Explorer you have to refresh your project.

Upvotes: 1

Evan
Evan

Reputation: 2635

Solved using Import... -> File System -> Find my images and add them to the project.

Upvotes: 0

Related Questions