问题
I'm using JFreeChart to draw XYPlot. What I'm currently trying to do:
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);}
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