
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Font;
import java.awt.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;

import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;

import java.awt.event.*;
import java.util.Enumeration;

import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;


public class TestBehavior extends JPanel implements ActionListener {
	static StringBuffer temperature;
	JTextField temp,temp5;
  Thread thread;

  public class SimpleBehavior extends Behavior{
    private String text1="30.00";
    private String text2="48.11";
    private String save="";
    private String text;
    private Text3D txt;

    // create SimpleBehavior
    SimpleBehavior(Text3D txt){
      this.txt = txt;
      this.text = txt.getString();
      this.save = this.text;
    }

    // initialize the Behavior
    //     set initial wakeup condition
    //     called when behavior beacomes live
    public void initialize(){
      // set initial wakeup condition
      this.wakeupOn(new WakeupOnElapsedTime(1000));
    }

    // behave
    // called by Java 3D when appropriate stimulus occures
    public void processStimulus(Enumeration criteria){
      // decode event

      // do what is necessary
      this.text=this.txt.getString();
      if (temperature.toString()!=save){
        this.txt.setString(temperature.toString());
        save = temperature.toString();
      }

      this.wakeupOn(new WakeupOnElapsedTime(1000));
    }
  } // end of class SimpleBehavior

  public BranchGroup createSceneGraph(String s) {
	  // Create the root of the branch graph
	  float sl = s.length();
	  // Create the root of the branch graph
	  BranchGroup objRoot = new BranchGroup();
    Alpha alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,10000, 0, 0, 0, 0, 0);
    //Alpha alpha = new Alpha(-1, 10000);

