📄 drag.java
字号:
package Java3DApplet;import com.sun.j3d.utils.behaviors.mouse.MouseRotate;import com.sun.j3d.utils.behaviors.mouse.MouseZoom;import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;import java.applet.*;import java.awt.*;import java.io.*;import java.net.*;import java.awt.BorderLayout;import java.awt.event.*;import javax.media.j3d.*;import javax.vecmath.*;import com.sun.j3d.utils.universe.*;/** * Class Drag * * Author Jim Argabright * Date 11/24/97 */public class Drag extends Applet { boolean appletFlag = true; /** * Sets the "applet" flag which is used to determine * whether or not this program is running as an applet. */ public void setAppletFlag(boolean flag) { appletFlag = flag; } /** * Create the scenegraph for this program. */ public BranchGroup createSceneGraph() { // Define colors Color3f white = new Color3f(1.0f, 1.0f, 1.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f red = new Color3f(0.80f, 0.20f, 0.2f); Color3f ambientRed = new Color3f(0.2f, 0.05f, 0.0f); Color3f ambient = new Color3f(0.2f, 0.2f, 0.2f); Color3f diffuse = new Color3f(0.7f, 0.7f, 0.7f); Color3f specular = new Color3f(0.7f, 0.7f, 0.7f); Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f); // Create the branch group BranchGroup branchGroup = new BranchGroup(); // 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); branchGroup.addChild(objScale); // Create the bounding leaf node BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); BoundingLeaf boundingLeaf = new BoundingLeaf(bounds); objScale.addChild(boundingLeaf); // Set up the background Background bg = new Background(bgColor); bg.setApplicationBounds(bounds); objScale.addChild(bg); // Create the ambient light AmbientLight ambLight = new AmbientLight(white); ambLight.setInfluencingBounds(bounds); objScale.addChild(ambLight); // Create the directional light Vector3f dir = new Vector3f(-1.0f, -1.0f, -1.0f); DirectionalLight dirLight = new DirectionalLight(white, dir); dirLight.setInfluencingBounds(bounds); objScale.addChild(dirLight); // Create the red appearance node Material redMaterial = new Material(ambientRed, black, red, specular, 75.0f); redMaterial.setLightingEnable(true); Appearance redAppearance = new Appearance(); redAppearance.setMaterial(redMaterial); // Create the white appearance node Material whiteMaterial = new Material(ambient, black, diffuse, specular, 75.0f); whiteMaterial.setLightingEnable(true); Appearance whiteAppearance = new Appearance(); whiteAppearance.setMaterial(whiteMaterial); // Create the transform node TransformGroup transformGroup = new TransformGroup(); transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); transformGroup.addChild(new Cube(redAppearance).getChild()); transformGroup.addChild(new Corners(whiteAppearance).getChild()); objScale.addChild(transformGroup); // Create the drag behavior node MouseRotate behavior = new MouseRotate(); behavior.setTransformGroup(transformGroup); transformGroup.addChild(behavior); behavior.setSchedulingBounds(bounds); // Create the zoom behavior node MouseZoom behavior2 = new MouseZoom(); behavior2.setTransformGroup(transformGroup); transformGroup.addChild(behavior2); behavior2.setSchedulingBounds(bounds); // Create the zoom behavior node MouseTranslate behavior3 = new MouseTranslate(); behavior3.setTransformGroup(transformGroup); transformGroup.addChild(behavior3); behavior3.setSchedulingBounds(bounds); // Let Java 3D perform optimizations on this scene graph. branchGroup.compile(); return branchGroup; } /** * Calls the various methods necessary to initialize the * program. */ public void init() { // Set the layout manager setLayout(new BorderLayout()); // Create the 3D canvas Canvas3D canvas = new Canvas3D(null); add("Center", canvas); // Create the scene branch graph BranchGroup scene = createSceneGraph(); // Create the Universe, the Locale, and the view branch graph SimpleUniverse u = new SimpleUniverse(canvas); // This will move the ViewPlatform back a bit so the // objects in the scene can be viewed. u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } /** * Inner class used to "kill" the window when running as * an application. */ static class killAdapter extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } } /** * Used when running as an application. */ public static void main(String[] args) { Drag drag = new Drag(); drag.setAppletFlag(false); drag.init(); Frame frame = new Frame("Drag the mouse in the window"); frame.setSize(640, 480); frame.add("Center", drag); frame.addWindowListener(new killAdapter()); frame.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -