⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 viewingplatform.java

📁 JAVA3D矩陈的相关类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**     * Returns a reference to the "bottom most" transform in the     * MultiTransformGroup that is above the ViewPlatform node.     *     * @return The TransformGroup that is immediately above the     *  ViewPlatform object.     */    public TransformGroup getViewPlatformTransform() {        return mtg.getTransformGroup(mtg.getNumTransforms() - 1);    }    /**     * Sets the nominal viewing distance in the ViewPlatform transform based     * on the current field of view.  If the ViewAttachPolicy is not the     * default of View.NOMINAL_HEAD, then this method has no effect.<p>     *      * The ViewPlatform is moved back along Z so that objects at the origin     * spanning the normalized X range of -1.0 to +1.0 can be fully viewed     * across the width of the window.  This is done by setting a translation     * of 1/(tan(fieldOfView/2)) in the ViewPlatform transform.<p>     *     * If there is no Viewer object associated with this ViewingPlatform     * object the default field of view of PI/4.0 is used.<p>     *      * NOTE: Support for multiple Viewer objects is not available.  If     * multiple viewers are attached to this ViewingPlatform than a     * RuntimeException will be thrown.     */    public void setNominalViewingTransform() {	if (viewPlatform.getViewAttachPolicy() == View.NOMINAL_HEAD) {	    double fieldOfView;	    if (viewerList.size() == 0) {		// No Viewer associated with this ViewingPlatform, so use the		// default field of view value to move the ViewingPlatform.		fieldOfView = Math.PI/4.0;	    }	    else {		if (viewerList.size() > 1) {		    throw new RuntimeException			(J3dUtilsI18N.getString("ViewingPlatform0"));		}		Viewer viewer = (Viewer)viewerList.keys().nextElement();		View view = viewer.getView();		fieldOfView = view.getFieldOfView();	    }	    Transform3D t3d = new Transform3D();	    double viewDistance = 1.0/Math.tan(fieldOfView/2.0);	    t3d.set(new Vector3d(0.0, 0.0, viewDistance));	    getViewPlatformTransform().setTransform(t3d);	}    }    /**     * Returns the avatarRoot child number of the ViewerAvatar object.     * All the children of the avatarRoot are compared with the passed     * in ViewerAvatar.  If a match is found, the index is returned.     *     * @param avatar The ViewerAvatar object to look for in the avatarRoot's     *  child nodes.     * @return The index of the child that corresponds to the ViewerAvatar.     *  If the avatarRoot does not contain the ViewerAvatar -1 is returned.     */    private int findAvatarChild(ViewerAvatar avatar) {        // Search the avatarRoot for the ViewerAvatar associated with        // with the Viewer object        for (int i = 0; i < avatarRoot.numChildren(); i++) {            if (((ViewerAvatar)avatarRoot.getChild(i)) == avatar)                return i;        }        // Should never get here.        System.err.println("ViewingPlatform.findAvatarChild:Child not found.");        return -1;    }    /**     * Adds the ViewerAvatar to the scene graph.  An avatar (geometry)     * can be associated with a Viewer object and displayed by Java 3D.     *     * @param viewer The viewer object to associate with this avatar.     * @param avatar The avatar to add to the scene graph.  Passing in     *  null removes any currently assigned avatar.     */    void setAvatar(Viewer viewer, ViewerAvatar avatar) {        Object oldAvatar = viewerList.get(viewer);        // A position of -1 means the avatar is not a child of the avatarRoot.        int avatarPosition = -1;        // Because "null" cannot be used in a put the avatarRoot object        // is used to signify that there is no ViewerAvatar associated        // with this Viewer.        if (oldAvatar != avatarRoot)            avatarPosition = findAvatarChild((ViewerAvatar)oldAvatar);        // If the avatar is null, will be removing any geometry already present.        if (avatar == null) {            if (avatarPosition != -1) {                avatarRoot.removeChild(avatarPosition);                // Reset hashtable entry - avatarRoot == null.                viewerList.put(viewer, avatarRoot);            }        }        else {            // see if there is an old ViewerAvater to replace            if (avatarPosition != -1)                avatarRoot.setChild(avatar, avatarPosition);            else                avatarRoot.addChild(avatar);            // Update hashtable with new avatar.            viewerList.put(viewer, avatar);        }    }    /**     * When a ViewingPlatform is set by a Viewer, the ViewingPlatform     * needs to be informed, via a call to this method.  This will add     * the Viewer to the ViewingPlatform's viewerList for use when     * things such as the PlatformGeometry are changed and all Viewer     * scene graphs need to be modified.     */    void addViewer(Viewer viewer) {        // Because the viewerList is also used to associate ViewerAvatars        // with Viewer objects a hashtable is used. This routine does not        // check for the presence of a ViewerAvatar but the Viewer still        // needs to be added to the hashtable.  Because "null" cannot be        // used in a put the avatarRoot object is used to signify that there        // is no ViewerAvatar associated with this Viewer.        viewerList.put(viewer, avatarRoot);    }    /*     * Cleanup when Viewer set another ViewingPlatform     */    void removeViewer(Viewer viewer) {	viewerList.remove(viewer);    }        /**     * Adds a new ViewPlatformBehavior to the ViewingPlatform     */    void addViewPlatformBehavior(ViewPlatformBehavior behavior) {	behavior.setViewingPlatform(this); 	if (behaviors == null) { 	    behaviors = new BranchGroup(); 	    behaviors.setCapability(BranchGroup.ALLOW_DETACH);	    behaviors.setCapability(BranchGroup.ALLOW_CHILDREN_READ); 	} 	// otherwise detach the BranchGroup so we can add to it	else {	    behaviors.detach();	}	behaviors.addChild(behavior);	this.addChild(behaviors);    }    /**     * Sets the ViewPlatformBehavior which will operate on the ViewPlatform     * transform (the TransformGroup returned by     * ViewingPlatform.getViewPlatformTransform()). The ViewPlatformBehavior     * may be set after the ViewingPlatform is setLive().     * If a behavior is already present, it will be detached and it's     * setViewingPlatform method will be called with a parameter of null.     * @param behavior The ViewPlatformBehavior to add to the ViewingPlatform.     * null will remove the ViewingPlatform behavior.     * @since Java 3D 1.2.1     */    public void setViewPlatformBehavior(ViewPlatformBehavior behavior) {	if (behaviors != null) {	    removeViewPlatformBehavior((ViewPlatformBehavior)behaviors.getChild(0));	}	if (behavior != null) {	    addViewPlatformBehavior(behavior);	}    }    /**     * Removes the specified ViewPlatformBehavior     */    void removeViewPlatformBehavior(ViewPlatformBehavior behavior) {	// remove from the behaviors branch group	if (behaviors != null) {	    behaviors.detach();	    for (int i = 0; i < behaviors.numChildren(); i++) {		if (behaviors.getChild(i) == behavior) {	            behavior.setViewingPlatform( null );		    behaviors.removeChild(i);		    break;		}	    }	    if (behaviors.numChildren() == 0) behaviors = null;	    else this.addChild(behaviors);	}    }        /**     * Returns the number of ViewPlatformBehaviors on the ViewingPlatform     */    int getViewPlatformBehaviorCount() {	return behaviors.numChildren();    }        /**     * Returns the ViewPlatformBehavior at the specified index     */    ViewPlatformBehavior getViewPlatformBehavior(int index) {	return (ViewPlatformBehavior)behaviors.getChild(index);    }    /**     * Returns the ViewPlatformBehavior     * @return the ViewPlatformBehavior for the ViewingPlatform.     * Returns null if there is no ViewPlatformBehavior set.     * @since Java 3D 1.2.1     */    public ViewPlatformBehavior getViewPlatformBehavior() {	if (behaviors == null) {	    return null;	}	return getViewPlatformBehavior(0);    }        /**     * Returns the Viewers attached to this ViewingPlatform     *     * @return the Viewers attached to this viewing platform     * @since Java 3D 1.3     */    public Viewer[] getViewers() {	if (viewerList.size() == 0) return null;        return (Viewer[])viewerList.keySet().toArray( new Viewer[0] );    }        /**     * Returns the Universe to which this ViewingPlatform is attached     *     * @return the Universe to which this ViewingPlatform is attached     * @since Java 3D 1.3     */    public SimpleUniverse getUniverse() {        return universe;    }        /**     * Sets the Universe to which this ViewingPlatform is attached     *     * @param universe the Universe to which this ViewingPlatform is attached     * @since Java 3D 1.3     */    public void setUniverse( SimpleUniverse universe ) {        this.universe = universe;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -