的所有人。 我想使用java 3D模拟热气球,但我不知道如何绘制自定义球体以及如何在球体底部打孔,或者让它像热气球一样。我知道多边形,但我不知道编辑或切片或删除它。
这是我的代码:
package HotAirBallon;
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Sphere;
import javax.swing.Timer;
/**
*
* @author pc
*/
public class HotAirBallon extends Applet{
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
// private Button go = new Button("Go");
private TransformGroup objTrans;
private TransformGroup objCube;
private TransformGroup objtest1;
private TransformGroup objtest2;
private Transform3D trans = new Transform3D();
private float height=0.0f;
private float sign = 1.0f; // going up or down
private Timer timer;
private float xloc=0.0f;
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
objTrans = new TransformGroup();
objCube = new TransformGroup();
objtest1 = new TransformGroup();
objtest2 = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objCube.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objtest1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objtest2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
objRoot.addChild(objCube);
objRoot.addChild(objtest1);
Appearance app = new Appearance();
Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
float shininess = 20.0f;
app.setMaterial(new Material(ambientColour, emissiveColour,
diffuseColour, specularColour, shininess));
// Create a simple shape leaf node, add it to the scene graph.
Sphere sphere = new Sphere(0.6f, Sphere.GENERATE_NORMALS,120,app);
Cylinder cylinder = new Cylinder(0.2f, 0.2f,app);
Cylinder tali1 = new Cylinder(0.01f, 0.6f,app);
Cylinder tali2 = new Cylinder(0.01f, 0.6f,app);
// Appearance ap = new Appearance();
// Color3f col = new Color3f(0.0f, 0.0f, 1.0f);
// ColoringAttributes ca = new ColoringAttributes
// (col, ColoringAttributes.SHADE_GOURAUD);
// ap.setColoringAttributes(ca);
// sphere.setAppearance(ap);
objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objCube = new TransformGroup();
objCube.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objtest1 = new TransformGroup();
objtest1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objtest2 = new TransformGroup();
objtest2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
Transform3D pos2 = new Transform3D();
Transform3D pos3 = new Transform3D();
Transform3D pos4 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,-0.5f,0.0f));
pos2.setTranslation(new Vector3f(0.0f,0.3f,0.0f));
pos3.setTranslation(new Vector3f(0.2f,-0.3f,0.0f));
pos3.setRotation(new AxisAngle4d(1.0,1.0,0.0,Math.PI/5.0)) ;
pos4.setTranslation(new Vector3f(-0.2f,-0.3f,0.0f));
pos4.setRotation(new AxisAngle4d(1.0,-1.0,0.0,Math.PI/5.0)) ;
objTrans.setTransform(pos1);
objTrans.addChild(cylinder);
objtest1.setTransform(pos3);
objtest1.addChild(tali1);
objtest2.setTransform(pos4);
objtest2.addChild(tali2);
objCube.setTransform(pos2);
objCube.addChild(sphere);
objRoot.addChild(objTrans);
objRoot.addChild(objCube);
objRoot.addChild(objtest1);
objRoot.addChild(objtest2);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
addLights(objRoot);
// Color3f light1Color = new Color3f(1.0f, 0.0f, 0.2f);
// Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
// DirectionalLight light1
// = new DirectionalLight(light1Color, light1Direction);
// light1.setInfluencingBounds(bounds);
// objRoot.addChild(light1);
//
// // Set up the ambient light
// Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f);
// AmbientLight ambientLightNode = new AmbientLight(ambientColor);
// ambientLightNode.setInfluencingBounds(bounds);
// objRoot.addChild(ambientLightNode);
return objRoot;
}
protected void addLights(BranchGroup b) {
// Create a bounds for the lights
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
// Set up the global lights
Color3f lightColour1 = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f lightDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
Color3f lightColour2 = new Color3f(1.0f, 1.0f, 1.0f);
Point3f lightPosition2 = new Point3f(3.0f, 3.0f, 3.0f);
Point3f lightAtten2 = new Point3f(0.0f, 0.0f, 1.0f);
Vector3f lightDir2 = new Vector3f(-1.0f, -1.0f, -1.0f);
Color3f ambientColour = new Color3f(0.2f, 0.2f, 0.2f);
AmbientLight ambientLight1 = new AmbientLight(ambientColour);
ambientLight1.setInfluencingBounds(bounds);
DirectionalLight light1 = new DirectionalLight(lightColour1, lightDir1);
light1.setInfluencingBounds(bounds);
PointLight light2 = new PointLight(lightColour2, lightPosition2,
lightAtten2);
light2.setInfluencingBounds(bounds);
b.addChild(ambientLight1);
b.addChild(light1);
//b.addChild(light2);
}
/*
@Override
public void init() {
// TODO start asynchronous download of heavy resources
}
*/
// TODO overwrite start(), stop() and destroy() methods
public HotAirBallon() {
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);
}
}
感谢任何人帮助我...... 问候。