Creating A Background

I’m creating a Java 3D maze game, and experienceing difficulties. I would like for anyone to help me. I’m trying to add a background texture but at the moment it isn’t working, i would appreciate it very much if anyone can have a look at the code and tell me where im going wrong.

Ps I am very new to Java 3D

import javax.media.j3d.;
import javax.vecmath.
;
import java.awt.;
import java.awt.event.
;
import com.sun.j3d.utils.behaviors.keyboard.;
import com.sun.j3d.utils.geometry.
;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.image.TextureLoader;

public class MazeJava3DBackground extends Frame {
private Canvas3D myCanvas3D = new Canvas3D(null);

private BoundingSphere bounds = new BoundingSphere(new Point3d(10,0,0), 1000);

  private BranchGroup buildViewBranch(Canvas3D c) {
        BranchGroup viewBranch = new BranchGroup();
        Transform3D viewXfm = new Transform3D();
    viewXfm.set(new Vector3f(0f,0f,0f));
        TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
        viewXfmGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        viewXfmGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        BoundingSphere movingBounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        BoundingLeaf boundLeaf = new BoundingLeaf(movingBounds);
        ViewPlatform myViewPlatform = new ViewPlatform();
        viewXfmGroup.addChild(boundLeaf);
    PhysicalBody myBody = new PhysicalBody();
    PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
        viewXfmGroup.addChild(myViewPlatform);
        viewBranch.addChild(viewXfmGroup);
        View myView = new View();
        myView.addCanvas3D(c);
        myView.attachViewPlatform(myViewPlatform);
    myView.setPhysicalBody(myBody);
    myView.setPhysicalEnvironment(myEnvironment);


        KeyNavigatorBehavior keyNav = new KeyNavigatorBehavior(viewXfmGroup);
        keyNav.setSchedulingBounds(movingBounds);
        viewBranch.addChild(keyNav);

        System.out.println("Use arrow keys on the keyboard to move around");

        return viewBranch;
  }



  private void addLights(BranchGroup b) {
        Color3f ambLightColour = new Color3f(0.6f, 0.6f, 0.6f);
        AmbientLight ambLight = new AmbientLight(ambLightColour);
    ambLight.setInfluencingBounds(bounds);
    Color3f dirLightColour = new Color3f(1.0f, 10.0f, 1.0f);
    Vector3f dirLightDir  = new Vector3f(0.0f, -1.0f, -1.0f);
    DirectionalLight dirLight = new DirectionalLight(dirLightColour, dirLightDir);
    dirLight.setInfluencingBounds(bounds);
        b.addChild(ambLight);
    b.addChild(dirLight);

  }

  private BranchGroup createBackgound() {

  BranchGroup bgGeometry = new BranchGroup();
  Appearance App = new Appearance();
  try {Texture tex = new TextureLoader(new java.net.URL("back.jpg"), this).getTexture();
  App.setTexture(tex);
  }
  catch (Exception e){e.printStackTrace();
  }
  Sphere outerWorld = new Sphere( 1.0f, Primitive.GENERATE_TEXTURE_COORDS | Primitive.GENERATE_NORMALS_INWARD, App);
  bgGeometry.addChild(outerWorld);
  Background bg = new Background();
  bg.setApplicationBounds(bounds);
  bg.setGeometry(bgGeometry);
  bgGeometry.addChild(bg);
  return bgGeometry;

}

Well, first, just implementing createBackgound() isn’t enough. You should also call it!

im very new at this can u please tell me where i shud call it, coz im getting so frustrated.

Maybe I’ll try … in the meantime: did you look up the background demo provided with the Java3D SDK?

no i havn’t is it any useful to me? where can i get it from?

yes, I think it does what you want!

You should have it installed with the Java3D SDK at

/demo/java3d/Background/BackgroundGeometry.java

        Background bg = new Background();
        bg.setApplicationBounds(bounds);
            BranchGroup backGeoBranch = new BranchGroup();
        Sphere sphereObj = new Sphere(1.0f, Sphere.GENERATE_NORMALS |
                            Sphere.GENERATE_NORMALS_INWARD |
                          Sphere.GENERATE_TEXTURE_COORDS, 45);
        Appearance backgroundApp = sphereObj.getAppearance();
        backGeoBranch.addChild(sphereObj);
        bg.setGeometry(backGeoBranch);
        objTrans.addChild(bg);

        TextureLoader tex = new TextureLoader(bgImage,
                                    new String("RGB"), this);
        if (tex != null) 
          backgroundApp.setTexture(tex.getTexture());