线程" main"中的例外情况java.lang.ClassCastException:org.j3d.aviatrix3d.output.graphics.SimpleAWTSurface无法强制转换为javax.swing.JComponent

时间:2016-04-20 11:21:03

标签: java classcastexception

我试图创建一个导入X3D模型并显示它们的Java类。我在Netbeans项目中保存了名为test.x3d的X3D模型。我的代码是

import java.awt.*;
import java.util.HashMap;
import javax.swing.*;

import org.web3d.x3d.sai.*;
import org.xj3d.sai.Xj3DBrowser;

public class Xj3DSpecificDemo extends JFrame {

    public Xj3DSpecificDemo() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container contentPane = getContentPane();

        // Setup browser parameters
        HashMap requestedParameters=new HashMap();
        requestedParameters.put("Xj3D_ShowConsole",Boolean.FALSE);
        requestedParameters.put("Xj3D_FPSShown", Boolean.TRUE);

        // Create an SAI component
        X3DComponent x3dComp = BrowserFactory.createX3DComponent(requestedParameters);

        // Add the component to the UI
        JComponent x3dPanel = (JComponent)x3dComp.getImplementation();
        contentPane.add(x3dPanel, BorderLayout.CENTER);

        // Get an external browser
        ExternalBrowser x3dBrowser = x3dComp.getBrowser();

        boolean useXj3D = true;

        if (x3dBrowser.getName().indexOf("Xj3D") < 0) {
            System.out.println("Not running on Xj3D, extended functions disabled");
            useXj3D = false;
        }
        setSize(500,500);
        setVisible(true);


        // Create an X3D scene by loading a file
        X3DScene mainScene = x3dBrowser.createX3DFromURL(new String[] { "test.x3d" });

        // Replace the current world with the new one
        x3dBrowser.replaceWorld(mainScene);

        if (!useXj3D)
            return;

        Xj3DBrowser xj3dBrowser = (Xj3DBrowser) x3dBrowser;
        xj3dBrowser.setMinimumFrameInterval(1000 / 15);

        boolean lines = true;

        while(true) {
            if (lines) {
                xj3dBrowser.setRenderingStyle(Xj3DBrowser.RENDER_LINES);
                lines = false;
            } else {
                xj3dBrowser.setRenderingStyle(Xj3DBrowser.RENDER_SHADED);
                lines = true;
            }

            try { Thread.sleep(250); } catch(Exception e) {}
        }

    }
    public static void main(String[] args) {

        Xj3DSpecificDemo demo = new Xj3DSpecificDemo();
    }

}

我得到的错误是

Exception in thread "main" java.lang.ClassCastException: org.j3d.aviatrix3d.output.graphics.SimpleAWTSurface cannot be cast to javax.swing.JComponent
    at Xj3DSpecificDemo.<init>(Xj3DSpecificDemo.java:44)
    at Xj3DSpecificDemo.main(Xj3DSpecificDemo.java:95)

但我似乎无法在任何地方找到aviatrix3d jar下载

编辑

下载aviatrix3d jar后,我收到以下错误

Exception in thread "main" java.lang.ClassCastException: org.j3d.aviatrix3d.output.graphics.SimpleAWTSurface cannot be cast to javax.swing.JComponent
    at Xj3DSpecificDemo.<init>(Xj3DSpecificDemo.java:23)
    at Xj3DSpecificDemo.main(Xj3DSpecificDemo.java:69)

修改#2

我还尝试使用以下代码,因此我认为它是Jars中的一个,因为我得到了同样的错误

package simplesaidemo1;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.JFrame;
import org.web3d.x3d.sai.*;

public class SimpleSAIDemo1 extends JFrame{
    public SimpleSAIDemo1() {
        Container contentPane = getContentPane();

        // Create an SAI component
        X3DComponent x3dComp = BrowserFactory.createX3DComponent(null);

        // Add the component to the UI
        JComponent x3dPanel = (JComponent)x3dComp.getImplementation();
        contentPane.add(x3dPanel, BorderLayout.CENTER);

        // Get an external browser
        ExternalBrowser x3dBrowser = x3dComp.getBrowser();

        // Create an X3D scene by loading a file
        X3DScene mainScene = x3dBrowser.createX3DFromURL(new String[] { "test.x3d" });

        // Replace the current world with the new one
        x3dBrowser.replaceWorld(mainScene);
    }
    public static void main(String[] args) {
        SimpleSAIDemo1 demo = new SimpleSAIDemo1();
    }    
}

唯一的区别是以下行说错误在

at simplesaidemo1.SimpleSAIDemo1.<init>(SimpleSAIDemo1.java:17)
at simplesaidemo1.SimpleSAIDemo1.main(SimpleSAIDemo1.java:30)

0 个答案:

没有答案