Example usage for java.awt.geom Point2D getX

List of usage examples for java.awt.geom Point2D getX

Introduction

In this page you can find the example usage for java.awt.geom Point2D getX.

Prototype

public abstract double getX();

Source Link

Document

Returns the X coordinate of this Point2D in double precision.

Usage

From source file:org.gumtree.vis.awt.JChartPanel.java

protected Point2D translateScreenToChart(Point2D point) {
    EntityCollection entities = getChartRenderingInfo().getEntityCollection();
    ChartEntity entity = entities.getEntity(point.getX(), point.getY());
    if (entity instanceof XYItemEntity) {
        XYDataset dataset = ((XYItemEntity) entity).getDataset();
        int item = ((XYItemEntity) entity).getItem();
        double chartX = dataset.getXValue(0, item);
        double chartY = dataset.getYValue(0, item);
        //           double chartZ = ((XYZDataset) dataset).getZValue(0, item);
        return new Point2D.Double(chartX, chartY);
    }/*from   ww  w .j  av a2s .c  o m*/
    return null;
}

From source file:org.gumtree.vis.plot1d.Plot1DPanel.java

private int followDomainTrace(int seriesIndex, int domainLocation) {
    if (seriesIndex >= getDataset().getSeriesCount()) {
        return -1;
    }// w w  w . j a  v a2  s.c  om
    double xInChart = ChartMaskingUtilities.translateScreenX(domainLocation, getScreenDataArea(), getChart());
    XYDataset dataset = getChart().getXYPlot().getDataset();
    IXYErrorDataset patternDataset = (IXYErrorDataset) dataset;
    int itemIndex = patternDataset.getItemFromX(seriesIndex, xInChart);
    if (itemIndex >= 0 && itemIndex < patternDataset.getItemCount(seriesIndex)) {
        double chartX = patternDataset.getXValue(seriesIndex, itemIndex);
        double chartY = patternDataset.getYValue(seriesIndex, itemIndex);
        Point2D axisTrace = ChartMaskingUtilities.translateChartPoint(new Point2D.Double(chartX, chartY),
                getScreenDataArea(), getChart());
        setChartX(chartX);
        setChartY(chartY);
        setSeriesIndex(seriesIndex);
        setItemIndex(itemIndex);
        if (dataset instanceof IXYErrorDataset) {
            setChartError(((IXYErrorDataset) dataset).getYError(seriesIndex, itemIndex));

        }
        setHorizontalTraceLocation((int) axisTrace.getX());
        setVerticalTraceLocation((int) axisTrace.getY());
        if (getHorizontalAxisTrace() || getVerticalAxisTrace() || isToolTipFollowerEnabled()) {
            repaint();
        }
        return seriesIndex;
    }
    return -1;
}

From source file:com.net2plan.libraries.GraphUtils.java

/** Computes the Haversine distance between two points, that is, the shortest distance over the Earth's surface.
 * //from ww  w  .jav  a  2  s . c om
 * <p><b>Important</b>: It is assumed an Earth's radius equal to {@link com.net2plan.utils.Constants#EARTH_RADIUS_IN_KM Constants.EARTH_RADIUS_IN_KM }</p>
 * 
 * <p><b>Important</b>: Coordinates are assumed to be given in degress, conversions to radians are made internally.</p>
 * 
 * @param point1 Point 1 (x-coord is equal to longitude, y-coord is equal to latitude)
 * @param point2 Point 2 (x-coord is equal to longitude, y-coord is equal to latitude)
 * @return Haversine distance between two points
 * @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">Calculate distance, bearing and more between Latitude/Longitude points</a> */
