如何在JFrame中集成jzy3d canvas?

时间:2019-01-29 11:02:03

标签: java swing jzy3d

我想在JFrame中添加3D图。为此,我使用jzy3d库绘制3D图。我可以在新窗口中绘制3D图,但是找不到在其他组件(例如JButton等)的JFrame中创建3D图的解决方案。
当然,我看到其他人也有同样的问题,但我并没有成功回答以下问题:Answer 1Answer 2Answer 3

我当前不适当的解决方案:(有效)

import org.jzy3d.analysis.AbstractAnalysis;
import org.jzy3d.analysis.AnalysisLauncher;
import org.jzy3d.chart.factories.AWTChartComponentFactory;
import org.jzy3d.colors.Color;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.builder.Builder;
import org.jzy3d.plot3d.builder.Mapper;
import org.jzy3d.plot3d.builder.concrete.OrthonormalGrid;
import org.jzy3d.plot3d.primitives.Shape;
import org.jzy3d.plot3d.rendering.canvas.Quality;

public class Demo extends AbstractAnalysis {

@Override
public void init() throws Exception {
    // Define a function to plot
    Mapper mapper = new Mapper() {
        @Override
        public double f(double x, double y) {
            return x * y;
        }
    };

    // Define range and precision for the function to plot
    Range range = new Range(-1, 1);
    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 = AWTChartComponentFactory.chart(Quality.Advanced, getCanvasType());
    chart.getScene().getGraph().add(surface);

}

public static void main(String[] args) throws Exception {
    AnalysisLauncher.open(new Demo());
}

}

我想要一个代码:

public class Demo extends AbstractAnalysis {

@Override
public void init() throws Exception {
    // Same code
}

public JPanel getJPanel() {
    // Unknown code
}

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    Demo demo = new Demo();
    demo.init();
    JPanel mainPanel = new JPanel();
    JPanel panel = demo.getJPanel();
    mainPanel.add(panel, BorderLayout.CENTER);
    JButton button = new JButton("OK");
    mainPanel.add(button, BorderLayout.SOUTH);
    frame.add(mainPanel);
    frame.setVisible(true);
}

}

0 个答案:

没有答案