List of usage examples for java.awt.geom Point2D getY
public abstract double getY();
From source file:ec.util.chart.swing.Charts.java
@Nullable public static LegendItemEntity getSeriesForPoint(@Nonnull Point pt, @Nonnull ChartPanel cp) { final double chartX; final double chartY; final Rectangle2D plotArea; final XYPlot plot; {/*w w w. j a v a2 s. c o m*/ // Let's find the X and Y values of the clicked point Point2D p = cp.translateScreenToJava2D(pt); chartX = p.getX(); chartY = p.getY(); // Let's find plotArea and plot XYPlot tmpPlot = cp.getChart().getXYPlot(); PlotRenderingInfo plotInfo = cp.getChartRenderingInfo().getPlotInfo(); if (tmpPlot instanceof CombinedDomainXYPlot) { int subplotIndex = plotInfo.getSubplotIndex(p); if (subplotIndex == -1) { return null; } plotArea = plotInfo.getSubplotInfo(subplotIndex).getDataArea(); plot = ((CombinedDomainXYPlot) tmpPlot).findSubplot(plotInfo, p); } else { plotArea = plotInfo.getDataArea(); plot = tmpPlot; } } // Let's avoid unnecessary computation final ValueAxis domainAxis = plot.getDomainAxis(); final ValueAxis rangeAxis = plot.getRangeAxis(); final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge(); final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge(); final double x = domainAxis.java2DToValue(chartX, plotArea, domainAxisEdge); final double sensitivity = TOL; double distanceClickSeries = TOL + 1; Entry<XYDataset, Comparable> result = null; // For each series in each datasets for (XYDataset dataset : asDatasetList(plot)) { for (int series = 0; series < dataset.getSeriesCount(); series++) { // Index of the closest data item of the current series just left to the click int lp = getNearestLeftPoint(x, 0, dataset.getItemCount(series) - 1, series, dataset); try { // X and Y values of data items to the left and to the right double leftX = dataset.getXValue(series, lp); double leftY = dataset.getYValue(series, lp); double rightX = dataset.getXValue(series, lp + 1); double rightY = dataset.getYValue(series, lp + 1); double lx = domainAxis.valueToJava2D(leftX, plotArea, domainAxisEdge); double ly = rangeAxis.valueToJava2D(leftY, plotArea, rangeAxisEdge); double rx = domainAxis.valueToJava2D(rightX, plotArea, domainAxisEdge); double ry = rangeAxis.valueToJava2D(rightY, plotArea, rangeAxisEdge); // Distance to left point double distL = Point2D.distance(lx, ly, chartX, chartY); // Distance to right point double distR = Point2D.distance(rx, ry, chartX, chartY); // Average of both distances double distLRavg = (distL + distR) / 2d; // Distance to the segment between L and R //double distSeg = Line2D.ptSegDist(leftX, leftY, rightX, rightY, chartX, chartY); double distSeg = ptSegDist(lx, ly, rx, ry, chartX, chartY); // With a line renderer, this is probably a bit of overkill as // distSeg would be enough, but it becomes more reliable to check all these // if using splines double tmp = Math.min(Math.min(distSeg, Math.min(distL, distR)), distLRavg); // Are we closer than the previous series? if (tmp < sensitivity && tmp < distanceClickSeries) { distanceClickSeries = tmp; result = new SimpleEntry<>(dataset, dataset.getSeriesKey(series)); } } catch (Exception ex) { /* * An exception might happen when some series have less data * than others, catching the the exception here will simply rule * them out from the detection on this click */ } } } return result != null ? createFakeLegendItemEntity(result.getKey(), result.getValue()) : null; }
From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java
private static void drawMaskName(Graphics2D g2, AbstractMask mask, Rectangle2D imageArea, double fontSizeRate) { if (mask.getName() == null) { return;//from ww w . java 2s . c om } Point2D fontLocation = mask.getTitleLocation(imageArea); g2.setPaint(Color.black); Font currentFont = g2.getFont(); g2.setFont(currentFont.deriveFont((float) (maskNameFont.getSize() * fontSizeRate)).deriveFont(Font.ITALIC)); g2.drawString(mask.getName(), (int) fontLocation.getX(), (int) fontLocation.getY()); g2.setFont(currentFont); }
From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java
public static Abstract2DMask translateChartRectangle(Abstract2DMask mask, Rectangle2D imageArea, JFreeChart chart) {/*from ww w . j a va2 s . co m*/ Rectangle2D bound = mask.getRectangleFrame(); Point2D start = new Point2D.Double(bound.getMinX(), bound.getMinY()); Point2D end = new Point2D.Double(bound.getMaxX(), bound.getMaxY()); Point2D screenStart = translateChartPoint(start, imageArea, chart); Point2D screenEnd = translateChartPoint(end, imageArea, chart); Abstract2DMask imageMask = mask.clone(); imageMask.setRectangleFrame(new Rectangle2D.Double(Math.min(screenStart.getX(), screenEnd.getX()), Math.min(screenStart.getY(), screenEnd.getY()), Math.abs(screenStart.getX() - screenEnd.getX()), Math.abs(screenStart.getY() - screenEnd.getY()))); return imageMask; }
From source file:com.t_oster.visicut.misc.Helper.java
/** * Returns an AffineTransform, which transformes src to dest * and constists of a scale and translate component * @param src//ww w .j av a 2s. co m * @param dest * @return */ public static AffineTransform getTransform(Rectangle2D src, Rectangle2D dest) { AffineTransform scale = AffineTransform.getScaleInstance(dest.getWidth() / src.getWidth(), dest.getHeight() / src.getHeight()); Point2D scaled = scale.transform(new Point.Double(src.getX(), src.getY()), null); AffineTransform result = AffineTransform.getTranslateInstance(dest.getX() - scaled.getX(), dest.getY() - scaled.getY()); result.concatenate(scale); return result; }
From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java
public static Point2D translateChartPoint(Point2D point, Rectangle2D imageArea, JFreeChart chart) { XYPlot plot = chart.getXYPlot();/*from w w w .j av a 2 s. c o m*/ double x, y; ValueAxis domainAxis = plot.getDomainAxis(); ValueAxis rangeAxis = plot.getRangeAxis(); x = domainAxis.valueToJava2D(point.getX(), imageArea, RectangleEdge.BOTTOM); y = rangeAxis.valueToJava2D(point.getY(), imageArea, RectangleEdge.LEFT); return new Point2D.Double(x, y); }
From source file:ShapeTransform.java
/** * Normalize the line; the point with the lowest X is the primary point, if * both points have the same X, that point with the lowest Y value wins. * /*from ww w . ja va 2 s . c o m*/ * @param line * the original line * @return the normalized line */ private static Line2D getNormalizedLine(final Line2D line) { final Line2D lineClone = (Line2D) line.clone(); final Point2D p1 = line.getP1(); final Point2D p2 = line.getP2(); if (p1.getX() < p2.getX()) { return lineClone; } if (p1.getX() > p2.getX()) { lineClone.setLine(p2, p1); return lineClone; } if (p1.getY() < p2.getY()) { return lineClone; } lineClone.setLine(p2, p1); return lineClone; }
From source file:net.sf.mzmine.chartbasics.ChartLogicsFX.java
/** * Translates screen (pixel) values to plot values * //from ww w. j ava2s . co m * @param myChart * @return width in data space for x and y */ public static Point2D screenValueToPlotValue(ChartViewer myChart, int val) { Point2D p = mouseXYToPlotXY(myChart, 0, 0); Point2D p2 = mouseXYToPlotXY(myChart, val, val); // inverted y return new Point2D.Double(p2.getX() - p.getX(), p.getY() - p2.getY()); }
From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java
public static Shape translateChartShape(Shape shape, Rectangle2D imageArea, JFreeChart chart) { if (shape instanceof Line2D) { Line2D line = (Line2D) shape; double length = line.getP1().distance(line.getP2()); if (length == 0) { Point2D point = line.getP1(); Point2D newPoint = ChartMaskingUtilities.translateChartPoint(point, imageArea, chart); Shape oShape = ShapeUtilities.createDiagonalCross(5f, 0.2f); // Shape oShape = ShapeUtilities.createRegularCross(3f, 0.5f); Shape newShape = ShapeUtilities.createTranslatedShape(oShape, newPoint.getX(), newPoint.getY()); return newShape; } else if (length < 1e-6) { if (line.getP1().getX() == line.getP2().getX()) { double newX = ChartMaskingUtilities.translateChartPoint(line.getP1(), imageArea, chart).getX(); Line2D newLine = new Line2D.Double(newX, imageArea.getMinY(), newX, imageArea.getMaxY()); return newLine; } else { double newY = ChartMaskingUtilities.translateChartPoint(line.getP1(), imageArea, chart).getY(); Line2D newLine = new Line2D.Double(imageArea.getMinX(), newY, imageArea.getMaxX(), newY); return newLine; }//from w w w . jav a 2 s . c om } Line2D newShape = (Line2D) line.clone(); Point2D newP1 = translateChartPoint(line.getP1(), imageArea, chart); Point2D newP2 = translateChartPoint(line.getP2(), imageArea, chart); newShape.setLine(newP1, newP2); return newShape; } else if (shape instanceof RectangularShape) { RectangularShape rect = (RectangularShape) shape; RectangularShape newShape = (RectangularShape) rect.clone(); Rectangle2D bound = rect.getBounds2D(); Point2D start = new Point2D.Double(bound.getMinX(), bound.getMinY()); Point2D end = new Point2D.Double(bound.getMaxX(), bound.getMaxY()); Point2D screenStart = translateChartPoint(start, imageArea, chart); Point2D screenEnd = translateChartPoint(end, imageArea, chart); newShape.setFrame(new Rectangle2D.Double(Math.min(screenStart.getX(), screenEnd.getX()), Math.min(screenStart.getY(), screenEnd.getY()), Math.abs(screenStart.getX() - screenEnd.getX()), Math.abs(screenStart.getY() - screenEnd.getY()))); return newShape; } else { return shape; } }
From source file:net.sf.mzmine.chartbasics.ChartLogics.java
/** * Translates screen (pixel) values to plot values * //from w w w.jav a2 s . c o m * @param myChart * @return width in data space for x and y * @throws Exception */ public static Point2D screenValueToPlotValue(ChartPanel myChart, int val) throws Exception { Point2D p = mouseXYToPlotXY(myChart, 0, 0); Point2D p2 = mouseXYToPlotXY(myChart, val, val); // inverted y return new Point2D.Double(p2.getX() - p.getX(), p.getY() - p2.getY()); }
From source file:net.sf.mzmine.chartbasics.gestures.ChartGestureHandler.java
/** * Create preset handlers/*from ww w.j av a2 s . c o m*/ * * @param handler * @param g * @param param Parameters for specific handlers <br> * @return */ public static ChartGestureHandler createHandler(Handler handler, final ChartGesture g, Object[] param) { Consumer<ChartGestureEvent> newHandler = null; switch (handler) { case DEBUG: newHandler = e -> System.out.println(e.toString()); break; case PREVIOUS_ZOOM_HISTORY: newHandler = e -> { ZoomHistory h = e.getChartWrapper().getZoomHistory(); if (h != null) { Range[] range = h.setPreviousPoint(); if (range != null && range.length > 0 && range[0] != null) { ValueAxis dom = e.getChart().getXYPlot().getDomainAxis(); ValueAxis ran = e.getChart().getXYPlot().getRangeAxis(); ChartLogics.setZoomAxis(dom, range[0]); ChartLogics.setZoomAxis(ran, range[1]); } } }; break; case NEXT_ZOOM_HISTORY: newHandler = e -> { ZoomHistory h = e.getChartWrapper().getZoomHistory(); if (h != null) { Range[] range = h.setNextPoint(); if (range != null && range.length > 0 && range[0] != null) { ValueAxis dom = e.getChart().getXYPlot().getDomainAxis(); ValueAxis ran = e.getChart().getXYPlot().getRangeAxis(); ChartLogics.setZoomAxis(dom, range[0]); ChartLogics.setZoomAxis(ran, range[1]); } } }; break; case TITLE_REMOVER: newHandler = e -> { if (e.getEntity() instanceof TitleEntity) { TitleEntity te = (TitleEntity) e.getEntity(); te.getTitle().setVisible(false); } }; break; case AUTO_ZOOM_AXIS: newHandler = e -> { ValueAxis a = e.getAxis(); if (a != null) ChartLogics.autoAxis(a); }; break; case AUTO_ZOOM_OPPOSITE_AXIS: newHandler = e -> { if (e.getGesture().getEntity().equals(Entity.AXIS)) { e.getChartWrapper().autoRangeAxis(); e.getChartWrapper().autoDomainAxis(); } else if (e.getGesture().getEntity().equals(Entity.DOMAIN_AXIS)) e.getChartWrapper().autoRangeAxis(); else e.getChartWrapper().autoDomainAxis(); }; break; case SCROLL_AXIS: newHandler = e -> { ValueAxis axis = e.getAxis(); if (axis != null) { double diff = 0.03; if (e.getMouseEvent().isMouseWheelEvent()) { diff = -0.10 * e.getMouseEvent().getWheelRotation(); } ChartLogics.offsetAxis(axis, diff); } }; break; case SCROLL_AXIS_AND_AUTO_ZOOM: newHandler = e -> { ValueAxis axis = e.getAxis(); if (axis != null) { double diff = 0.03; if (e.getMouseEvent().isMouseWheelEvent()) { diff = -0.10 * e.getMouseEvent().getWheelRotation(); } ChartLogics.offsetAxis(axis, diff); if (e.getGesture().getEntity().equals(Entity.DOMAIN_AXIS)) e.getChartWrapper().autoRangeAxis(); else e.getChartWrapper().autoDomainAxis(); } }; break; case ZOOM_AXIS_INCLUDE_ZERO: newHandler = e -> { ValueAxis axis = e.getAxis(); if (axis != null) { double diff = 0.05; if (e.getMouseEvent().isMouseWheelEvent()) { diff = -0.10 * e.getMouseEvent().getWheelRotation(); } ChartLogics.zoomAxis(axis, diff, true); } }; break; case ZOOM_AXIS_CENTER: newHandler = e -> { ValueAxis axis = e.getAxis(); if (axis != null) { MouseEventWrapper p = e.getMouseEvent(); double diff = 0.05; if (e.getMouseEvent().isMouseWheelEvent()) { diff = -0.10 * p.getWheelRotation(); } // get data space coordinates Point2D point = e.getCoordinates(p.getX(), p.getY()); if (point != null) { // vertical ? Boolean orient = e.isVerticalAxis(axis); if (orient == null) return; else if (orient) ChartLogics.zoomAxis(axis, diff, point.getY()); else ChartLogics.zoomAxis(axis, diff, point.getX()); } } }; break; default: break; } if (newHandler == null) return null; else return new ChartGestureHandler(g, newHandler); }