2012-08-23 114 views
2

我正在學習java3D 1.5.2並已經完成了一些ColorCube測試。我做的最後一個java3D applet大約是2000個ColorCubes旋轉,在自己和整個宇宙中進行翻譯。立方體被作爲變換分支組的一個葉節點陣列生成Java3D在applet中繪製空白窗口

圖片: enter image description here

我的問題是:有時它顯示了良好,沒有任何問題,但有時窗口是白色的,沒有任何顯示,直到我調整該窗口或使其全屏。

問題:你能給我一些關於這個問題的提示嗎?我沒有使用任何重繪()或類似的方法。 Java3D線程正在繪製我假設的整個事情。我如何克服這個繪圖更新問題?

下面是一個簡短pseude代碼:

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(); 
      } 
     } 
    }}} 

謝謝您的時間。

+1

Java控制檯中的輸出是什麼? –

+1

Java控制檯輸出僅寫入「applet stopped」,「applet active」,「applet destroyed」。在控制檯中沒有任何類型的錯誤。 –

+1

我沒有得到任何錯誤。它關於得到一個白色的窗口,而不是獲得一些彩色立方體(這是50%的試驗) –

回答

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的聲望,因爲我沒有註冊。我什麼時候註冊我不知道。

+2

非常酷。有效。註冊時通知我。請。 –

+1

所以,init()實際上是一個初始化:)可能與構造函數結合使用。 –

+0

參見[*初始線程*](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)和[*賞金系統如何工作?*](http:// meta .stackexchange.com/q/16065/163188) – trashgod

3

試試看: 在小程序啓動代碼中將系統屬性「sun.awt.noerasebackground」和「sun.awt.erasebackgroundonresize」設置爲true。

+1

沒有工作。仍然是白色。沒有SecurityException被捕獲。 –

+1

如果沒有更好的答案來,我給你200 :) –