Java怎么打开jf_Java – 鼠标按钮释放后如何接收点坐标(JF...

我用JFreeChart写了一个简单的抛物线图.我得到图形点坐标,当按下鼠标按钮时,我也想获得图形点坐标,当鼠标按钮被释放时.

但是按下鼠标按钮时,chartMouseMoved不起作用.如何在释放鼠标按钮后解决问题并获取图形坐标?

这是我的代码:

package parabolademo;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

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, prx = 0.0, pry = 0.0, chartpx = 0.0, chartpy = 0.0,

chartX = 0.0, chartY = 0.0;

int windowheight = 270;

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

);

final ChartPanel chartPanel = new ChartPanel(chart);

chartPanel.addMouseListener(new CustomListener());

chartPanel.addChartMouseListener(new ChartMouseListener() {

@Override

public void chartMouseClicked(ChartMouseEvent cme) {

}

@Override

public void chartMouseMoved(ChartMouseEvent cme) {

ChartEntity ce = cme.getEntity();

if (ce instanceof XYItemEntity) {

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);

}

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 CustomListener implements MouseListener {

public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

flag = 1;

px = chartpx;

py = chartpy;

System.out.println("Mouse Pressed! xpos = " + px + "; py = " + py);

}

public void mouseReleased(MouseEvent e) {

flag = 1;

prx = chartX;

pry = chartY;

System.out.println("Mouse Released! xpos = " + prx + "; py = " + pry);

}

}

}

提前致谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值