1

Plot of parabola isn't updated, when I calculate new coefficients of parabola. New coefficients of a parabola are calculated when the mouse is зкуыыув and moves. New coefficients on a graphics are displayed, but the plot remains the same. Why so?

package parabolademo;

import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.ArrayRealVector;
import org.apache.commons.math3.linear.DecompositionSolver;
import org.apache.commons.math3.linear.LUDecomposition;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.linear.RealVector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.function.PolynomialFunction2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


public class ParabolaDemo extends ApplicationFrame {

    /*
     * @param title  the frame title.
     */
    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;
    XYPlot plot;
    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;
        p = new PolynomialFunction2D(a);
        XYDataset dataset = DatasetUtilities.sampleFunction2D(p, lrange, rrange, 20, "y = f(x)");

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

        plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer render = (XYLineAndShapeRenderer) plot.getRenderer();
        render.setBaseShapesVisible(true);
        render.setSeriesShape(0, new Rectangle(-6, -6, 12, 12));

        chartPanel = new ChartPanel(chart);

        chartPanel.addMouseMotionListener(new MotionListener());

        chartPanel.addChartMouseListener(new ChartMouseListener() {

            @Override
            public void chartMouseClicked(ChartMouseEvent cme) {

            }

            @Override
            public void chartMouseMoved(ChartMouseEvent cme) {
                ChartEntity ce = cme.getEntity();
                if (ce instanceof XYItemEntity) {
                    flag = 1;
                    XYItemEntity e = (XYItemEntity) ce;
                    XYDataset d = ((XYItemEntity) ce).getDataset();
                    int i = ((XYItemEntity) ce).getItem();
                    chartpx = d.getXValue(0, i);
                    chartpy = d.getYValue(0, i);
                    //System.out.println("X:" + chartpx + ", Y:" + chartpy);
                }
                else
                    flag = 0;
                Point2D po = chartPanel.translateScreenToJava2D(cme.getTrigger().getPoint());
                Rectangle2D plotArea = chartPanel.getScreenDataArea();
                XYPlot plot = (XYPlot) chart.getPlot(); // your plot
                chartX = plot.getDomainAxis().java2DToValue(po.getX(), plotArea, plot.getDomainAxisEdge());
                chartY = plot.getRangeAxis().java2DToValue(po.getY(), plotArea, plot.getRangeAxisEdge());
                //System.out.println("X:" + chartX + ", Y:" + chartY);
            }
        });
        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) {
            double dx = me.getX();
            double dy = windowheight - me.getY();
            double graphx, graphy;
            if (dx < 83)
                dx = 83;
            else if (dx > 468)
                dx = 468;
            if (dy < 74)
                dy = 74;
            else if (dy > 468)
                dy = 468;
            dx -= 83;
            dy -= 74;
            graphx = 40.0 / 385.0 * dx - 20;
            graphy = 400.0 / 153.0 * dy;
            if (flag == 1) {
                //System.out.println("Dragged! x = " + dx + ", y = " + dy + ", xplot = " + graphx + ", yplot = " + graphy);
            }
            a = calculate(graphx, graphy);
            plot.setDataset(createDataset(a));
        }

        @Override
        public void mouseMoved(MouseEvent me) {

        }

    }

    private double[] calculate(double x, double y) {
        double px1, px2, py1, py2;
        double[] coef;
        coef = p.getCoefficients();
        if (x == (-coef[1])/2*coef[2]) {
            px1 = lrange;
            py1 = p.getValue(px1);
            px2 = rrange;
            py2 = p.getValue(px2);
        }
        else if (x == lrange) {
            px1 = (-coef[1])/2*coef[2];
            py1 = p.getValue(px1);
            px2 = rrange;
            py2 = p.getValue(px2);
        }
        else if (x == rrange) {
            px1 = lrange;
            py1 = p.getValue(px1);
            px2 = (-coef[1])/2*coef[0];
            py2 = p.getValue(px2);
        }
        else if (x > (-coef[1])/2*coef[2]){
            px1 = lrange;
            py1 = p.getValue(px1);
            px2 = (-coef[1])/2*coef[2];
            py2 = p.getValue(px2);
        }
        else {
            px1 = (-coef[1])/2*coef[2];
            py1 = p.getValue(px1);
            px2 = rrange;
            py2 = p.getValue(px2);
        }
        System.out.println("("+px1+","+py1+") ("+px2+","+py2+") ("+x+","+y+")");
        RealMatrix coefficients = new Array2DRowRealMatrix(new double[][] { { px1*px1, px1, 1 }, { px2*px2, px2, 1 },
            { x*x, x, 1 } }, false);
        DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();
        RealVector constants = new ArrayRealVector(new double[] { py1, py2, y }, false);
        RealVector solution = solver.solve(constants);
        coef[2] = solution.getEntry(0);
        coef[1] = solution.getEntry(1);
        coef[0] = solution.getEntry(2);
        System.out.println(coef[0] + ", " + coef[1] + ", " + coef[2]);
        return coef;
    }

    private XYDataset createDataset(double[] a) {
        double[] array = a;
        return DatasetUtilities.sampleFunction2D(
            p, -20.0, 20.0, 20, "y = " + array[2] + "x²+" + array[1] + "x+" + array[0] + " {-20…20}");

    }
}

2 Answers 2

1

Your createDataset() method is using the original PolynomialFunction2D; you can update it as shown below:

private XYDataset createDataset(double[] a) {
    p = new PolynomialFunction2D(a);
    return DatasetUtilities.sampleFunction2D(
        p, -20.0, 20.0, 20, "y = " + a[2] + "x²+" + a[1] + "x+" + a[0] + " {-20…20}");
}
Sign up to request clarification or add additional context in comments.

2 Comments

it works, thanks! whether there is any method to fix the y coordinate that its value 400 remained maximum?
The axis should auto-range by default, but you can set the bounds explicitly.
0

it works:

    plot = (XYPlot) chart.getPlot();

    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setRange(0.0, 400.0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.