public static double computeHaversineDistanceInKm(Point2D point1, Point2D point2) {
    double lon1 = point1.getX();
    double lat1 = point1.getY();
    double lon2 = point2.getX();
    double lat2 = point2.getY();

    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double sindLat = Math.sin(dLat / 2);
    double sindLon = Math.sin(dLon / 2);
    double a = Math.pow(sindLat, 2)
            + Math.pow(sindLon, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return Constants.EARTH_RADIUS_IN_KM * c;
}

From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java

private boolean _updateMainTraceInfoForCandlestick(Point2D point) {
    if (point == null) {
        return false;
    }//w w w .  j a v  a  2  s  . c  om

    final ChartPanel chartPanel = this.chartJDialog.getChartPanel();
    // Top most plot.
    final XYPlot plot = this.chartJDialog.getPlot();

    final org.jfree.data.xy.DefaultHighLowDataset defaultHighLowDataset = (org.jfree.data.xy.DefaultHighLowDataset) plot
            .getDataset();

    // I also not sure why. This is what are being done in Mouse Listener Demo 4.
    //
    // Don't use it. It will cause us to lose precision.
    //final Point2D p2 = chartPanel.translateScreenToJava2D((Point)point);

    /* Try to get correct main chart area. */
    final Rectangle2D _plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0)
            .getDataArea();

    final ValueAxis domainAxis = plot.getDomainAxis();
    final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
    final ValueAxis rangeAxis = plot.getRangeAxis();
    final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
    // Don't use it. It will cause us to lose precision.
    //final double coordinateX = domainAxis.java2DToValue(p2.getX(), _plotArea,
    //        domainAxisEdge);
    final double coordinateX = domainAxis.java2DToValue(point.getX(), _plotArea, domainAxisEdge);
    //double coordinateY = rangeAxis.java2DToValue(mousePoint2.getY(), plotArea,
    //        rangeAxisEdge);

    int low = 0;
    int high = defaultHighLowDataset.getItemCount(0) - 1;
    Date date = new Date((long) coordinateX);
    final long time = date.getTime();
    long bestDistance = Long.MAX_VALUE;
    int bestMid = 0;

    while (low <= high) {
        int mid = (low + high) >>> 1;

        final Date d = defaultHighLowDataset.getXDate(0, mid);
        final long search = d.getTime();
        final long cmp = search - time;

        if (cmp < 0) {
            low = mid + 1;
        } else if (cmp > 0) {
            high = mid - 1;
        } else {
            bestDistance = 0;
            bestMid = mid;
            break;
        }

        final long abs_cmp = Math.abs(cmp);
        if (abs_cmp < bestDistance) {
            bestDistance = abs_cmp;
            bestMid = mid;
        }
    }

    final double xValue = defaultHighLowDataset.getXDate(0, bestMid).getTime();
    final double yValue = defaultHighLowDataset.getCloseValue(0, bestMid);
    final double xJava2D = domainAxis.valueToJava2D(xValue, _plotArea, domainAxisEdge);
    final double yJava2D = rangeAxis.valueToJava2D(yValue, _plotArea, rangeAxisEdge);

    final int tmpIndex = bestMid;
    // translateJava2DToScreen will internally convert Point2D.Double to Point.
    final Point2D tmpPoint = chartPanel.translateJava2DToScreen(new Point2D.Double(xJava2D, yJava2D));
    this.mainDrawArea.setRect(_plotArea);

    if (this.mainDrawArea.contains(tmpPoint)) {
        // 0 indicates main plot.
        this.mainTraceInfo = TraceInfo.newInstance(tmpPoint, 0, 0, tmpIndex);
        return true;
    }
    return false;
}

From source file:org.gumtree.vis.awt.JChartPanel.java

private void selectText(double xNew, double yNew) {
    for (Entry<Rectangle2D, String> item : textContentMap.entrySet()) {
        Rectangle2D rect = item.getKey();
        Point2D screenPoint = ChartMaskingUtilities.translateChartPoint(
                new Point2D.Double(rect.getMinX(), rect.getMinY()), getScreenDataArea(), getChart());
        Rectangle2D screenRect = new Rectangle2D.Double(screenPoint.getX(), screenPoint.getY() - 15,
                rect.getWidth(), rect.getHeight());
        if (screenRect.contains(xNew, yNew)) {
            if (selectedTextWrapper == rect) {
                selectedTextWrapper = null;
            } else {
                selectedTextWrapper = rect;
                setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                return;
            }//from   w w w  .  jav a 2 s.c o  m
        }
    }
    if (selectedTextWrapper != null) {
        selectedTextWrapper = null;
    }
}

