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

📄 textureimage.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch06.section08;

import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.Box;
import javax.media.j3d.*;
import javax.vecmath.*;

public class TextureImage
    extends Applet {

  private java.net.URL texImage = null;

  private SimpleUniverse u = null;

  public BranchGroup createSceneGraph() {
    //创建根节点
    BranchGroup objRoot = new BranchGroup();
    //创建转换节点
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);

    Appearance app = new Appearance();
    //导入纹理
    Texture tex = new TextureLoader(texImage, this).getTexture();
    //使用纹理
    app.setTexture(tex);
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    app.setTextureAttributes(texAttr);
    //创建几何体
    Box textureCube = new Box(0.4f, 0.4f, 0.4f,
                              Box.GENERATE_TEXTURE_COORDS, app);
    objTrans.addChild(textureCube);

    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);
    BoundingSphere bounds =
        new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    rotator.setSchedulingBounds(bounds);
    objTrans.addChild(rotator);

    objRoot.compile();

    return objRoot;
  }

  public TextureImage() {
  }

  public TextureImage(java.net.URL url) {
    texImage = url;
  }

  public void init() {
    setLayout(new BorderLayout());
    GraphicsConfiguration config =
        SimpleUniverse.getPreferredConfiguration();

    Canvas3D c = new Canvas3D(config);
    add("Center", c);

    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(c);

    u.getViewingPlatform().setNominalViewingTransform();

    u.addBranchGraph(scene);
  }

  public void destroy() {
    u.removeAllLocales();
  }

  public static void main(String[] args) {
    java.net.URL url = null;
    try {
      url = new java.net.URL("file:c:/stone.jpg");
    }
    catch (java.net.MalformedURLException ex) {
      System.out.println(ex.getMessage());
      System.exit(1);
    }
    new MainFrame(new TextureImage(url), 256, 256);
  }

}

⌨️ 快捷键说明

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