展开全部
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import javax.media.j3d.*;
import javax.vecmath.*;
/**
* PickWorld creates spheres, cylinders, and cones of different resolutions
* and colors. You can pick each one and drag/zoom them around.
* The setup comes from TickTockPicking.
*/
public class World extends Applet {
public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
TransformGroup objScale = new TransformGroup();
objRoot.addChild(objScale);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
Color3f bgColor = new Color3f(0.05f, 0.05f, 0.4f);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
//灯光62616964757a686964616fe4b893e5b19e31333264643066
Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
//灯光
AmbientLight aLgt = new AmbientLight(alColor);
aLgt.setInfluencingBounds(bounds);
DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
lgt1.setInfluencingBounds(bounds);
objRoot.addChild(aLgt);
objRoot.addChild(lgt1);
Appearance app = new Appearance();
//贴图
TextureLoader tex = new TextureLoader("D:\\我的文档\\NetBeansProjects\\Test_Java3DApplication\\src\\aa\\earth.jpg", this);
app.setTexture(tex.getTexture());
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Color3f gray = new Color3f(0.4f, 0.4f, 0.4f);
app.setMaterial(new Material(gray, black, gray, white, 1.0f));
Transform3D t = new Transform3D();
t.set(new Vector3d(0.0, 0.0, -5.0));
TransformGroup objTrans = new TransformGroup(t);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
Sphere s = new Sphere(1.0f, Sphere.GENERATE_NORMALS | Sphere.GENERATE_TEXTURE_COORDS, 100, app);
objTrans.addChild(s);
objScale.addChild(objTrans);
//加入鼠标旋转事件
MouseRotate behavior = new MouseRotate();
behavior.setTransformGroup(objTrans);
objRoot.addChild(behavior);
behavior.setSchedulingBounds(bounds);
//加入鼠标放大事件
MouseZoom behacvior2 = new MouseZoom();
behacvior2.setTransformGroup(objTrans);
objRoot.addChild(behacvior2);
behacvior2.setSchedulingBounds(bounds);
//加入鼠标位移事件
MouseTranslate behacvior3 = new MouseTranslate();
behacvior3.setTransformGroup(objTrans);
objRoot.addChild(behacvior3);
behacvior3.setSchedulingBounds(bounds);
objRoot.compile();
return objRoot;
}
public World() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new World(), 640, 480);
}
}
以上是1和2,earth.jpg这个图片你可以自己找,如果没有,可以问我要,我发给你!
下面是3
public class MyBehavior extends Behavior {
private TransformGroup targetTG;
private Transform3D rotation = new Transform3D();
private double angle = 0.0;
public MyBehavior(TransformGroup targetTG) {
//得到控制对象的引用
this.targetTG = targetTG;
}
@Override
public void initialize() {
//设置唤醒条件为有键按下
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
@Override
public void processStimulus(Enumeration criteria) {
WakeupCriterion wakeup = null;
AWTEvent[] event = null;
//对获得的事件进行解析,并做出响应
wakeup = (WakeupCriterion) criteria.nextElement();
//获得当前的唤醒条件
if (wakeup instanceof WakeupOnAWTEvent) {
event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
KeyEvent keyevent = (KeyEvent) event[0];
//判断是否为左键,并作相应左旋处理
if (keyevent.getKeyCode() == KeyEvent.VK_LEFT) {
//这里是说如果是左键,则做什么
}
if (keyevent.getKeyCode() == KeyEvent.VK_RIGHT) {
//这里是说如果是右键,则做什么
}
}
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
}
下面是4设置背景图片
TextureLoader myloader=new TextureLoader("bg.jpg",this);
ImageComponent2D myImage=myLoader.getImage();
Background bg=new Background();
bg.setImage(myImage);
1,2,4都没有问题,至于3,你需要调试,因为牵扯到碰撞检测,而且,不知道你需要什么效果,是不是向googleEarth那样,你自己先调吧,不行的话,我再帮你调~
已赞过
已踩过<
你对这个回答的评价是?
评论
收起