From source file:edu.purdue.cc.bionet.ui.GraphVisualizer.java

/**
 * Scales to the given zoom level, 1.0 being 100%, centered on the 
 * given point.//from w w w.  ja  v a  2 s  . c om
 * 
 * @param level The level to zoom to.
 * @param center The center point for the scaling operation.
 * @return The new zoom level.
 */
public float scaleTo(float level, Point2D center) {
    if (this.scrollPane == null)
        return 1.0f;
    float oldZoom = this.currentZoom;
    this.currentZoom = Math.max(minimumZoom, level);
    //      this.absoluteViewScaler.scale( this, level, center );
    Dimension viewSize;
    if (level < 1.0f) {
        viewSize = this.scrollPane.getSize();
    } else {
        viewSize = this.scrollPane.getViewport().getExtentSize();
    }
    Dimension newSize = new Dimension((int) (viewSize.width * currentZoom),
            (int) (viewSize.height * currentZoom));
    this.setPreferredSize(newSize);
    this.setSize(newSize);
    //      new LayoutScaler( this.getGraphLayout( )).setSize( newSize );
    if (Float.compare(level, 1.0f) <= 0)
        this.center();

    // translate the new view position so the mouse is in the same place
    // on the scaled view.
    JViewport vp = this.scrollPane.getViewport();
    double centerX = center.getX();
    double centerY = center.getY();
    double viewPortMouseX = centerX - vp.getViewPosition().getX();
    double viewPortMouseY = centerY - vp.getViewPosition().getY();
    centerX *= currentZoom / oldZoom;
    centerY *= currentZoom / oldZoom;
    viewPortMouseX = centerX - viewPortMouseX;
    viewPortMouseY = centerY - viewPortMouseY;
    vp.setViewPosition(new Point((int) viewPortMouseX, (int) viewPortMouseY));

    return this.currentZoom;
}

From source file:org.gumtree.vis.awt.JChartPanel.java

private void findSelectedMarker(Point point) {
    Line2D marker = null;/*from www.j a v a  2s.c o m*/
    double distance = Double.MAX_VALUE;
    double cDis;
    for (Entry<Line2D, Color> entry : domainMarkerMap.entrySet()) {
        Line2D line = entry.getKey();
        Point2D p2 = line.getP2();
        double height = p2.getY();
        cDis = Double.MAX_VALUE;
        if (height < 1e-4) {
            double xScr = ChartMaskingUtilities.translateChartPoint(p2, getScreenDataArea(), getChart()).getX();
            cDis = Math.abs(point.getX() - xScr);
        } else {
            Point2D newP2 = ChartMaskingUtilities.translateChartPoint(p2, getScreenDataArea(), getChart());
            if (newP2.getY() < point.getY()) {
                cDis = Math.abs(point.getX() - newP2.getX());
            } else {
                cDis = newP2.distance(point);
            }
        }
        if (cDis <= distance) {
            distance = cDis;
            marker = line;
        }
    }
    for (Entry<Line2D, Color> entry : rangeMarkerMap.entrySet()) {
        Line2D line = entry.getKey();
        Point2D p1 = line.getP1();
        Point2D p2 = line.getP2();
        double width = p2.getX();
        cDis = Double.MAX_VALUE;
        if (width < 1e-4) {
            double yScr = ChartMaskingUtilities.translateChartPoint(p1, getScreenDataArea(), getChart()).getY();
            cDis = Math.abs(point.getY() - yScr);
        } else {
            Point2D newP2 = ChartMaskingUtilities.translateChartPoint(p2, getScreenDataArea(), getChart());
            if (newP2.getX() > point.getX()) {
                cDis = Math.abs(point.getY() - newP2.getY());
            } else {
                cDis = newP2.distance(point);
            }
        }
        if (cDis <= distance) {
            distance = cDis;
            marker = line;
        }
    }
    for (Entry<Line2D, Color> entry : markerMap.entrySet()) {
        Line2D line = entry.getKey();
        Point2D p1 = line.getP1();
        p1 = ChartMaskingUtilities.translateChartPoint(p1, getScreenDataArea(), getChart());
        cDis = p1.distance(point);
        if (cDis <= distance) {
            distance = cDis;
            marker = line;
        }
    }
    if (distance < 5) {
        selectedMarker = marker;
    } else {
        selectedMarker = null;
    }
}

