簡體   English   中英

如何在NetBeans平台應用程序中更快地顯示Java3D圖像?

[英]How to display Java3D image faster in NetBeans platform app?

我使用Java Swing技術在NetBeans平台上制作了一個桌面應用程序。 在該應用程序中,我使用PointArray []對象對3D圖像進行圖像處理。

當我的應用程序在具有Windows 7,圖形卡和良好的RAM容量(4GB RAM)的PC上運行時,在單擊3DImage按鈕后的5到6秒內將創建並顯示我的3D圖像。 但是,當我的應用程序在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);
    }
}

這是java3D版本問題。 我的代碼還可以。 但是我安裝的Java3d對於該操作系統的圖形配置不是很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM