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- 它是 awt Component。你不应该一开始就进行演员表,但如果你这样做了,就把它转换成Component

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

有关更多信息,请参阅Javadocs

于 2012-10-17T15:17:39.387 回答