Problems with loading texture file

Hi everybody.



I'm acomplete newbie in jmonkey and  trying to create a HUD following the HUD tutorial in wiki.

I have problems to understand which file format i can use and where

to save it for loading.

Actually my file, it's a png-file  ist stored in the same directory folder as my java source code.

the warning says :"Could not load image.  Specified URL is null."

what does that mean the URL is null?



Hope very much anybody can help me.

If the source code is needed for understanding I will post it of course too. :slight_smile:  


It would certainly help to see some code in this case…



URL is null means that the file could not be found at the path specified.  It also tells me that you'll see [if you haven't already] a NullPointerException in the very near future :wink:

Ok thanks for your reply.



here's my code… hope it will help  to identify the bug.


package A_JMCA;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.math.Vector2f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.util.*;
import com.jme.image.Texture;
import com.jme.scene.*;
import com.jme.util.geom.BufferUtils;
import java.nio.FloatBuffer;
import com.jme.scene.shape.*;




public class HUD extends SimpleGame {
   
   
     private Node hudNode;
     private Quad hudQuad;
     private int textureWidth;
     private int textureHeight;
     private Quaternion rotQuat = new Quaternion();
     private Vector3f axis = new Vector3f(1, 1, 0);
     private Cylinder cylinder;
     private float angle = 0;

    
     public static void main(String[] args) {
         HUD app = new HUD();
         app.setConfigShowMode(SimpleGame.ConfigShowMode.AlwaysShow);
         app.start();
     }
    
     // transforms the Texture Coordinates into UV Coordinates
     private float getUForPixel(int xPixel) {
         return (float) xPixel / textureWidth;
     }
 
     private float getVForPixel(int yPixel) {
         return 1f - (float) yPixel / textureHeight;
     }
 
    
     protected void simpleInitGame() {
         display.setTitle("HUD");
         
         // einen Zylinder erzeugen
         cylinder = new Cylinder("Cylinder", 6, 18, 5, 10);
         cylinder.setModelBound(new BoundingBox());
         cylinder.updateModelBound();
 
         MaterialState ms = display.getRenderer().createMaterialState();
         ms.setAmbient(new ColorRGBA(1f, 0f, 0f, 1f));
         ms.setDiffuse(new ColorRGBA(1f, 0f, 0f, 1f));
         ms.setEnabled(true);
         cylinder.setRenderState(ms);
         cylinder.updateRenderState();
 
         rootNode.attachChild(cylinder);
        
      
        
        
        // Head up Display erstellen
         int displayWidth= display.getWidth();
         int HudHeight = display.getWidth()/4;
         System.out.println("displayWidth = " + displayWidth );
         System.out.println("displayWidth = " + HudHeight );
        
        
         hudNode = new Node("hudNode");
         hudQuad = new Quad("hud", 40f, 40f);
         hudQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
        
          // HUD mittig plazieren 
           hudQuad.setLocalTranslation(new Vector3f(display.getWidth()/2,display.getHeight()/2,0));
         //hudQuad.setLocalTranslation(new Vector3f(0,0,0));
         
         
         LightState ls = display.getRenderer().createLightState();
         ls.setEnabled(true);
         hudQuad.setRenderState(ls);
 
       
         hudQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
         hudQuad.updateRenderState();
 
         hudNode.attachChild(hudQuad);
         rootNode.attachChild(hudNode);
        
         // Textur Manager erstellen um die Textur zu steuern
        
         final TextureState ts = display.getRenderer().createTextureState();
        
         // loading the texture as image file
       
         final Texture texture=  TextureManager.loadTexture(getClass().getResource("texture.png"),Texture.MinificationFilter.Trilinear,Texture.MagnificationFilter.Bilinear, 1.0f,true);
        
         // Textur Zustand festlegen
         ts.setTexture(texture);
         // Texturdaten initialisieren
         textureWidth = ts.getTexture().getImage().getWidth();
         textureHeight = ts.getTexture().getImage().getHeight();
         // Textur aktivieren
         ts.setEnabled(true);
        
        
         // UV Mapping and anwenden der Texturdaten auf dem Quadrat
        
         final FloatBuffer texCoords = BufferUtils.createVector2Buffer(4);
         // Koordinate (0,0) f

so we don't have to dig, this is the relevant line of code:



 final Texture texture=  TextureManager.loadTexture(getClass().getResource("texture.png"),Texture.MinificationFilter.Trilinear,Texture.MagnificationFilter.Bilinear, 1.0f,true);



I  would try:


// Locate the texture
      SimpleResourceLocator locator = new SimpleResourceLocator(new File("path/to/package/texture.png").toURI());
      // Create the texture
      Texture texture = TextureManager.loadTexture(locator.locateResource("path/to/package/texture.png"),
            MinificationFilter.BilinearNearestMipMap,MagnificationFilter.Bilinear);



(the path should include your src folder as well)

uups. Sorry for the long code.  :roll:



your idea worked very well. The texture file was found. Thanx!

But theres still the Problem, that the texture coordinates doesn't match with

the quad ones.

  :?

whats wrong with them?

Well since a quad is a square, does your image have square dimensions? Square dimensions in power of two is preferable (though the power of two is no longer required on all hardware).  For example, 256x256, 512x512, 1024x1024, etc

I've tried a texture with 64x64 on a quad with the dimensons 40x40… like described in the tutorial.

but it didn't work. 

I don't want my quad to be a square but a rectangular, with the width of the display, so actually my squad is

800x 80.

does that work with a texture that is 64x64? even if i'll make my texture 512x512? which will fit better for such dimensons?