⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 show.java

📁 里面是五个关于java3D的源代码
💻 JAVA
字号:
import  java.io.RandomAccessFile;
import  java.io.IOException;
import  java.applet.Applet;
import  java.awt.*;
import  java.awt.event.*;
import  javax.media.j3d.*;
import  javax.vecmath.*;
import  java.util.Hashtable;
import  java.util.Properties;

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  com.sun.j3d.utils.geometry.ColorCube;

import java.awt.GraphicsConfiguration; 
import com.sun.j3d.utils.universe.*;



public class Show extends Applet
{
    Canvas3D            canvas          = null;
    VirtualUniverse     universe        = null;
    Locale              locale          = null;
    PhysicalBody        physicalBody    = null;
    PhysicalEnvironment physicalEnv     = null;
    View                view            = null;
    ViewPlatform        vp              = null;
    TransformGroup      vpTrans         = null;
    BranchGroup         sceneRoot       = null;
	float				scalingFactor	= 1.0f;


    static class killAdapter extends WindowAdapter
    {
        public void windowClosing( WindowEvent event )
        {
            System.exit( 0 );
        }
    }


    public static void main( String[] args )
    {
    	try
    	{
    		float	scale = 1.0f;			//调整大小

    		if ( args.length > 1 )
    		{
    			scale = Float.valueOf( args[1] ).floatValue();
    		}

	        Show     show = new Show( args[0], scale );
	        show.init();

	        Frame frame = new Frame();
	        frame.setSize( 500, 400 );
	        frame.add( "Center", show );
	        frame.addWindowListener( new killAdapter() );
	        frame.setVisible( true );
	    }
	    catch ( Throwable e )
	    {
	        System.err.println( "** " + e.getMessage() );
	    }
    }



    public Show( String filename, float scale )
    {
        //
        // Create a canvas that Java3D can render into and
        // add it to the applet window.
        //

		scalingFactor	= scale;

        setLayout( new BorderLayout() );

        GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration(); 
			  canvas =new Canvas3D(config); 
        add( "Center", canvas );
        canvas.setSize( 700, 500 );

        //
        // Create the basic structure of a Universe.
        //

        createUniverse();


        //
        // Now create some content for the scene and attach it to
        // the scene graph.
        //

        createScene( filename );
        locale.addBranchGraph( sceneRoot );
    }


    private void createUniverse()
    {
        //
        // Create container of all things Java3D.
        //

        universe    = new VirtualUniverse();

        //
        // Create a HiRes Locale in the universe at ( 0, 0, 0 ).
        //

        locale      = new Locale( universe );


        //
        // Create an appropriate view.
        //

        createView();
    }


    private void createView()
    {
        //
        // Create a default representation of the user's head and
        // the environment they inhabit.
        //

        physicalBody    = new PhysicalBody();
        physicalEnv     = new PhysicalEnvironment();


        //
        // Create a View which contains all the information needed to
        // render a view of the world, including the canvas to render
        // onto.
        //

        view    = new View();
        view.addCanvas3D( canvas );
        view.setPhysicalBody( physicalBody );
        view.setPhysicalEnvironment( physicalEnv );


        //
        // Now create a ViewPlatform and a home for it in the
        // scene graph.  Transform the view platform 4 metres
        // along the z axis, "out of the screen".
        //

        BranchGroup     vpRoot  = new BranchGroup();
        Transform3D     t       = new Transform3D();

        t.set( new Vector3f( 0.0f, 0.0f, 5.0f ) );

        TransformGroup  vpTrans = new TransformGroup( t );

        vp = new ViewPlatform();
        vpTrans.addChild( vp );
        vpRoot.addChild( vpTrans );

        view.attachViewPlatform( vp );


        //
        // Now that we have a view platform, attach it at the
        // top level of the locale.
        //

        locale.addBranchGraph( vpRoot );
    }


