📄 color.java
字号:
import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.media.j3d.Canvas3D;
import javax.vecmath.*;
import java.awt.GraphicsConfiguration;
public class Color extends Applet {
private BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
objRoot.addChild(createObject());
Color3f bgColor = new Color3f(1.0f, 1.0f, 1.0f);
BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,0.0,0.0
),100.0);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
objRoot.compile();
return objRoot;
}
private Group createObject() {
Transform3D t = new Transform3D();
TransformGroup objTrans = new TransformGroup(t);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Appearance ap=new Appearance();
ap.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
ap.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
Material mat = new Material( );
mat.setCapability(Material.ALLOW_COMPONENT_WRITE);
Color3f emissive = new Color3f(0.0f, 1.0f, 0.0f);
Color3f diffuse = new Color3f(0.0f, 0.0f, 1.0f);
mat.setEmissiveColor(emissive);
mat.setDiffuseColor(diffuse);
ap.setMaterial(mat);
PolygonAttributes pa = new PolygonAttributes();
pa.setCullFace(PolygonAttributes.CULL_NONE);
ap.setPolygonAttributes(pa);
ColoringAttributes ca = new ColoringAttributes( );
ca.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);
ap.setColoringAttributes( ca);
Alpha alpha = new Alpha(-1,
Alpha.INCREASING_ENABLE|Alpha.DECREASING_ENABLE,
0, 0, 8000, 0, 0, 8000, 0, 0);
ColorInterpolator ci = new ColorInterpolator(alpha, mat,
new Color3f(1.0f,0.0f, 1.0f), new Color3f(0.0f,
0.0f, 1.0f));
BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,0.0,0.0
),100.0);
ci.setSchedulingBounds(bounds);
Shape3D shape = new Shape3D();
shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
shape.setAppearance(ap);
objTrans.addChild(shape);
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0, 4000, 0, 0, 0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objTrans, yAxis,
0.0f, (float) Math.PI*2.0f);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
return objTrans;
}
public Color() {
setLayout(new BorderLayout());
GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse();
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new Color(), 540,361);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -