
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 javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;

import java.awt.event.*;
import java.util.Enumeration;


public class TestBehavior extends JPanel implements ActionListener {

	static StringBuffer temperature;
	JTextField temp,temp5;
	
    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();

        // 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);

	// 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);



    } 

    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(){

    String tmp = temp.getText();
    Float ff;
    int appended=0;
    int cont=0;
    StringBuffer tp;
    
    tp = new StringBuffer(6);
    
    try {
    	ff = new Float(tmp);
    } catch ( NumberFormatException n ){
    	ff = new Float(100f);
    }
    
    if (ff.floatValue()>-99.99f && ff.floatValue()<99.99f){
    	if(ff.floatValue()>0f)
    		tp.append('+');
    	else
    		tp.append('-');
    	
    	if ( ff.floatValue()<-9.99f || ff.floatValue()>9.99f){
    		tp.append(Math.abs(ff.intValue()));
    		//System.out.println(ff.intValue());
    	}
    	else {
    		tp.append('0');
    		tp.append(Math.abs(ff.intValue()));
    	}
    	
    	tp.append('.');
    	Float q = new Float(ff.floatValue()*100-ff.intValue()*100);
    	if (q.intValue()==0)
    		tp.append("00");
    	else
    		tp.append(Math.abs(q.intValue()));
    	temperature=tp;
    	temp5.setText(temperature.toString());
    }
    
    }
    
    

    //  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);

    }

} 
