我正在学习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();
           }
       }
   }}}


感谢您的时间。

最佳答案

第二次尝试:将以下代码行从您的线程移动到applet的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的声誉,因为我尚未注册。我什么时候注册我不知道。

10-05 18:36