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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…