Check if point belongs to the plot

孤者浪人 提交于 2019-12-11 05:42:51

问题


I'm using JFreeChart to draw XYPlot. What I'm currently trying to do:

  1. User clicks on the plot and the app gives him back the coordinates of the chosen point; I think that I've got it right. Here is some code:

        public void chartMouseClicked(ChartMouseEvent arg0) {
               Rectangle2D plotArea = chPanel.getScreenDataArea();
               XYPlot plot = (XYPlot) chart.getPlot();
               double chartX = plot.getDomainAxis().java2DToValue(arg0.getTrigger().getPoint().getX(), plotArea, plot.getDomainAxisEdge());
               double chartY = plot.getRangeAxis().java2DToValue(arg0.getTrigger().getPoint().getY(), plotArea, plot.getRangeAxisEdge());
               JOptionPane.showMessageDialog(null, "   x: "+chartX+ "  y: "+chartY);}       
    
  2. I need to determine if user clicked on the line or not. The target is to mark the clicked spot if it is on the drawn graph.

I would be grateful for any clue.


回答1:


Instead of working with coordinates, add a ChartMouseListener as shown here. You can examine the ChartMouseEvent and any corresponding ChartEntity for a variety of details.




回答2:


ShapeUtilities.intersects(java.awt.geom.Rectangle2D rect1, java.awt.geom.Rectangle2D rect2) 

where rect2 can be "0" rectangle (for your points).



来源:https://stackoverflow.com/questions/10156338/check-if-point-belongs-to-the-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!