【发布时间】:2013-11-27 18:00:20
【问题描述】:
我有一个抛物线图,抛物线方程的系数存储在数组a 中。在mouseDragged(mousemotionlistener)中,抛物线的系数发生了变化,我想用新的系数实时更新抛物线图。我怎样才能做到这一点?
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"
}
}
【问题讨论】:
标签: java plot jfreechart