0

次のコードがあります。

public class SurfaceViewerFrame extends JFrame {

    public SurfaceViewerFrame() {
        setResizable(false);
        //System.loadLibrary("lib/jogl2-rc10/gluegen-rt.jar");
        Settings.getInstance().setHardwareAccelerated(true);
        FormLayout layout=new FormLayout("10px, 300px, 10px", "30px, 10px, 20px, 300px, 10px");
        CellConstraints сс=new CellConstraints();

        JLabel title=new JLabel("Выходная поверхность");


        Mapper mapper = new Mapper() {
            public double f(double x, double y) {
                return x * Math.sin(x * y);
            }
        };
        // Define range and precision for the function to plot
        Range range = new Range(-300, 300);
        int steps = 80;

        // Create the object to represent the function over the given range.
        final Shape surface = Builder.buildOrthonormal(new OrthonormalGrid(range, steps, range, steps), mapper);
        surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(), surface.getBounds().getZmax(), new Color(1, 1, 1, .5f)));
        surface.setFaceDisplayed(true);
        surface.setWireframeDisplayed(false);

        // Create a chart
        Chart chart = new Chart(Quality.Advanced, "awt");
        chart.getScene().getGraph().add(surface);
        chart.addController(new CameraKeyController());

        JPanel panel=new JPanel();
        panel.add(title, сс.xy(1, 1));
        panel.add((JComponent)chart.getCanvas(), CC.xy(1, 3));
        add(panel);
        setSize(320, 370);
        setVisible(true);
    }
}

Chart オブジェクトを作成したら、それを JFrame の特別な場所に追加する必要があります。しかし、この構造を使用しようとすると、chart.getCanvas() の JComponent へのキャストに関する例外が発生します。教えてください、どうすれば修正できますか?前もって感謝します。

4

1 に答える 1

2

CanvasではありませんJComponent- それは awtComponentです。そもそもキャストする必要はありませんが、そうする場合はComponent代わりにキャストしてください。

panel.add((Component)chart.getCanvas(), CC.xy(1, 3));

詳細については、Javadocを参照してください。

于 2012-10-17T15:17:39.387 に答える