如何在不创建新的JFrame的情况下将jzy3d图表添加到JFrame?

时间:2012-10-18 07:01:30

标签: java swing jframe layout-manager

在JFrame中创建jzy3d图表有以下代码:

public class SurfaceViewerFrame extends IconFrame {

    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());

//      ChartLauncher.openChart(chart, new Rectangle(0, 0, 100, 100), "122");

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

但如果我不推荐openChart()方法,我什么也看不见。如果我这样做,我的JFrame中会有一个Chart和一个新的空JFrame;我不想用它。请告诉我,我该如何解决?我需要在我的JFrame中显示图形而不需要新的图形。

更新: 抱歉,jzy3d是用于制作3D曲面的库。这段代码有效,我不需要其他LayoutManager,请再次阅读我的问题。

1 个答案:

答案 0 :(得分:3)

CardLayout非常适合此用途。有关详细信息和工作示例,请参阅How to Use CardLayout

其他合并数据的策略可以在this answer“使用多个JFrame,好/坏的做法?”中查看/链接。

更新

充实这个想法。

  • 不要延伸框架,只需保留对面板的引用。
  • 将面板设为GridLayoutBorderLayout(任何单个组件都添加到任何一个没有约束的组件,将拉伸到可用的宽度和高度)。
  • 在上面的代码段中填写该面板。
  • 将该面板添加到主(仅)框架中CardLayout的卡片中。
  • (如有必要)翻到那张卡片。