抛物线图未更新。当鼠标зкуыыув并移动时,计算抛物线的新系数。显示图形上的新系数,但图表保持不变。为什么这样?
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}");
}
}
答案 0 :(得分:1)
您的createDataset()
方法正在使用原始PolynomialFunction2D
;你可以更新它,如下所示:
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}");
}
答案 1 :(得分:0)
它有效:
plot = (XYPlot) chart.getPlot();
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setRange(0.0, 400.0);