jmathplot doesn't work in swing application

前端 未结 2 848
春和景丽
春和景丽 2021-01-20 09:36

I\'m trying to use the jmathplot library in a swing application.

Problem is, it doesn\'t seem to work when I add it to a JPanel as follows:

    // cr         


        
相关标签:
2条回答
  • 2021-01-20 09:55

    You can see here examples if you want Jean-Paul:

    http://jmathtools.berlios.de/doku.php?id=jmathplot:tutorial

    0 讨论(0)
  • 2021-01-20 10:21
    • works with NullLayout, without any issue, but I'm quite not familair with this layout manager (delegate Insets from parent), then I'm leaving any comments about

    • simple don't to use NullLayout

    • override getPreferredSize for Plot2DPanel

    • JPanel has FLowLayout (with the same output as from NullLayout! on resize), accepting only PreferredSize, child aren't resizable with its contianer, is required to use BorderLayout/GridLayout for simple graph

    .

    enter image description here .

    enter image description here

    .

    from code

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import org.math.plot.Plot2DPanel;
    
    public class MyPlot {
    
        private JFrame frame = new JFrame("JMathPlot library in a swing application.");
        private JPanel panel = new JPanel();
    
        public MyPlot() {
            double[] x = new double[]{0, 1, 2, 3, 4, 5};
            double[] y = new double[]{10, 11, 12, 14, 15, 16};
            Plot2DPanel plot = new Plot2DPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(400, 200);
                }
            };
            plot.addLinePlot("my plot", x, y); // add a line plot to the PlotPanel
            panel.setLayout(new BorderLayout());
            panel.add(plot);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(panel);
            frame.pack();
            frame.setLocation(150, 150);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new MyPlot();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题