2

我正在学习 java3D 1.5.2 并且已经完成了一些 ColorCube 测试。我完成的最后一个 java3D 小程序是大约 2000 个 ColorCube 旋转,在它们自身和整个宇宙中平移。立方体被生成为变换分支组的叶节点数组

图片: 在此处输入图像描述

我的问题是:有时它显示得很好,没有任何问题,但有时窗口是白色的,在我调整窗口大小或使其全屏之前什么都没有显示。

问题:你能给我一些关于这个问题的提示吗?我没有使用任何 repaint() 或类似的方法。Java3D 线程正在绘制我假设的全部内容。我怎样才能克服这个绘图更新问题?

这是一个简短的伪代码:

class myclass extends Applet
{
    public void init()
        {
           //some init here
        }

    public void start()
        {
           //i make my thread start here
           mythread.start();
        }

    public void stop()
        {
           //...i make my thread pause here
        }

    public void destroy()
        {
           // Here i release the thread
        }

    public void paint()
        {
           //nothing here. its empty
        }

    public class my_thread extends Thread
        {
            public void run()
                 {
                        //here i use the java3D to initialize java3D things
                        while(working)
                           {
                               // here i change 2000 colorcubes' displacement and rotation
                           }
                 }
        } 
}

这是我提到的 java3D 的一些初始化:

            GraphicsConfiguration GC_me=SimpleUniverse.getPreferredConfiguration();
            Canvas3D Canvas_me=new Canvas3D(GC_me);
            SimpleUniverse Space_me = new SimpleUniverse(Canvas_me);

谢谢。

这是一个完整的代码:

package fiziksel_model;


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

import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.*;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;


public class yabanci extends Applet{

/**
* Version 1
*/


private static final long serialVersionUID = 1L;



public drawer_thread palette=new drawer_thread();
int i;
public void init()
{
   System.out.println(" started! ");



}
//Geometry geom;
public void start()
{
   System.out.println(" continuing...");


   palette.start();


}
public void stop()
{
   System.out.println(" stopped! ");

}
public void destroy()
{
   System.out.println(" destroyed!");

}
public void paint(Graphics g)
{

           System.out.println(" painted! ");// does not print anything.
}


public class drawer_thread extends Thread
{

   public void run()
   {
       Vector3f temp_vect=new Vector3f();
       ColorCube color_box=new ColorCube(0.2f);

       GraphicsConfiguration GC_me=SimpleUniverse.getPreferredConfiguration();
       Canvas3D Canvas_me=new Canvas3D(GC_me);
       SimpleUniverse my_space = new SimpleUniverse(Canvas_me);

       BranchGroup cube_barrage = new BranchGroup();
       Transform3D rotator_element=new Transform3D();
       Transform3D temp_rotator=new Transform3D();


       cube_barrage.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
       cube_barrage.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
       cube_barrage.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
       cube_barrage.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
       setLayout(new BorderLayout());




       TransformGroup rotator1=new TransformGroup(rotator_element);
       TransformGroup rotator2=new TransformGroup(rotator_element);
       TransformGroup rotator3=new TransformGroup(rotator_element);
       TransformGroup rotator4=new TransformGroup(rotator_element);

       Vector3f translation[]=new Vector3f[4000];
       TransformGroup core1[]=new TransformGroup[4000];
       TransformGroup core2[]=new TransformGroup[4000];
       TransformGroup core3[]=new TransformGroup[4000];
       Transform3D core_move1[]=new Transform3D[4000];
       Transform3D core_move2[]=new Transform3D[4000];
       Transform3D core_move3[]=new Transform3D[4000];
       for(int j=0;j<4000;j++)
       {
       translation[j]=new Vector3f();
       core1[j]=new TransformGroup();core2[j]=new TransformGroup();
       core_move1[j]=new Transform3D();
       core_move2[j]=new Transform3D();    
       core_move3[j]=new Transform3D();
       }

       for(int j=0;j<4000;j++)
       {
       translation[j]=new Vector3f();
       translation[j].x=(float) (j/10.0);
       translation[j].y=(float)(-0.5+Math.random()*1.0);
       translation[j].z=(float)(-0.5+Math.random()*1.0);

       core_move1[j].setTranslation(translation[j]);
       core_move2[j].setTranslation(translation[j]);
       core_move3[j].setTranslation(translation[j]);

       core1[j]=new TransformGroup(core_move1[j]);
       core2[j]=new TransformGroup(core_move1[j]);
       core3[j]=new TransformGroup(core_move1[j]);
       core1[j].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
       core1[j].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
       core2[j].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
       core2[j].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

       core3[j].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
       core3[j].setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
       }






       rotator1.addChild(rotator2);
       rotator2.addChild(rotator3);rotator3.addChild(rotator4);
       rotator1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
       rotator1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
       rotator2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
       rotator2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
       rotator3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
       rotator3.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
       rotator4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
       rotator4.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);




