
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;

public class PahaSilma extends java.applet.Applet
{
        Texture2D getTexture(String filename)
        {
                TextureLoader loader = new TextureLoader(filename, this);
                ImageComponent2D image = loader.getImage();

                if(image == null) {
                        System.out.println("load failed for texture: "+filename);
                        return null;
                }
                Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGB, image.getWidth(), image.getHeight());
                texture.setMagFilter(Texture2D.BASE_LEVEL_LINEAR);
                texture.setMinFilter(Texture2D.BASE_LEVEL_LINEAR);
                texture.setImage(0, image);
                return texture;
        }

        BranchGroup createBranchGroup()
        {
                BranchGroup objRoot = new BranchGroup();
                Appearance appear = new Appearance();
        	
                Box box=new Box(.5f,.5f,.5f, Box.GENERATE_NORMALS|Box.GENERATE_TEXTURE_COORDS, appear);
             TexCoordGeneration texgen1=new TexCoordGeneration(TexCoordGeneration.EYE_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2);    	
                Texture2D tex0=getTexture("images/stone.jpg");
                Texture2D tex1=getTexture("images/envplane.jpg");
        	
                TextureAttributes texAttr0 = new TextureAttributes();
                texAttr0.setTextureMode(TextureAttributes.DECAL);            
                TextureAttributes texAttr1 = new TextureAttributes();              texAttr1.setTextureMode(TextureAttributes.MODULATE);
                             TextureUnitState[] textureUnitState=new TextureUnitState[2];
                textureUnitState[0] = new TextureUnitState(tex0, texAttr0, null);
                textureUnitState[1] = new TextureUnitState(tex1, texAttr1, texgen1);
                
                appear.setTextureUnitState(textureUnitState);
                box.setAppearance(appear);

        TransformGroup objRotate = new TransformGroup();
        objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
                objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                objRotate.addChild(box);
        	
        MouseRotate myMouseRotate = new MouseRotate();
        myMouseRotate.setTransformGroup(objRotate);
        myMouseRotate.setSchedulingBounds(new BoundingSphere());
        objRoot.addChild(myMouseRotate);
                objRoot.addChild(objRotate);

                return objRoot;
        }
	
        public PahaSilma (){
                setLayout(new java.awt.BorderLayout());
        	
                Canvas3D c = new Canvas3D(null);
                add("Center", c);

                SimpleUniverse u = new SimpleUniverse(c);
                u.getViewingPlatform().setNominalViewingTransform();
                u.addBranchGraph(createBranchGroup());
                requestFocus();
        }
	
        public static void main(String argv[])
        {
                new com.sun.j3d.utils.applet.MainFrame(new PahaSilma(), 400, 400);
        }
}
