鼠标拖动时获取鼠标坐标(JFreeChart)

时间:2013-11-24 21:55:41

标签: java jfreechart mouselistener

拖动鼠标时如何在绘图中获取鼠标坐标?

package parabolademo;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

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.data.function.Function2D;
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 delta = 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;
    public ParabolaDemo(final String title) {

        super(title);
        double[] a = {0.0, 0.0, 1.0};
        Function2D p = new PolynomialFunction2D(a);
        double lrange = -20.0;
        double rrange = 20.0;
        XYDataset dataset = DatasetUtilities.sampleFunction2D(p, lrange, rrange, 1000, "y = f(x)");
        double max = 0.0;

        if (a[2] < 0) {
            max = p.getValue(-a[1] / 2*a[2]);
        }
        else {
            if (p.getValue(lrange) > p.getValue(rrange))
                max = p.getValue(lrange);
            else
                max = p.getValue(rrange);
        }
        System.out.println("max = " + max);
        delta = max / 16.4;

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

        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 = 0.0, dy = 0.0;
            dx = me.getX();
            dy = windowheight - me.getY();
            if (flag == 1)
                System.out.println("Dragged! x = " + dx + ", y = " + dy);
        }

        @Override
        public void mouseMoved(MouseEvent me) {

        }

    }

} 

当用

拖动鼠标时,我只能得到鼠标坐标
dx = me.getX();
dy = windowheight - me.getY();

但是坐标是鼠标坐标,而不是情节坐标。如何在MouseDragged中获得 plot 坐标?

0 个答案:

没有答案