       for(int j=0;j<4000;j++)
       {
            rotator4.addChild(core1[j]);
            core1[j].addChild(core2[j]);
            core2[j].addChild(core3[j]);
            core3[j].addChild(new ColorCube(0.1));
       }


       cube_barrage.addChild(rotator1);


       cube_barrage.compile();
       my_space.addBranchGraph(cube_barrage);
       add("Center", Canvas_me);
       //Canvas_me.validate();

       my_space.getViewingPlatform().setNominalViewingTransform();
       float ran[]=new float [4000];
       float ran2[]=new float [4000];
       Vector3f temp_vector=new Vector3f();
       for(int j=0;j<4000;j++)
       {
           ran[j]=(float) Math.random();
           ran2[j]=(float) Math.random();
       }

       for(i=0;i<10000;i++)
       {



           rotator1.getTransform(temp_rotator);
           temp_rotator.setScale(0.1+0.05*Math.abs(Math.sin(i*0.03)));
           rotator1.setTransform(temp_rotator);

           rotator2.getTransform(temp_rotator);
           temp_rotator.rotX(i*0.03);
           rotator2.setTransform(temp_rotator);

           rotator3.getTransform(temp_rotator);
           temp_rotator.rotY(i*0.03);
           rotator3.setTransform(temp_rotator);

           rotator4.getTransform(temp_rotator);
           temp_rotator.rotZ(i*0.03);
           rotator4.setTransform(temp_rotator);


           for(int j=0;j<4000;j++)
           {
               core1[j].getTransform(temp_rotator);
               temp_rotator.get(temp_vector);
               temp_vector.x+=(float) (-0.5+Math.random())*0.02;
               temp_vector.y+=(float) (-0.5+Math.random())*0.02;
               temp_vector.z+=(float) (-0.5+Math.random())*0.02;

               temp_rotator.setTranslation(temp_vector);
               core1[j].setTransform(temp_rotator);

               core2[j].getTransform(temp_rotator);


               temp_rotator.rotZ(i*0.03+0.01*ran[j]*i);

               core2[j].setTransform(temp_rotator);


               core2[j].getTransform(temp_rotator);
               temp_rotator.rotY(i*0.02+0.01*ran2[j]*i);
               core2[j].setTransform(temp_rotator);
           }


           System.out.println(i);
           try {
               sleep(40);
           } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
   }}}

感谢您的时间。

4

2 回答 2

3

第二次尝试:将以下代码行从您的线程移动到小程序的 init 方法中,以确保它们在事件分派线程 (EDT) 中运行。

private SimpleUniverse my_space = null;
public void init()
{
   System.out.println(" started! ");
   GraphicsConfiguration GC_me = SimpleUniverse.getPreferredConfiguration();
   Canvas3D Canvas_me = new Canvas3D(GC_me);
   my_space = new SimpleUniverse(Canvas_me);
   setLayout(new BorderLayout());
   add("Center", Canvas_me);
}

请保存你的 200 声望,因为我没有注册。我什么时候注册我不知道。

于 2012-08-29T16:34:08.777 回答
3

试一试:在您的小程序启动代码中将系统属性“sun.awt.noerasebackground”和“sun.awt.erasebackgroundonresize”设置为 true。

于 2012-08-29T07:24:30.810 回答