// Example 1, RGBTest import java.awt.*; import java.applet.*; import java.lang.*; import vrml.external.field.EventOut; import vrml.external.field.EventInSFColor; // 色を取り込む? import vrml.external.field.EventOutSFColor; // 色を送信する。 import vrml.external.field.EventOutSFTime; //  import vrml.external.field.EventOutObserver; import vrml.external.Node; import vrml.external.Browser; import vrml.external.exception.*; public class vrml52 extends Applet implements EventOutObserver { // ↓実際は必要はありません。実際動いてるかどうか確認のためのものです。 TextArea output = null; // ↓VRMLのブラウザを取得する設定です。 Browser browser = null; Node material = null; EventInSFColor diffuseColor = null; EventOutSFColor outputColor = null; EventOutSFTime touchTime = null; boolean error = false; public void init() { System.out.println("RGBTest.init().."); // ↓ボタンを生成します。そして下はテキストの範囲を設定します。 add(new Button("Red")); add(new Button("Green")); add(new Button("Blue")); output = new TextArea(5, 40); add(output); } public void start() { System.out.println("RGBTest.start().."); output.appendText("\nRGBTest.start()..\n"); // vrmlのブラウザを取り込む設定。 browser = (Browser) vrml.external.Browser.getBrowser(this); output.appendText("Got the Browser!\n"); System.out.println("Got the browser: "+ browser); // ブラウザを取り込むのに成功しました。 try { // ノードを取り込むのに成功しました。 material = browser.getNode("MAT"); output.appendText("Got node : MAT\n"); diffuseColor = (EventInSFColor) material.getEventIn("set_diffuseColor"); output.appendText("Got EventIn : set_diffuseColor\n"); Node sensor = browser.getNode("TOUCH"); output.appendText("Got node: TOUCH\n"); // ノードを取り込む touchTime = (EventOutSFTime) sensor.getEventOut("touchTime"); output.appendText("Got EventOut : touchTime\n"); touchTime.advise(this, new Integer(1)); } // ノードを取り込むのに失敗しました。 catch (InvalidNodeException ne) { add(new TextField("Failed to get node : " + ne)); error = true; } // EventInを取り込むのに失敗しました。 catch (InvalidEventInException ee) { add(new TextField("Failed to get EventIn : " + ee)); error = true; } // EventOutを取り込むのに失敗しました。 catch (InvalidEventOutException ee) { add(new TextField("Failed to get EventOut : " + ee)); error = true; } } public void callback(EventOut who, double when, Object which) { System.out.println("RGBTest : EAI callback().."); Integer whichNum = (Integer) which; if (whichNum.intValue() == 1) { double val = touchTime.getValue(); output.appendText("Got time " + val + " at time " + when + "\n"); } if (whichNum.intValue() == 2) { float[] val = outputColor.getValue(); output.appendText("Got color " + val[0] + ", " + val[1] + ", " + val[2] + " at time " + when + "\n"); } } public boolean action(Event event, Object what) { if (error) { showStatus("problems! Had an error during initialization"); return true; } // ボタンが押されたときの設定をしております。 if (event.target instanceof Button) { Button b = (Button) event.target; if(b.getLabel() == "Red") { showStatus ("Red!"); // ステータスバーに表示する float[] val = new float[3]; val[0] = 0.8f; val[1] = 0.2f; val[2] = 0.2f; diffuseColor.setValue(val); } else if(b.getLabel() == "Green") { showStatus("Green!"); float[] val = new float[3]; val[0] = 0.2f; val[1] = 0.8f; val[2] = 0.2f; diffuseColor.setValue(val); } else if(b.getLabel() == "Blue") { showStatus("Blue!"); float[] val = new float[3]; val[0] = 0.2f; val[1] = 0.2f; val[2] = 0.8f; diffuseColor.setValue(val); } } return true; } }