`

Java3D-任意角度查看对象

阅读更多
关键词:Java3D对象,任意角度,查看
一个Java3D的鼠标拖动组件的特效,通过鼠标可以随意角度的查看3D对象。
运行效果如下图:



package com.zakisoft.hw;

/*
 *	@(#)ModelClipTest.java 1.10 02/10/21 13:45:01
 *
 * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that Software is not designed,licensed or intended
 * for use in the design, construction, operation or maintenance of
 * any nuclear facility.
 */

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.media.j3d.ModelClip;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector4d;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.universe.SimpleUniverse;

/**
 * ModelClipTest draws a cylinder and creates two clip planes to see the
 * interior of the cylinder.
 */
public class ModelClipTest extends Applet {

	private static final long serialVersionUID = 9087471750590410314L;
	private SimpleUniverse u = null;

	public BranchGroup createSceneGraph() {
		// Create the root of the branch graph
		BranchGroup objRoot = new BranchGroup();

		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
				100.0);

		// Create a Transformgroup to scale all objects so they
		// appear in the scene.
		TransformGroup objScale = new TransformGroup();
		Transform3D t3d = new Transform3D();
		t3d.setScale(0.4);
		objScale.setTransform(t3d);
		objRoot.addChild(objScale);

		// This Transformgroup is used by the mouse manipulators to
		// move the CYlinder.
		TransformGroup objTrans = new TransformGroup();
		objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		objScale.addChild(objTrans);

		// Create Model Clip
		ModelClip mc = new ModelClip();
		boolean enables[] = { false, false, false, false, false, false };
		Vector4d eqn1 = new Vector4d(0.0, 1.0, 0.0, 0.0);
		Vector4d eqn2 = new Vector4d(1.0, 1.0, 0.0, 0.0);
		mc.setEnables(enables);
		mc.setPlane(1, eqn1);
		mc.setPlane(2, eqn2);
		mc.setEnable(1, true);
		mc.setEnable(2, true);
		mc.setInfluencingBounds(bounds);
		objTrans.addChild(mc);

		// Create a cylinder
		PolygonAttributes attr = new PolygonAttributes();
		attr.setCullFace(PolygonAttributes.CULL_NONE);
		Appearance ap = new Appearance();
		Material mat = new Material();
		mat.setLightingEnable(true);
		ap.setMaterial(mat);
		ap.setPolygonAttributes(attr);

		Cylinder CylinderObj = new Cylinder(1.0f, 2.0f, ap);
		objTrans.addChild(CylinderObj);

		// Create the rotate behavior node
		MouseRotate behavior = new MouseRotate(objTrans);
		objTrans.addChild(behavior);
		behavior.setSchedulingBounds(bounds);

		// Create the zoom behavior node
		MouseZoom behavior2 = new MouseZoom(objTrans);
		objTrans.addChild(behavior2);
		behavior2.setSchedulingBounds(bounds);

		// Shine it with two colored lights.
		Color3f lColor1 = new Color3f(0.5f, 0.0f, 0.5f);
		Color3f lColor2 = new Color3f(0.7f, 0.7f, 0.0f);
		Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, 1.0f);
		Vector3f lDir2 = new Vector3f(0.0f, 0.0f, -1.0f);
		DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
		DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
		lgt1.setInfluencingBounds(bounds);
		lgt2.setInfluencingBounds(bounds);
		objScale.addChild(lgt1);
		objScale.addChild(lgt2);

		// Let Java 3D perform optimizations on this scene graph.
		objRoot.compile();

		return objRoot;
	}

	public ModelClipTest() {
	}

	public void init() {
		setLayout(new BorderLayout());
		GraphicsConfiguration config = SimpleUniverse
				.getPreferredConfiguration();

		Canvas3D c = new Canvas3D(config);
		add("Center", c);

		// Create a simple scene and attach it to the virtual universe
		BranchGroup scene = createSceneGraph();
		u = new SimpleUniverse(c);

		// This will move the ViewPlatform back a bit so the
		// objects in the scene can be viewed.
		u.getViewingPlatform().setNominalViewingTransform();

		u.addBranchGraph(scene);
	}

	public void destroy() {
		u.cleanup();
	}

	public static void main(String argv[]) {

		new MainFrame(new ModelClipTest(), 500, 500);
	}
}


文章地址:http://javapub.iteye.com/blog/760468
分享到:
评论

相关推荐

    java3d安装包和api文档及源代码

    1. **Scene Graph**:场景图是Java3D用来组织和管理3D对象的主要数据结构。它类似于一棵树,其中每个节点代表一个3D对象,如几何形状、变换或光照。通过操作场景图,开发者可以控制整个3D场景的布局和行为。 2. **...

    Tra.rar_Java3d图形旋转_java 3d_画图_画图 java_画图 旋转

    3. **3D旋转**:Java 3D提供了旋转操作,允许用户以任意轴和角度旋转3D对象。这通常通过`Transform3D`类来实现,该类可以对坐标系统进行旋转、平移和缩放操作。通过设置`Transform3D`的旋转参数,可以改变对象的显示...

    java 3d universe

    通过设置Transform3D对象的旋转角度,并将其应用到Box对象上,我们可以实现盒子围绕任意轴的旋转。 此外,光照和纹理也是增强三维视觉效果的关键。Java 3D支持多种光源类型,如点光源、平行光和聚光灯。开发者可以...

    基于JAVA3D的房屋建模

    本文主要介绍了JAVA和虚拟现实技术在房屋网络销售中的应用意义,并通过面向对象语言JAVA3D实现了简单的房屋建模。以下是相关知识点的总结: 一、 虚拟现实(Virtual Reality,VR)技术 虚拟现实技术是一种逼真地...

    Activity3D旋转动画效果

    2. **`ObjectAnimator`**: `ObjectAnimator`可以更灵活地控制动画,它基于属性动画系统,能够对对象的任意属性进行动画化。若要实现3D旋转,可以使用`ObjectAnimator.ofFloat(view, "rotationX", startAngle, ...

    android 3D动画效果

    它允许对象沿着任意轴进行旋转,提供了一种在2D屏幕上模拟3D空间变换的方式。要创建一个`Rotate3DAnimation`,我们需要指定旋转的中心点、旋转的角度、旋转轴以及是否重复等参数。 在XML中定义3D旋转动画,可以使用...

    The Design and Implementation Of An Object-Oriented

    作者讨论了VTK的核心特征,包括面向对象模型在图形和可视化中的应用、系统执行同步的方法、数据表示方案的概述、C++的角色、跨PC和Unix系统的可移植性问题,以及如何自动将C++类库包装成解释语言(如Java和Tcl)的...

    Android 旋转的地球源码-IT计算机-毕业设计.zip

    属性动画系统允许开发者改变对象的任意属性并随着时间推移产生动画效果。在这个例子中,开发者可能设置了地球围绕自己的轴心进行平滑旋转的动画,这需要对时间、速度和角度的精确控制。 此外,源码中还可能包含了UI...

    android---image--functon.zip_Android 动画_android_android image

    而属性动画系统则更为强大,它可以改变对象的任意属性,并且支持3D效果和物理模拟。 帧动画的实现主要通过`AnimationDrawable`类。你需要在XML资源文件中定义一系列图片,并在代码中设置为某个ImageView的背景。...

    android 旋转动画源码.rar

    属性动画系统自API 11(Android 3.0 Honeycomb)引入,功能强大,可以对对象的任意属性进行动画处理;而视图动画系统较早,适用于API 1及更高版本,它实际上并不改变对象的状态,只是在视觉上产生动画效果。 1. **...

    Gladiator-visible-fields

    在2D棋盘上,通常以角色为中心,按照一定的角度(例如45度、90度或任意角度)向外扩展,直到遇到障碍物(如墙壁、地形阻挡等)。这一步涉及到数学中的几何和三角函数,比如余弦和正弦,以确定视线在棋盘格子上的方向...

    安卓各种动画

    属性动画是Android 3.0(API 11)引入的新功能,它允许开发者对任何对象的任意属性进行动画化,不仅仅是View。属性动画提供了更大的灵活性,可以实现更为复杂的效果。 使用属性动画的基本步骤: 1. 创建`...

    Rotations.zip_rotation_旋转

    1. **二维旋转**:在二维平面上,一个对象的旋转通常是围绕其中心点或任意指定点进行的。这种旋转可以用数学矩阵表示,通过乘以一个旋转矩阵来实现。例如,一个顺时针旋转θ度的矩阵是: \[ R(\theta) = \begin{b...

    Transition3d:Android 三D翻转动画,源自APIDemos

    属性动画是Android 3.0(API Level 11)引入的新特性,它可以改变对象的任意属性,并且这些变化会在真实对象上反映出来,而不仅仅是视觉效果。视图动画则是在早期版本中使用的,它主要改变视图的绘制状态,而非实际...

    基于几何和图像的机舱漫游场景建模技术.pdf

    用户通过这些设备在虚拟环境中漫游,能够从任意角度观察环境中的虚拟对象,同时进行物体的规划和操作。 虚拟漫游技术是虚拟现实技术的重要组成部分,它具有沉浸感、交互性和构想性三大特性。沉浸感指的是用户感觉...

Global site tag (gtag.js) - Google Analytics