📄 simplematerial.java
字号:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Label;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class SimpleMaterial extends Applet {
TransformGroup createTG(float x, float y, float z){
Vector3f position = new Vector3f(x, y, z);
Transform3D translate = new Transform3D();
translate.set(position);
TransformGroup trans1 = new TransformGroup(translate);
return trans1;
}
Appearance createMatAppear(Color3f dColor, Color3f sColor, float shine) {
Appearance appear = new Appearance();
Material material = new Material();
material.setDiffuseColor(dColor);
material.setSpecularColor(sColor);
material.setShininess(shine);
appear.setMaterial(material);
return appear;
}
BranchGroup createScene()
{
BranchGroup sceneRoot = new BranchGroup();
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
TransformGroup trans11 = createTG(-0.7f, 0.7f, -0.5f);
sceneRoot.addChild(trans11);
trans11.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1.0f)));
TransformGroup trans12 = createTG(0.0f, 0.7f, -0.5f);
sceneRoot.addChild(trans12);
trans12.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 2.0f)));
TransformGroup trans13 = createTG(0.7f, 0.7f, -0.5f);
sceneRoot.addChild(trans13);
trans13.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 4.0f)));
TransformGroup trans21 = createTG(-0.7f, 0.0f, -0.5f);
sceneRoot.addChild(trans21);
trans21.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 8.0f)));
TransformGroup trans22 = createTG(0.0f, 0.0f, -0.5f);
sceneRoot.addChild(trans22);
trans22.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 16.0f)));
TransformGroup trans23 = createTG(0.7f, 0.0f, -0.5f);
sceneRoot.addChild(trans23);
trans23.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 32.0f)));
TransformGroup trans31 = createTG(-0.7f, -0.7f, -0.5f);
sceneRoot.addChild(trans31);
trans31.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 64.0f)));
TransformGroup trans32 = createTG(0.0f, -0.7f, -0.5f);
sceneRoot.addChild(trans32);
trans32.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 128.0f)));
TransformGroup trans33 = createTG(0.7f, -0.7f, -0.5f);
sceneRoot.addChild(trans33);
trans33.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 128.0f)));
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* TransformGroup trans11 = createTG(-0.7f, 0.7f, -0.5f);
sceneRoot.addChild(trans11);
trans11.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
TransformGroup trans12 = createTG(0.0f, 0.7f, -0.5f);
sceneRoot.addChild(trans12);
trans12.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
TransformGroup trans13 = createTG(0.7f, 0.7f, -0.5f);
sceneRoot.addChild(trans13);
trans13.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
TransformGroup trans21 = createTG(-0.7f, 0.0f, -0.5f);
sceneRoot.addChild(trans21);
trans21.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
TransformGroup trans22 = createTG(0.0f, 0.0f, -0.5f);
sceneRoot.addChild(trans22);
trans22.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
TransformGroup trans23 = createTG(0.7f, 0.0f, -0.5f);
sceneRoot.addChild(trans23);
trans23.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
TransformGroup trans31 = createTG(-0.7f, -0.7f, -0.5f);
sceneRoot.addChild(trans31);
trans31.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
TransformGroup trans32 = createTG(0.0f, -0.7f, -0.5f);
sceneRoot.addChild(trans32);
trans32.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
TransformGroup trans33 = createTG(0.7f, -0.7f, -0.5f);
sceneRoot.addChild(trans33);
trans33.addChild(new Sphere(0.3f, Sphere.GENERATE_NORMALS, 60,
createMatAppear(blue, white, 1000.0f)));
*/
AmbientLight lightA = new AmbientLight();
lightA.setInfluencingBounds(new BoundingSphere());
sceneRoot.addChild(lightA);
DirectionalLight lightD1 = new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere());
Vector3f direction = new Vector3f(0.0f, 0.0f, -1.0f);
direction.normalize();
lightD1.setDirection(direction);
lightD1.setColor(red);
sceneRoot.addChild(lightD1);
PointLight lightP1 = new PointLight();
lightP1.setInfluencingBounds(new BoundingSphere());
Point3f position = new Point3f(0.0f, 0.0f, 1.0f);
lightP1.setPosition(position);
sceneRoot.addChild(lightP1);
Background bg = new Background();
bg.setApplicationBounds(new BoundingSphere());
bg.setColor(white);
sceneRoot.addChild(bg);
return sceneRoot;
}
public SimpleMaterial (){
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
SimpleUniverse u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
//Enable Local Eye Lighting
u.getViewer().getView().setLocalEyeLightingEnable(true);
u.addBranchGraph(createScene());
}
public static void main(String argv[])
{
new MainFrame(new SimpleMaterial(), 400, 400);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -