Java3D消失行为

时间:2013-03-04 18:49:24

标签: java-3d

在我看来,当调整Frame / JPanel / Canvas3D的大小时,Java3D有时会重新绘制场景。我在使用Java3D / JUNG3D的应用程序中遇到问题,如果Frame / JPanel / Canvas3D的水平大小具有一定的大小,整个场景就会消失。我试图简单地解决问题然后发现类似的问题出现在下面的示例代码中:http://www.java3d.org/samples.html(倒数第二个示例,“文本3D示例”)为方便起见,我将在底部粘贴。

步骤:

  1. 安装Java3D
  2. 使用3个Java3D jar构建此示例。
  3. 运行
  4. 将框架水平拉伸至500像素左右,然后一切都消失。收缩它然后又在那里。事情已经消失,垂直伸展,事情再次出现。
  5. 它似乎与Frame / JPanel / Canvas3D宽高比以及它显示的内容的长宽比有关。我猜自己的问题略有不同,因为我正在显示不同宽高比的场景,而潜在的问题是相同的。谁能告诉我如何绕过这个bug /功能,以便在调整窗口大小时场景永远不会消失?

    以下是我迄今为止尝试过的内容并没有成功:

    1. 捕获JPanel调整大小事件,然后强制Canvas3D重绘
    2. 使用双缓冲设置
    3. 我现在还记不起很多其他事情 - 刚从假期回来:)
    4. 代码:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.media.j3d.*;
      import javax.vecmath.*;
      import com.sun.j3d.utils.universe.*;
      import com.sun.j3d.utils.geometry.*;
      
      public class Titles {
      
          public static void main(String[] args) {
              Titles t = new Titles();
              t.setUp();
          }
      
          public void setUp() {
              JFrame jf = new JFrame("Welcome");
              // kill the window on close
              jf.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent winEvent) {
                      System.exit(0);
                  }
              });
              JPanel panel = new JPanel();
              panel.setLayout(new GridLayout(1, 1, 2, 2));
      
              GraphicsConfiguration config = SimpleUniverse
                      .getPreferredConfiguration();
              Canvas3D canvas3D = new Canvas3D(config);
              canvas3D.setSize(360, 160);
              SimpleUniverse universe = new SimpleUniverse(canvas3D);
              BranchGroup group = new BranchGroup();
              addObjects(group);
              addLights(group);
              universe.getViewingPlatform().setNominalViewingTransform();
              universe.addBranchGraph(group);
              panel.add(canvas3D);
              jf.getContentPane().add(panel, BorderLayout.CENTER);
              jf.pack();
              jf.setVisible(true);
          }
      
          public void addLights(BranchGroup group) {
              BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
                      1000.0);
      
              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);
              group.addChild(light1);
      
              // Set up the ambient light
              Color3f ambientColor = new Color3f(.1f, .1f, .1f);
              AmbientLight ambientLightNode = new AmbientLight(ambientColor);
              ambientLightNode.setInfluencingBounds(bounds);
              group.addChild(ambientLightNode);
          }
      
          private void addObjects(BranchGroup group) {
              Font3D f3d = new Font3D(new Font("TestFont", Font.PLAIN, 2),
                      new FontExtrusion());
              Text3D text = new Text3D(f3d, new String("Java3D.org"), new Point3f(-3.5f,
                      -.5f, -4.5f));
      
              text.setString("Java3D.org");
              Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
              Color3f blue = new Color3f(.2f, 0.2f, 0.6f);
              Appearance a = new Appearance();
              Material m = new Material(blue, blue, blue, white, 80.0f);
              m.setLightingEnable(true);
              a.setMaterial(m);
      
              Shape3D sh = new Shape3D();
              sh.setGeometry(text);
              sh.setAppearance(a);
              TransformGroup tg = new TransformGroup();
              Transform3D t3d = new Transform3D();
              Transform3D tDown = new Transform3D();
              Transform3D rot = new Transform3D();
              Vector3f v3f = new Vector3f(-1.6f, -1.35f, -6.5f);
              t3d.setTranslation(v3f);
              rot.rotX(Math.PI / 5);
              t3d.mul(rot);
              v3f = new Vector3f(0, -1.4f, 0f);
              tDown.setTranslation(v3f);
              t3d.mul(tDown);
              tg.setTransform(t3d);
              tg.addChild(sh);
              group.addChild(tg);
      
          }
      }
      

2 个答案:

答案 0 :(得分:0)

尝试在此处设置缓冲区,教程。 http://www.youtube.com/watch?v=vzfnUSLKUDs&list=ECA331A6709F40B79D

答案 1 :(得分:0)

已经过了5年但是试试solution

我尝试过它并且使用了lookAt()函数。