From source file:lcmc.gui.ResourceGraph.java

/** Scale function. */
protected final ViewScalingControl getScalingControl() {
    return new ViewScalingControl() {
        void superScale(final VisualizationServer thisVV, final float amount, final Point2D from) {
            super.scale(thisVV, amount, from);
        }//from w  ww.j  av  a  2s.  c o  m

        @Override
        public void scale(final VisualizationServer thisVV, final float amount, final Point2D from) {
            final JScrollBar sbV = getScrollPane().getVerticalScrollBar();
            final JScrollBar sbH = getScrollPane().getHorizontalScrollBar();
            final Point2D last = posWithScrollbar(getLastPosition());
            final double fromX = from.getX() < last.getX() ? from.getX() : last.getX();
            final double fromY = from.getY() < last.getY() ? from.getY() : last.getY();
            final double width = getVisualizationViewer().getSize().getWidth();
            final double height = getVisualizationViewer().getSize().getHeight();
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    final Point2D prevPoint = getVisualizationViewer().getRenderContext()
                            .getMultiLayerTransformer().inverseTransform(Layer.VIEW, from);
                    final double scaledSoFar = getScaledSoFar();
                    float am = amount;
                    if (am < 1) {
                        if (scaledSoFar < 0.3) {
                            am = 1;
                        } else {
                            superScale(thisVV, 1 / am, new Point2D.Double(0, 0));
                        }
                    } else if (am > 1) {
                        if (scaledSoFar > 5) {
                            am = 1;
                        } else {
                            superScale(thisVV, 1 / am, new Point2D.Double(0, 0));
                        }
                    }
                    setScaledSoFar(scaledSoFar * am);
                    final Point2D p2 = getVisualizationViewer().getRenderContext().getMultiLayerTransformer()
                            .inverseTransform(Layer.VIEW, from);
                    final int valueY = (int) (sbV.getValue() + prevPoint.getY() - p2.getY());
                    sbV.setValue(valueY);
                    sbV.repaint();
                    final int valueX = (int) (sbH.getValue() + prevPoint.getX() - p2.getX());
                    sbH.setValue(valueX);
                    sbH.repaint();

                    thisVV.repaint();
                }
            });
        }
    };
}

From source file:spinworld.gui.RadarPlot.java

