java - 如何在NetBeans平台应用程序中更快地显示Java3D图像?

标签 java performance java-3d netbeans-platform

我使用 Java Swing 技术在 NetBeans 平台上制作了一个桌面应用程序。在该应用程序中,我使用 PointArray[] 对象对 3D 图像进行图像处理。

当我的应用程序在配备 Windows 7、显卡和良好 RAM 容量(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);
    }
}

最佳答案

这是java3D版本问题。我的代码没问题。但我安装的 java3d 不适用于该操作系统的图形配置。现在我使用 Diretecx 安装其​​他 java3d,它运行良好且速度更快。

关于java - 如何在NetBeans平台应用程序中更快地显示Java3D图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12693924/

相关文章:

java - StackOverflowError 计算 BigInteger 的阶乘?

java - 在 JfxPane 中创建上下文菜单

c# - 使用 C# 将 HashSet 中的一组正则表达式与 ASP.NET 中的字符串进行匹配的最佳方法是什么?

java - 线程 “main” java.lang.NoClassDefFoundError中的异常:org/apache/commons/cli2/Option

java - 像 read() 这样的 I/O 方法如何在 Java 中将线程置于阻塞状态?

mysql - PostgreSQL vs MySQL 简单查询性能

php - 通过 PHP 下载文件还是通过直接链接下载文件更快?

java - 存储 3D 对象? ( java )

java - Java3D:逐步旋转Universe

java - 在 Java 3D 中使用多种行为 - 丢失 "focus"