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

📄 java3dapplet.java

📁 JAVA网络三维技术3D的设计与实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package Java3DApplet;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.media.j3d.GraphicsConfigTemplate3D;
import javax.vecmath.*;
import com.sun.j3d.utils.image.*;
import java.io.*;
//import java.awt.event.KeyListener;

public class Java3DApplet
    extends Example {
  boolean isStandalone = false;
  BorderLayout borderLayout1 = new BorderLayout();

  //Get a parameter value
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
        (getParameter(key) != null ? getParameter(key) : def);
  }

  //Construct the applet
  public Java3DApplet() {
  }

  //Initialize the applet
  public void init() {
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
public void start()
  {
   Java3DApplet ex = new Java3DApplet();
    ex.initialize(null);
    ex.buildUniverse();
    ex.showFrame();
  }
  //Component initialization
  private void jbInit() throws Exception {
  }

  //Get Applet information
  public String getAppletInfo() {
    return "Applet Information";
  }

  //Get parameter info
  public String[][] getParameterInfo() {
    return null;
  }

  private ExponentialFog fog      = null;
  private Background background   = null;
  private int    moveNodeIndex    = -1;
  RotationInterpolator rotator1   = null;
  RotationInterpolator rotator2   = null;
  PositionInterpolator translator = null;
  RotationInterpolator spinner    = null;
  ColorInterpolator Material      = null;

  private AmbientLight ambient = null;
  private AmbientLight brightAmbient = null;
  private DirectionalLight redDirectional = null;
  private DirectionalLight yellowDirectional = null;
  private PointLight orangePoint = null;

  /////////
   BranchGroup objRoot = new BranchGroup();

   private BackgroundSound  backgroundSound = null;
  //
  //  Build scene
  ///*SimpleUniverse u/*
  public Group buildScene(SimpleUniverse u) {
    // Get the current color
    // Get the current image
    Group Scene1 =new Group();
    Scene1.setCapability(Group.ALLOW_CHILDREN_READ);
    Scene1.setCapability(Group.ALLOW_CHILDREN_WRITE);
    Scene1.setCapability(Group.ALLOW_CHILDREN_EXTEND);

    ImageComponent2D image = imageComponents[currentImage];

    Color3f color = (Color3f) colors[currentColor].value;
    float density = ( (Float) densities[currentDensity].value).floatValue();

    // Get the initial sound volume
    // Turn off the example headlight
    setHeadlightEnable(false);

    // Default to walk navigation
    setNavigationType(Walk);

    // Load the texture images
    TextureLoader texLoader = new TextureLoader( "tree1.jpg", this);
    ImageComponent2D oneImage = texLoader.getImage();
    if ( oneImage == null )
    {
         System.err.println( "Cannot load 'tree1.jpg'" );
    }
      // Preload the sounds
    //
    if ( debug ) System.err.println( "  sounds..." );
    String path = getCurrentDirectory( );
    MediaContainer backgroundMedia  = new MediaContainer(
      path + "canon.wav" );
    backgroundMedia.setCacheEnable( true );


    // Create the scene group


    // BEGIN EXAMPLE TOPIC
    // Create influencing bounds
    BoundingSphere worldBounds = new BoundingSphere(
        new Point3d(0.0, 0.0, 0.0), // Center
        10000.0); // Extent

   // Background sound
             backgroundSound = new BackgroundSound( );
             backgroundSound.setEnable( true );
             backgroundSound.setLoop( Sound.INFINITE_LOOPS );
             backgroundSound.setSoundData( backgroundMedia );
             backgroundSound.setInitialGain( 1.0f );
             backgroundSound.setSchedulingBounds( worldBounds );
             backgroundSound.setCapability( Sound.ALLOW_ENABLE_WRITE );
             backgroundSound.setCapability( Sound.ALLOW_INITIAL_GAIN_WRITE );
             backgroundSound.setCapability( Sound.ALLOW_PAUSE_WRITE);
             backgroundSound.setCapability( Sound.ALLOW_PAUSE_READ);
             Scene1.addChild( backgroundSound );


    // Set the fog color, density, and its influencing bounds
    fog = new ExponentialFog();
    fog.setColor(color);
    fog.setDensity(density);
    fog.setCapability(Fog.ALLOW_COLOR_WRITE);
    fog.setCapability(ExponentialFog.ALLOW_DENSITY_WRITE);
    fog.setInfluencingBounds(worldBounds);
    Scene1.addChild(fog);
    // END EXAMPLE TOPIC

    background = new Background();
    background.setColor(color);
    background.setApplicationBounds(worldBounds);
    background.setCapability(Background.ALLOW_COLOR_WRITE);
    background.setCapability(Background.ALLOW_IMAGE_WRITE);
    background.setImageScaleMode(Background.SCALE_FIT_MAX);
    Scene1.addChild(background);
    //////////



    // Build foreground geometry

    Scene1.addChild(new TowerScene (this));
    Scene1.addChild(new ColumnScene (this));

    ////////
    TransformGroup objScale = new TransformGroup();

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

    TransformGroup vpTrans =
       universe.getViewingPlatform().getViewPlatformTransform();
    Transform3D axisOfTranslation = new Transform3D();
    Alpha transAlpha = new Alpha(-1,
                                 Alpha.INCREASING_ENABLE |
                                 Alpha.DECREASING_ENABLE,
                                 0, 0,
                                 5000, 0, 0,
                                 5000, 0, 0);
    axisOfTranslation.rotY(-Math.PI/2.0);
    translator = new PositionInterpolator(transAlpha,
                                vpTrans,
                                axisOfTranslation,
                                -4.0f, 20.5f);
    translator.setSchedulingBounds(bounds);
    translator.setEnable(false);
    objScale.addChild(translator);
    Scene1.addChild(objScale);

     TransformGroup trans = new TransformGroup();
     trans.setCapability(trans.ALLOW_TRANSFORM_READ);
     trans.setCapability(trans.ALLOW_TRANSFORM_WRITE);

    Alpha spinAlpha = new Alpha( -1, 4000 );
    spinAlpha.setCapability(Alpha.DECREASING_ENABLE);
    spinAlpha.setCapability(Alpha.INCREASING_ENABLE);
    spinner =
        new RotationInterpolator( spinAlpha, trans );
    spinner.setEnable(false);
    spinner.setSchedulingBounds(
            new BoundingSphere( new Point3d( ), 1000.0 ) );
    trans.addChild( spinner );

    Transform3D objTrans = new Transform3D();
    trans.getTransform(objTrans);
    objTrans.setScale( 0.6 );
    trans.setTransform(objTrans);
    trans.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);


    int flags = ObjectFile.RESIZE;
    ObjectFile f = new ObjectFile(flags, (float) (49.0 * Math.PI / 180.0));

    Scene s = null;
    try {
      s = f.load("galleon.obj");
    }
    catch (FileNotFoundException e) {
      System.err.println(e);
      System.exit(1);
    }
    catch (ParsingErrorException e) {
       System.err.println(e);
       System.exit(1);
    }
    catch (IncorrectFormatException e) {
       System.err.println(e);
       System.exit(1);
    }


    trans.addChild(s.getSceneGroup());

    Scene1.addChild(trans);
    // Create influencing bounds

    // General Ambient light
    ambient = new AmbientLight( );
    ambient.setEnable( ambientOnOff );
    ambient.setColor( new Color3f( 0.3f, 0.3f, 0.3f ) );
    ambient.setCapability( AmbientLight.ALLOW_STATE_WRITE );
    ambient.setInfluencingBounds( worldBounds );
    Scene1.addChild( ambient );

    // Bright Ambient light
    brightAmbient = new AmbientLight( );
    brightAmbient.setEnable( brightAmbientOnOff );
    brightAmbient.setColor( new Color3f( 1.0f, 1.0f, 1.0f ) );
    brightAmbient.setCapability( AmbientLight.ALLOW_STATE_WRITE );
    brightAmbient.setInfluencingBounds( worldBounds );
    Scene1.addChild( brightAmbient );

    // Red directional light
    redDirectional = new DirectionalLight( );
    redDirectional.setEnable( redDirectionalOnOff );
    redDirectional.setColor( new Color3f( 1.0f, 0.0f, 0.0f ) );
    redDirectional.setDirection( new Vector3f( 1.0f, -0.5f, -0.5f ) );
    redDirectional.setCapability( AmbientLight.ALLOW_STATE_WRITE );
    redDirectional.setInfluencingBounds( worldBounds );
    Scene1.addChild( redDirectional );

    // Yellow directional light
    yellowDirectional = new DirectionalLight( );
    yellowDirectional.setEnable( yellowDirectionalOnOff );
    yellowDirectional.setColor( new Color3f( 1.0f, 0.8f, 0.0f ) );
    yellowDirectional.setDirection( new Vector3f( -1.0f, 0.5f, 1.0f ) );
    yellowDirectional.setCapability( AmbientLight.ALLOW_STATE_WRITE );
    yellowDirectional.setInfluencingBounds( worldBounds );
    Scene1.addChild( yellowDirectional );

    // Orange point light
    orangePoint = new PointLight( );
    orangePoint.setEnable( orangePointOnOff );
    orangePoint.setColor( new Color3f( 1.0f, 0.5f, 0.0f ) );
    orangePoint.setPosition( new Point3f( 0.0f, 0.5f, 0.0f ) );
    orangePoint.setCapability( AmbientLight.ALLOW_STATE_WRITE );
    orangePoint.setInfluencingBounds( worldBounds );
    Scene1.addChild( orangePoint );


    return Scene1;
  }
 //--------------------------------------------------------------
  //  USER INTERFACE
  //--------------------------------------------------------------

  //
  //  Main
  //
  /*
  public static void main(String[] args) {
    Java3DApplet ex = new Java3DApplet();
    ex.initialize(args);
    ex.buildUniverse();
    ex.showFrame();
  }*/

⌨️ 快捷键说明

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