    private BranchGroup createScene( String filename )
    {
        //
        // Create a home for the scene in the scene graph.
        //

        sceneRoot   = new BranchGroup();


        //
        // Add a background.
        //

	    BoundingSphere  bounds  =
	                        new BoundingSphere( new Point3d( 0.0, 0.0, 0.0 ), 100.0 );

	    Background      bg      = new Background( 0.1f, 0.4f, 0.7f );
    	bg.setApplicationBounds( bounds );
        sceneRoot.addChild( bg );


        //
        // Add some fog.
        //
/*
        LinearFog   fog = new LinearFog( 1.0f, 1.0f, 1.0f, 0.0f, 10.0f );
        fog.setInfluencingBounds( bounds );
        sceneRoot.addChild( fog );
*/

        //
        // Add a light.
        //
/*
        SpotLight   spot = new SpotLight( new Color3f( 1.0f, 1.0f, 1.0f ),
                                          new Point3f( 5.0f, 5.0f, 5.0f ),
                                          new Point3f( 1.0f, 1.0f, 1.0f ),
                                          new Vector3f( 0.5f, 0.5f, 0.5f ),
                                          (float)Math.PI / 2, (float)Math.PI / 4 );
        spot.setInfluencingBounds( bounds );
        sceneRoot.addChild( spot );
*/

 /*       DirectionalLight dl = new DirectionalLight( new Color3f( 0.3f, 0.4f, 0.4f ),
                                                  new Vector3f( -6.0f, -2.0f, -1.0f ) );
        dl.setInfluencingBounds( bounds );
        sceneRoot.addChild( dl );

        Vector3f direction = new Vector3f( 4.0f,-7.0f, -12.0f );
        direction.normalize();
        dl = new DirectionalLight( new Color3f( 1.0f, 0.0f, 0.0f ),
                                   direction );
        dl.setInfluencingBounds( bounds );
        sceneRoot.addChild( dl );

		Color3f ambientColor = new Color3f(0.2f, 0.2f, 0.2f);		// Second, define the ambient light, and insert it in the branch		AmbientLight ambientLight = new AmbientLight(ambientColor);		ambientLight.setInfluencingBounds(bounds);		sceneRoot.addChild(ambientLight);
*/
		
		// Set up the global lights    // First, define the color of the lights    Color3f light1Color = new Color3f(0.0f, 1.0f,1.0f);    Vector3f light1Direction  = new Vector3f(4.0f, -7.0f, -12.0f);    Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);    Vector3f light2Direction  = new Vector3f(-6.0f, -2.0f, -1.0f);    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);    // Second, define the ambient light, and insert it in the branch    AmbientLight ambientLight = new AmbientLight(ambientColor);    ambientLight.setInfluencingBounds(bounds);    sceneRoot.addChild(ambientLight);    // Lastly, define the directional lights and insert it    DirectionalLight light1      = new DirectionalLight(light1Color, light1Direction);    light1.setInfluencingBounds(bounds);    sceneRoot.addChild(light1);    DirectionalLight light2      = new DirectionalLight(light2Color, light2Direction);    light2.setInfluencingBounds(bounds);    sceneRoot.addChild(light2);
        //
        // Create a cube.
        //

        //sceneRoot.addChild( new ColorCube().getShape() );

        try
        {
        	Transform3D	transform = new Transform3D();
        	transform.setIdentity();
        	transform.setScale( scalingFactor );
	        TransformGroup worldTrans   = new TransformGroup( transform );

        	worldTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	        worldTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	        sceneRoot.addChild( worldTrans );


            //
	        // Create the drag behavior node
	        //

	        MouseRotate behavior = new MouseRotate( worldTrans );
	        behavior.setSchedulingBounds( bounds );
	        sceneRoot.addChild( behavior );


            //
        	// Create the zoom behavior node
            //

        	MouseZoom behavior2 = new MouseZoom( worldTrans );
        	behavior2.setSchedulingBounds( bounds );
	        sceneRoot.addChild( behavior2 );


        	//
        	// Create the translate behavior node
        	//

        	MouseTranslate behavior3 = new MouseTranslate( worldTrans );
        	behavior3.setSchedulingBounds( bounds );
	        sceneRoot.addChild( behavior3 );

			System.out.println( "Loading..." );
            Properties  properties  = new Properties();
            Load3DS   l           = new Load3DS( filename, this,
                                                     new Hashtable(), new Hashtable(),
                                                     properties );

            System.out.println( "# Polygons = " + properties.get( "polygons" ) );
            System.out.println( "# Vertices = " + properties.get( "vertices" ) );

            worldTrans.addChild( l.root() );
        }
        catch ( IOException e )
        {
        	System.err.println( "** Exception: " + e.getMessage() );
            e.printStackTrace( System.err );
        }

        return sceneRoot;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -