2012-10-02 42 views
1

我使用Java Swing技術製作了一個NetBeans平臺的桌面應用程序。在那個應用程序中,我使用PointArray []對象在3D圖像上進行圖像處理。如何在NetBeans平臺應用程序中更快地顯示Java3D圖像?

當我的應用程序運行在具有Windows 7,圖形卡和良好內存容量(4GB RAM)的PC上時,我的3D圖像在創建並在點擊3DImage按鈕後的5至6秒內顯示。但是當我的應用在Windows XP,低圖形配置PC上運行時,我的3D圖像最多需要三分鐘才能渲染,或者一段時間不顯示圖像。那我該如何解決這個問題呢?

我的代碼如下。

import com.sun.j3d.utils.behaviors.mouse.MouseRotate; 
import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom; 
import com.sun.j3d.utils.universe.SimpleUniverse; 
import java.awt.BorderLayout; 
import java.awt.GraphicsConfiguration; 
import java.awt.image.BufferedImage; 
import javax.media.j3d.Appearance; 
import javax.media.j3d.BoundingSphere; 
import javax.media.j3d.BranchGroup; 
import javax.media.j3d.Canvas3D; 
import javax.media.j3d.ColoringAttributes; 
import javax.media.j3d.GeometryArray; 
import javax.media.j3d.PointArray; 
import javax.media.j3d.PointAttributes; 
import javax.media.j3d.Shape3D; 
import javax.media.j3d.TransformGroup; 
import javax.media.jai.JAI; 
import javax.media.jai.PlanarImage; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.vecmath.Color3f; 
import javax.vecmath.Point3d; 
import javax.vecmath.Point3f; 


public final class FinalDImage extends JPanel { 

    private static int s = 0, count = 0, r = 0, g = 0, b = 0, medHeight = 0, medWidth = 0; 
    private static float divisior1 = 300000.0f; 
    private static Shape3D plShape; 
    private static TransformGroup objRotate; 
    private static BranchGroup scene; 
    private static JButton btn; 
    private static Canvas3D canvas3D; 
    private static SimpleUniverse simpleU; 
    private static GraphicsConfiguration gc; 
    private static BufferedImage histImage; 
    private static JPanel jPanel1 = new JPanel(); 

    public FinalDImage(BufferedImage histImage) { 
     FinalDImage.histImage = histImage; 
     medWidth = (int) (histImage.getWidth()/2.0f); 
     medHeight = (int) (histImage.getHeight()/2.0f); 
     this.add(jPanel1); 
     initComponents(); 
    } 

    public void initComponents() { 
     setLayout(new BorderLayout()); 
     btn = new JButton("Intensity"); 
     gc = SimpleUniverse.getPreferredConfiguration(); 
     canvas3D = new Canvas3D(gc);//See the added gc? this is a preferred config 
     add("Center", canvas3D); 
     add(btn, BorderLayout.SOUTH); 
     scene = createSceneGraph(FinalDImage.histImage); 
     scene.setCapability(BranchGroup.ALLOW_DETACH); 
     scene.compile(); 
     simpleU = new SimpleUniverse(canvas3D); 
     simpleU.getViewingPlatform().setNominalViewingTransform(); 
     simpleU.addBranchGraph(scene); 
    } 

    public BranchGroup createSceneGraph(BufferedImage histImage) { 
     count = 0; 
     BranchGroup lineGroup = new BranchGroup(); 
     Appearance app = new Appearance(); 
     ColoringAttributes ca = new ColoringAttributes(new Color3f(204.0f, 204.0f, 204.0f), ColoringAttributes.SHADE_FLAT); 
     app.setColoringAttributes(ca); 

     Point3f plaPts; 
     Color3f color; 
     PointArray pla = new PointArray(histImage.getWidth() * histImage.getHeight(), GeometryArray.COLOR_3 | GeometryArray.COORDINATES); 

     if (histImage.getType() == 11) { 
      for (int i = histImage.getWidth() - 3; i >= 3; i--) { 
       for (int j = histImage.getHeight() - 3; j >= 3; j--) { 
        s = histImage.getRaster().getSample(i, j, 0); 
        plaPts = new Point3f((medWidth - i)/1500.0f, 
             (medHeight - j)/1500.0f, 
             s/divisior1); 
        color = new Color3f(s/60000.0f, s/60000.0f, s/60000.0f); 
        pla.setCoordinate(count, plaPts); 
        pla.setColor(count, color); 
        count++; 
       } 
      } 
     } else { 
      for (int i = 2; i < histImage.getWidth() - 2; i++) { 
       for (int j = 2; j < histImage.getHeight() - 2; j++) { 
        r = histImage.getRaster().getSample(i, j, 0); 
        g = histImage.getRaster().getSample(i, j, 1); 
        b = histImage.getRaster().getSample(i, j, 2); 
        s = (r + g + b)/3; 
        plaPts = new Point3f((medWidth - i)/1200.0f, 
             (medHeight - j)/1200.0f, 
             s/(divisior1/600.0f)); 
        color = new Color3f(r/300.0f, g/300.0f, b/300.0f); 
        pla.setCoordinate(count, plaPts); 
        pla.setColor(count, color); 
        count++; 
       } 
      } 
     } 


     PointAttributes a_point_just_bigger = new PointAttributes(); 
     a_point_just_bigger.setPointSize(4.0f);//10 pixel-wide point 
     a_point_just_bigger.setPointAntialiasingEnable(true); //now points are sphere-like (not a cube) 
     app.setPointAttributes(a_point_just_bigger); 
     plShape = new Shape3D(pla,app); 
     objRotate = new TransformGroup(); 
     objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
     objRotate.addChild(plShape); 
     lineGroup.addChild(objRotate); 
     MouseRotate myMouseRotate = new MouseRotate(); 
     myMouseRotate.setFactor(0.015, 0.015); 
     myMouseRotate.setTransformGroup(objRotate); 
     myMouseRotate.setSchedulingBounds(new BoundingSphere()); 
     myMouseRotate.setSchedulingBounds(new BoundingSphere(
       new Point3d(0.0, 0.0, 0.0), 100)); 

     lineGroup.addChild(myMouseRotate); 
     MouseWheelZoom mz = new MouseWheelZoom(); 
     mz.setFactor(0.01); 
     mz.setTransformGroup(objRotate); 
     mz.setSchedulingBounds(new BoundingSphere()); 
     lineGroup.addChild(mz); 
     return lineGroup; 
    } 

    public static void main(String[] args) { 
     PlanarImage plImg3 = JAI.create("fileload", "E:\\Data\\office\\teeth3.tiff"); 
     BufferedImage histImage = plImg3.getAsBufferedImage(); 
     JFrame frame = new JFrame(); 
     frame.add(new JScrollPane(new FinalDImage(histImage))); 
     frame.setSize(800, 700); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

回答

1

這是java3D版本問題。我的代碼是可以的。但我安裝java3d不太適合那個操作系統的圖形配置。現在我用Dirextx安裝其他java3d,它的工作更好,更快。

相關問題