/**
 * Draws the label for one axis./* ww  w. j a  v  a 2  s .  com*/
 *
 * @param g2  the graphics device.
 * @param plotArea  whole plot drawing area (e.g. including space for labels)
 * @param plotDrawingArea  the plot drawing area (just spanning of axis)
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, Rectangle2D plotDrawingArea, double value,
        int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label = null;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    } else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    double angle = normalize(startAngle);

    Font font = getLabelFont();
    Point2D labelLocation;
    do {
        Rectangle2D labelBounds = font.getStringBounds(label, frc);
        LineMetrics lm = font.getLineMetrics(label, frc);
        double ascent = lm.getAscent();

        labelLocation = calculateLabelLocation(labelBounds, ascent, plotDrawingArea, startAngle);

        boolean leftOut = angle > 90 && angle < 270 && labelLocation.getX() < plotArea.getX();
        boolean rightOut = (angle < 90 || angle > 270)
                && labelLocation.getX() + labelBounds.getWidth() > plotArea.getX() + plotArea.getWidth();

        if (leftOut || rightOut) {
            font = font.deriveFont(font.getSize2D() - 1);
        } else {
            break;
        }
    } while (font.getSize() > 8);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(font);
    g2.drawString(label, (float) labelLocation.getX(), (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}

From source file:edu.pitt.dbmi.odie.ui.jfreechart.EnhancedChartComposite.java

public void mouseMove(MouseEvent event) {

    // handle axis trace
    if (this.horizontalAxisTrace || this.verticalAxisTrace) {
        this.horizontalTraceLineY = event.y;
        this.verticalTraceLineX = event.x;
        this.canvas.redraw();
    }/*  w  ww  . ja va2 s  .c o m*/

    // handle tool tips in a simple way
    if (this.displayToolTips) {
        String s = getToolTipText(event);
        if (s == null && this.canvas.getToolTipText() != null
                || s != null && !s.equals(this.canvas.getToolTipText()))
            this.canvas.setToolTipText(s);
    }

    // new entity code
    Point2D javaPoint = translateScreenToJava2D(new Point(event.x, event.y));
    ChartEntity entity = getEntityForLocation(javaPoint.getX(), javaPoint.getY());

    // handle zoom box

    if (this.zoomPoint != null) {
        boolean hZoom, vZoom;
        Rectangle scaledDataArea = getScreenDataArea(this.zoomPoint.x, this.zoomPoint.y);
        org.eclipse.swt.graphics.Point movingPoint = getPointInRectangle(event.x, event.y, scaledDataArea);
        if (this.orientation == PlotOrientation.HORIZONTAL) {
            hZoom = this.rangeZoomable;
            vZoom = this.domainZoomable;
        } else {
            hZoom = this.domainZoomable;
            vZoom = this.rangeZoomable;
        }
        if (hZoom && vZoom) {
            // selected rectangle shouldn't extend outside the data area...
            this.zoomRectangle = new Rectangle(this.zoomPoint.x, this.zoomPoint.y,
                    movingPoint.x - this.zoomPoint.x, movingPoint.y - this.zoomPoint.y);
        } else if (hZoom) {
            this.zoomRectangle = new Rectangle(this.zoomPoint.x, scaledDataArea.y,
                    movingPoint.x - this.zoomPoint.x, scaledDataArea.height);
        } else if (vZoom) {
            int ymax = Math.max(movingPoint.y, scaledDataArea.y);
            this.zoomRectangle = new Rectangle(scaledDataArea.x, this.zoomPoint.y, scaledDataArea.width,
                    ymax - this.zoomPoint.y);
        }
        this.canvas.redraw();
    } else if (this.selectionPoint != null) {
        Rectangle scaledDataArea = getScreenDataArea(this.selectionPoint.x, this.selectionPoint.y);

        org.eclipse.swt.graphics.Point movingPoint = getPointInRectangle(event.x, event.y, scaledDataArea);

        if (this.orientation == PlotOrientation.VERTICAL) {
            Point2D selectionPointJava = translateScreenToJava2D(
                    new Point(this.selectionPoint.x, scaledDataArea.y));
            this.selectionRectangle = new Rectangle((int) selectionPointJava.getX(),
                    (int) selectionPointJava.getY(), (int) ((movingPoint.x - this.selectionPoint.x) / scaleX),
                    (int) (scaledDataArea.height / scaleY));
        } else {
            int ymax = Math.max(movingPoint.y, scaledDataArea.y);

            Point2D selectionPointJava = translateScreenToJava2D(
                    new Point(scaledDataArea.x, this.selectionPoint.y));
            this.selectionRectangle = new Rectangle((int) selectionPointJava.getX(),
                    (int) selectionPointJava.getY(), (int) (scaledDataArea.width / scaleX),
                    (int) ((ymax - this.selectionPoint.y) / scaleY));
        }

        this.canvas.redraw();
    }

    Object[] listeners = this.chartMouseListeners.getListeners(ChartMouseListener.class);
    if (listeners.length == 0) {
        return;
    }

    // pass mouse move event if some ChartMouseListener are listening
    java.awt.event.MouseEvent mouseEvent = SWTUtils.toAwtMouseEvent(event);
    ChartMouseEvent chartEvent = new ChartMouseEvent(getChart(), mouseEvent, entity);
    for (int i = listeners.length - 1; i >= 0; i -= 1) {
        ((ChartMouseListener) listeners[i]).chartMouseMoved(chartEvent);
    }
}