我正在尝试使用 Java 3D 做一些有趣的事情,我正在通过官方教程学习。 我的问题是:有时它显示得很好,没有任何问题,但有时窗口是白色的,在我调整窗口大小之前什么也没有显示。 我怎样才能克服这个绘图更新问题?
这是全部代码
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
public class Main {
public Main()
{
SimpleUniverse simpleUniverse = new SimpleUniverse();
BranchGroup group = new BranchGroup();
Sphere sphere = new Sphere(0.5f);
Color3f light1Color = new Color3f(0.7f,0.7f,5f);
BoundingSphere bounds = new BoundingSphere(new Point3d(0,0,0),100);
Vector3f light1Direction = new Vector3f(4.0f,-7.0f,-12.0f);
DirectionalLight light1 = new DirectionalLight(light1Color,light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);
Transform3D transform = new Transform3D();
TransformGroup tg = new TransformGroup();
Vector3f pos = new Vector3f(0,0,-5);
transform.setTranslation(pos);
tg.setTransform(transform);
tg.addChild(sphere);
group.addChild(tg);
simpleUniverse.getViewingPlatform().setNominalViewingTransform();
simpleUniverse.addBranchGraph(group);
}
public static void main(String[] args) {
new Main();
}
}
最佳答案
这是重量级渲染组件的常见问题,例如内部用于 Java3D 的 Canvas3D
。
你可以通过调用来避免这种情况
System.setProperty("sun.awt.noerasebackground", "true");
作为我们主要方法的第一行,或者通过启动你的程序
java YourProgram -Dsun.awt.noerasebackground=true
(Websearches for noerasebackground
带来了更多关于细节的信息,但是这个标志应该可以解决白色背景的问题)
关于Java3D画空白窗(不在小程序中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27784310/