/* Auteur: Nicolas JANEY, nico@univ-fcomte.fr */

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Enumeration;

public class BrasRobot extends Applet {

  public String host ;
  public float angle1 = 0.0F ;
  public float angle2 = 0.0F ;
  public float angle3 = 0.0F ;
  private static float extension = (float) Math.PI/4 ;
  private TransformGroup l1RotTrans ;
  private Transform3D yAxis ;
  private TransformGroup articulation ;
  private Transform3D arti ;
  private TransformGroup rotationTete ;
  private Transform3D rotTete ;

  public BranchGroup createSceneGraph(SimpleUniverse u) {
    Color3f noir = new Color3f(0.0f,0.0f,0.0f);
    Color3f blanc = new Color3f(1.0f,1.0f,1.0f);
    Color3f jaune = new Color3f(1.0f,1.0f,0.0f);
    Color3f gris = new Color3f(0.6f,0.6f,0.6f);
    Color3f vert = new Color3f(0.0f,1.0f,0.0f);
    Color3f rouge = new Color3f(1.0f,0.0f,0.0f);
    Color3f bleu = new Color3f(0.0f,0.0f,1.0f);
    Color3f grisFonce = new Color3f(0.2f, 0.2f, 0.2f);
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
    Transform3D t;
    BranchGroup objRoot = new BranchGroup();
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(0.3);
    objScale.setTransform(t3d);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);

    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);

    TransformGroup viewTransform = new TransformGroup();
    Transform3D tr = new Transform3D();
    tr.setTranslation(new Vector3f(-1.0F,-1.0F,0.0F));
    viewTransform.setTransform(tr);

    TransformGroup viewRotation = new TransformGroup();
    Transform3D viewRot = new Transform3D();
    viewRot.setRotation(new AxisAngle4f(1.0F,1.0F,0.0F,(float) Math.PI/5.0F)) ;
    viewRotation.setTransform(viewRot);

    TransformGroup rotx1 = new TransformGroup();
    Transform3D rx1 = new Transform3D();
    rx1.setRotation(new AxisAngle4f(1.0F,0.0F,0.0F,(float) Math.PI/2.0F)) ;
    rotx1.setTransform(rx1);

    TransformGroup rotx2 = new TransformGroup();
    Transform3D rx2 = new Transform3D();
    rx2.setRotation(new AxisAngle4f(1.0F,0.0F,0.0F,(float) Math.PI/2.0F)) ;
    rotx2.setTransform(rx2);

    Material matRouge = new Material(gris,noir,rouge,rouge,100.0f);
    Appearance a1 = new Appearance();
    matRouge.setLightingEnable(true);
    a1.setMaterial(matRouge);
    Box box1 = new Box(1.0F,0.15F,1.0F,a1);

    Material matVert = new Material(gris,noir,vert,vert,100.0f);
    Appearance a2 = new Appearance();
    matVert.setLightingEnable(true);
    a2.setMaterial(matVert);
    Box box2 = new Box(1.0f,0.1F,0.1F,a2);

    Cylinder cyl1 = new Cylinder(0.4F,0.25F,a2);
    Cylinder cyl2 = new Cylinder(0.2F,0.25F,a2);

    Material matBleu = new Material(gris,noir,bleu,bleu,100.0f);
    Appearance a3 = new Appearance();
    matVert.setLightingEnable(true);
    a3.setMaterial(matBleu);
    Box box3 = new Box(1.0f,0.1F,0.1F,a3);

    AmbientLight aLgt = new AmbientLight(grisFonce);
    aLgt.setInfluencingBounds(bounds);

    l1RotTrans = new TransformGroup();
    l1RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    yAxis = new Transform3D();
    yAxis.rotZ(angle1);
    l1RotTrans.setTransform(yAxis);

    Vector3f lDirect1 = new Vector3f(0.0F,0.0F,4.0F);
    lDirect1.negate();
    Light lgt1 = new DirectionalLight(blanc,lDirect1);
    lgt1.setInfluencingBounds(bounds);

    Group robot = new Group() ;

    TransformGroup translation1 = new TransformGroup();
    Transform3D trans = new Transform3D();
    trans.setTranslation(new Vector3f(1.0F,0.0F,0.0F));
    translation1.setTransform(trans);

    TransformGroup translation2 = new TransformGroup();
    Transform3D trans2 = new Transform3D();
    trans2.setTranslation(new Vector3f(1.0F,0.0F,0.0F));
    translation2.setTransform(trans2);

    articulation = new TransformGroup();
    articulation.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    arti = new Transform3D();
    arti.rotZ(-angle2); ;
    articulation.setTransform(arti);

    TransformGroup translation3 = new TransformGroup();
    Transform3D trans3 = new Transform3D();
    trans3.setTranslation(new Vector3f(1.0F,0.0F,0.0F));
    translation3.setTransform(trans3);

    TransformGroup translation4 = new TransformGroup();
    Transform3D trans4 = new Transform3D();
    trans4.setTranslation(new Vector3f(1.0F,0.0F,0.0F));
    translation4.setTransform(trans4);

    rotationTete = new TransformGroup();
    rotationTete.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    rotTete = new Transform3D();
    rotTete.rotX(angle3) ;
    rotationTete.setTransform(rotTete);

    Material matJaune = new Material(gris,noir,jaune,jaune,100.0f);
    Appearance a4 = new Appearance();
    matJaune.setLightingEnable(true);
    a4.setMaterial(matJaune);
    Box box4 = new Box(0.1f,0.25F,0.4F,a4);

    Material matBlanc = new Material(gris,noir,blanc,blanc,100.0f);
    Appearance a5 = new Appearance();
    matBlanc.setLightingEnable(true);
    a5.setMaterial(matBlanc);
    Box box5 = new Box(0.1f,0.3F,0.3F,a5);
    Box box6 = new Box(0.1f,0.3F,0.3F,a5);

    objRoot.addChild(objScale);
    objScale.addChild(bg);
    objScale.addChild(viewTransform);
    viewTransform.addChild(viewRotation);
    viewRotation.addChild(box1);
    viewRotation.addChild(rotx1);
    rotx1.addChild(cyl1);
    viewRotation.addChild(l1RotTrans);
    l1RotTrans.addChild(robot);
    robot.addChild(translation1);
    translation1.addChild(box2);
    box2.addChild(translation2);    
    translation2.addChild(articulation);    
    articulation.addChild(rotx2);
    rotx2.addChild(cyl2);
    articulation.addChild(translation3);    
    translation3.addChild(box3);
    box3.addChild(translation4);
    translation4.addChild(rotationTete);    
    rotationTete.addChild(box4);
    objScale.addChild(aLgt);
    objScale.addChild(lgt1);
    objRoot.compile();
    return objRoot;
  }

  public void miseAJour(int a,int b,int c) {
    angle1 = 0.4F+(float) Math.toRadians(a)/2.0F;
    angle2 = 0.4F+(float) Math.toRadians(b)/2.0F;
    angle3 = 0.4F+(float) Math.toRadians(c)/2.0F;
    yAxis.rotZ(angle1);
    l1RotTrans.setTransform(yAxis);
    arti.rotZ(-angle2); ;
    articulation.setTransform(arti);
    rotTete.rotX(angle3) ;
    rotationTete.setTransform(rotTete);
  }

  public void init() {
    URL url = getDocumentBase() ;
    host = url.getHost() ;
    System.out.println("----> "+host) ;
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D c3D = new Canvas3D(config);
    add("Center",c3D);
    SimpleUniverse u = new SimpleUniverse(c3D);
    BranchGroup scene = createSceneGraph(u);
    u.getViewingPlatform().setNominalViewingTransform();
    u.addBranchGraph(scene);
    ThreadBrasRobot te6 = new ThreadBrasRobot(this) ;
    te6.start() ;
    }

  public BrasRobot() {
  }

  public static void main(String[] args) {
    new MainFrame(new BrasRobot(),400,256);
  }
}



