Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a parabola plot, coefficients of parabola equation are stored in array a. In mouseDragged (mousemotionlistener), coefficients of parabola were changed and I want to update a parabola plot with new coefficients in realtime. How can I make this happen?

public class ParabolaDemo extends ApplicationFrame {
    int flag = 0;
    double px = 0.0, py = 0.0, chartpx = 0.0, chartpy = 0.0, 
    chartX = 0.0, chartY = 0.0;
    int windowheight = 270;
    ChartPanel chartPanel;
    PolynomialFunction2D p;
    double lrange = -20.0;
    double rrange = 20.0;
    double[] a;

    public ParabolaDemo(final String title) {

        super(title);
        double[] tmp = {0.0, 0.0, 1.0};
        a = tmp; // coeffcients of parabola (a[0] + a[1]*x + a[2]*x^2)
        p = new PolynomialFunction2D(a);
        XYDataset dataset = DatasetUtilities.sampleFunction2D(p, lrange, rrange, 1000, "y = f(x)");

        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Parabola",
            "X", 
            "Y", 
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );

        chartPanel = new ChartPanel(chart);

        chartPanel.addMouseMotionListener(new MotionListener());

        //some code...

        chartPanel.setPreferredSize(new java.awt.Dimension(500, windowheight));
        chartPanel.setDomainZoomable(false);
        chartPanel.setRangeZoomable(false);
        setContentPane(chartPanel);
    }

    public static void main(final String[] args) {
        final ParabolaDemo demo = new ParabolaDemo("Parabola Plot Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }


    public class MotionListener implements MouseMotionListener {

        @Override
        public void mouseDragged(MouseEvent me) {

            //some code...

            a = calculate(graphx, graphy);
        }

        @Override
        public void mouseMoved(MouseEvent me) {

        }

    }

    private double[] calculate(double x, double y) {
        //some code...
        //it is function that changes coefficients of array "a"
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
460 views
Welcome To Ask or Share your Answers For Others

1 Answer

If the data is DefaultCategoryDataset just use setValue on your dataSet. If its time series data then use TimeSeriesCollection.add() . The listeners for the charts should be called automatically for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...