2012-09-27 98 views
5

我想用鼠标单击在我的应用中设置点。我使用JFreeChart并在ChartPanel鼠标侦听器中使用。这是这个样子:将鼠标监听器坐标转换为图表坐标

panel.addChartMouseListener(new ThisMouseListener()); 

和我的鼠标监听ThisMouseListener()(未完成它):

class ThisMouseListener implements ChartMouseListener{ 

    @Override 
    public void chartMouseClicked(ChartMouseEvent event) { 
     int x = event.getTrigger().getX(); 
     int y = event.getTrigger().getY(); 

     System.out.println("X :" + x + " Y : " + y); 

     ChartEntity entity = event.getEntity(); 
     if(entity != null && (entity instanceof XYItemEntity)){ 
      XYItemEntity item = (XYItemEntity)entity; 
     } 
     new JOptionPane().showMessageDialog(null, "Hello", "Mouse Clicked event", JOptionPane.OK_OPTION); 
    } 

    @Override 
    public void chartMouseMoved(ChartMouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

} 

但这款鼠标监听器返回我我的面板坐标,我想从坐标我的图表。可能我必须使用其他对象的侦听器吗?或者我可以用某种方法转换坐标?

+0

是否要添加新的点或选择现有的点?请编辑您的问题以包含显示您当前方法的[sscce](http://sscce.org/)。 – trashgod

回答

3

您将侦听器添加到面板。因此,当您单击鼠标时,您会收到相对于面板的坐标 - 这是事件的来源。您需要将此侦听器添加到图表中。

其他可能性是获取图表坐标相对于面板,并从x和y中减去它们。

Point p = chart.getLocation();  
int px = p.getX(); 
int py = p.getY(); 

x = x-px; // x from event 
y = y-py; // y from event 
// x and y are now coordinates in respect to the chart 

if(x<0 || y<0 || x>chart.getWidth() || y>chart.getHeight()) // the click was outside of the chart 
else // the click happened within boundaries of the chart and 

如果面板是图表组件的容器,您的解决方案可能看起来像上面那样。请注意,这些坐标将是相对于图表左上角的坐标。

+1

这不会有点脆吗?看起来你必须在渲染器内部工作才能获得可靠的几何图形。 – trashgod

+0

你什么意思是脆?你认为它会在某个时候崩溃吗?通过渲染器,你的意思是监听器中的chartMouseClicked()函数(我认为你的意思是这个 - http://docs.oracle.com/javaee/5/api/javax/faces/render/Renderer.html)?我省略了x和y的声明,并不是因为我建议将它包含在鼠标单击的函数中,而是要注意这些变量的来源。 – user1581900

+0

我的意思是说图表相对坐标可能会发生变化,而渲染器可以使用轴在屏幕和模型坐标之间进行转换。 – trashgod

2

获取X,Y通过

double x = event.getChart().getXYPlot().getDomainCrosshairValue(); 
double y = event.getChart().getXYPlot().getRangeCrosshairValue(); 

的一个主要问题坐标在图形中的空间:我发现的JFreeChart不更新这些值,直到后我ChartMouseEvent处理程序被调用;每次通过我得到以前的值。您可以查看XYPlot.handleClick(x,y,info)以获取详细信息以获取处理程序中的当前值。

1

您必须获取对ChartPanel的引用,然后才能绘制它,并且只有在此之后,才能从Plot中获取正确的X,Y坐标。为此,您必须将坐标检索放在awt队列中,而不是直接调用它。下面是一个适用于我的示例(仅适用于X坐标)

@Override 
public void chartMouseClicked(ChartMouseEvent cme) { 
    final ChartMouseEvent cmeLocal = cme; 
    ChartPanel hostChartPanel = (ChartPanel) cme.getTrigger().getComponent(); 
    if (null != hostChartPanel) { 

     //Crosshair values are not valid until after the chart has been updated 
     //that is why call repaint() now and post Crosshair value retrieval on the 
     //awt thread queue to get them when repaint() is finished 
     hostChartPanel.repaint(); 

     java.awt.EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFreeChart chart = cmeLocal.getChart(); 
       XYPlot plot = chart.getXYPlot(); 
       double crossHairX = plot.getDomainCrosshairValue(); 
       JOptionPane.showMessageDialog(null, Double.toString(crossHairX), "X-Value", JOptionPane.OK_OPTION); 
      } 
     }); 
    } 
}