    // Create a Transformgroup to scale all objects so they
    // appear in the scene.
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    // Assuming uniform size chars, set scale to fit string in view
	  t3d.setScale(1.2/sl);
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);


	  // Create the transform group node and initialize it to the
	  // identity.  Enable the TRANSFORM_WRITE capability so that
	  // our behavior code can modify it at runtime.  Add it to the
	  // root of the subgraph.
	  TransformGroup objTrans = new TransformGroup();
	  objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	  objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	  objScale.addChild(objTrans);

    Font3D f3d = new Font3D(new Font("Helvetica", Font.PLAIN, 1),new FontExtrusion());
	  Text3D txt = new Text3D(f3d, "+55.33",new Point3f( -sl/2.0f, -1.f, -1.f));
	  txt.setCapability(Text3D.ALLOW_STRING_READ);
	  txt.setCapability(Text3D.ALLOW_STRING_WRITE);
	  Shape3D sh = new Shape3D();
	  Appearance app = new Appearance();
	  Material mm = new Material();
	  mm.setLightingEnable(true);
	  app.setMaterial(mm);
	  sh.setGeometry(txt);
	  sh.setAppearance(app);
	  //objTrans.addChild(sh);

	  BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
    ////////
    //TransformGroup target = new TransformGroup();
    //Transform3D axisOfRotPos = new Transform3D();
    float[] knots = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.6f, 0.8f, 0.9f, 1.0f};
    Quat4f[] quats = new Quat4f[9];
    Point3f[] positions = new Point3f[9];

    //target.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    AxisAngle4f axis = new AxisAngle4f(1.0f,0.0f,0.0f,0.0f);
    t3d.set(axis);

    quats[0] = new Quat4f(0.0f, 1.0f, 1.0f, 0.0f);
    quats[1] = new Quat4f(1.0f, 0.0f, 0.0f, 0.0f);
    quats[2] = new Quat4f(0.0f, 1.0f, 0.0f, 0.0f);
    quats[3] = new Quat4f(0.0f, 1.0f, 1.0f, 0.0f);
    quats[4] = new Quat4f(0.0f, 0.0f, 1.0f, 0.0f);
    quats[5] = new Quat4f(0.0f, 1.0f, 1.0f, 0.0f);
    quats[6] = new Quat4f(1.0f, 1.0f, 0.0f, 0.0f);
    quats[7] = new Quat4f(1.0f, 0.0f, 0.0f, 0.0f);
    quats[8] = quats[0];

    positions[0]= new Point3f(  0.0f,  0.0f, -1.0f);
    positions[1]= new Point3f(  1.0f, -1.0f, -2.0f);
    positions[2]= new Point3f( -1.0f,  1.0f, -3.0f);
    positions[3]= new Point3f(  2.0f,  0.0f, -4.0f);
    positions[4]= new Point3f( -2.0f, -1.0f, -5.0f);
    positions[5]= new Point3f(  3.0f,  1.0f, -6.0f);
    positions[6]= new Point3f( -3.0f,  0.0f, -7.0f);
    positions[7]= new Point3f(  2.0f, -1.0f, -4.0f);
    positions[8]= positions[0];

    RotPosPathInterpolator rotPosPath = new RotPosPathInterpolator(
                alpha, objTrans, t3d, knots, quats, positions);
    rotPosPath.setSchedulingBounds(new BoundingSphere());

    //objRoot.addChild(objTrans);
    objRoot.addChild(rotPosPath);
    //objTrans.addChild(new ColorCube(0.4));
    objTrans.addChild(sh);
    ///////

    ///mouse behavior
    MouseRotate myMouseRotate = new MouseRotate();
    myMouseRotate.setTransformGroup(objTrans);
    myMouseRotate.setSchedulingBounds(new BoundingSphere());
    objRoot.addChild(myMouseRotate);

    MouseTranslate myMouseTranslate = new MouseTranslate();
    myMouseTranslate.setTransformGroup(objTrans);
    myMouseTranslate.setSchedulingBounds(new BoundingSphere());
    objRoot.addChild(myMouseTranslate);

    MouseZoom myMouseZoom = new MouseZoom();
    myMouseZoom.setTransformGroup(objTrans);
    myMouseZoom.setSchedulingBounds(new BoundingSphere());
    objRoot.addChild(myMouseZoom);

	  // Set up the background
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
    Background bgNode = new Background(bgColor);
    bgNode.setApplicationBounds(bounds);
    objRoot.addChild(bgNode);

    // Set up the ambient light
    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    objRoot.addChild(ambientLightNode);

    // Set up the directional lights
    Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
    Vector3f light1Direction  = new Vector3f(4.0f, -7.0f, -12.0f);
    Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
    Vector3f light2Direction  = new Vector3f(-6.0f, -2.0f, -1.0f);

    DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);

    DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction);
    light2.setInfluencingBounds(bounds);
    objRoot.addChild(light2);

    SimpleBehavior myTestBehavior = new SimpleBehavior(txt);
    myTestBehavior.setSchedulingBounds(new BoundingSphere());
    objRoot.addChild(myTestBehavior);

	  // Let Java 3D perform optimizations on this scene graph.
    objRoot.compile();

	  return objRoot;
  }


  // Create a simple scene and attach it to the virtual universe
  public TestBehavior() {
    setLayout(new BorderLayout());
    this.setPreferredSize(new Dimension(600,600));

    Canvas3D canvas3D = new Canvas3D(null);
    add("Center", canvas3D);

    BranchGroup scene = createSceneGraph("Lala");

    // SimpleUniverse is a Convenience Utility class
    SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

	  // This will move the ViewPlatform back a bit so the
	  // objects in the scene can be viewed.
    simpleU.getViewingPlatform().setNominalViewingTransform();

    simpleU.addBranchGraph(scene);

    JPanel endHalf = new JPanel();
    endHalf.setLayout(new BoxLayout(endHalf, BoxLayout.X_AXIS));

    JButton b1 = new JButton("Ok");
    b1.setActionCommand("quit");
    b1.addActionListener(this);

    temp = new JTextField(6);
    temp5 = new JTextField(6);
    endHalf.add(temp);
    endHalf.add(temp5);

    JButton b2 = new JButton("Set");
    b2.setActionCommand("set");
    b2.addActionListener(this);

    endHalf.add(b1);
    endHalf.add(b2);

    add(endHalf,BorderLayout.SOUTH);
    thread = new Thread(new Runnable(){
      public void run(){
        while(true){
          try{
            ParseTemp();
            thread.sleep(1000);
            System.out.println("looping  ... ");
          }
          catch(InterruptedException ex){}
        }
      }
      });
    thread.start();
  }

  public void actionPerformed(ActionEvent e) {


    String command = e.getActionCommand();
    if (command == "quit") {
      System.exit(0);
    }

    if (command == "set"){
      ParseTemp();
    }
  }

  // Construct the temperature string
  void ParseTemp(){

    long long_time =  System.currentTimeMillis();
    Long l = new Long(long_time);
    String s_time = l.toString();

    System.out.println("user.timezone : " + System.getProperty("user.timezone"));
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy HH:mm:ss zz");
    sdf.setTimeZone( TimeZone.getDefault() );
    System.out.println(sdf.format(new Date()));

    String tmp = sdf.format(new Date()).toString();


    //String tmp = temp.getText();
    Float ff;
    int appended=0;
    int cont=0;
    StringBuffer tp;

    tp = new StringBuffer(tmp);

    temperature = new StringBuffer(tmp.substring(9,18));
    System.out.println("made it through ... ");
  }

  //  The following allows this to be run as an application
  //  as well as an applet
  public static void main(String[] args) {
	  temperature = new StringBuffer(6);
	  temperature.append("+00.00");
    JFrame frame = new JFrame();
	  frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
	  });
	  frame.getContentPane().add(new TestBehavior());
	  frame.pack();
	  frame.setVisible(true);
  }
}
