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:com.juanhg.icecubes.IceCubesApplet.java

/**
 * Calculate the multiplicative zoom that must be applied to the minimum range of the
 * plot, to achieve that the point will be drawn inside the plot.
 * @param point Point that must be drawn inside the chart
 * @param supXLimit Superior X Limit of the plot
 * @param infXLimit Inferior X Limit of the plot
 * @param supYLimit Superior Y Limit of the plot
 * @param infYLimit Inferior Y Limit of the plot
 *///w w  w.  j  a v  a2  s . c om
public double getZoom(Point2D point, int supXLimit, int infXLimit, int supYLimit, int infYLimit) {
    int tempZoom = 1;

    while (point.getX() >= supXLimit * tempZoom || point.getY() >= supYLimit * tempZoom
            || point.getX() <= infXLimit * tempZoom || point.getY() <= infYLimit * tempZoom) {
        tempZoom = tempZoom * 2;
    }

    return tempZoom;
}

From source file:org.gumtree.vis.awt.time.TimePlotPanel.java

@Override
public void paintComponent(Graphics g) {
    int datasetIndex = 0;
    if (selectedDataset != null) {
        datasetIndex = getXYPlot().indexOf(selectedDataset);
    }//from   w  w  w .ja v  a 2  s. c o  m
    Point2D point = ChartMaskingUtilities.translateChartPoint(new Point2D.Double(getChartX(), getChartY()),
            getScreenDataArea(), getChart(), datasetIndex);
    setHorizontalTraceLocation((int) point.getX());
    setVerticalTraceLocation((int) point.getY());
    if (isPaused) {
        //         Graphics2D g2 = (Graphics2D) g.create();
        //         if (getHorizontalAxisTrace()) {
        //            super.drawHorizontalAxisTrace(g2, getHorizontalTraceLocation());
        //         }
        //         if (getVerticalAxisTrace()) {
        //            drawVerticalAxisTrace(g2, getVerticalTraceLocation());
        //         }
        //         if (isToolTipFollowerEnabled) {
        //            drawToolTipFollower(g2, horizontalTraceLocation, verticalTraceLocation);
        //         }
        //         return;
    }
    super.paintComponent(g);
}

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

/**
 * Scales to the given zoom level, 1.0 being 100%, centered on the 
 * given point./*w  ww  .  j  ava2  s.  com*/
 * 
 * @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) {
    float oldZoom = this.currentZoom;
    this.currentZoom = Math.max(level, 0.99f);
    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);

    // 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:pipeline.parameter_cell_views.FloatRangeSlider.java

/**
 * Translates MouseEvent screen coordinates to coordinates in chart units
 * /*from   ww w.  j ava 2  s. c o  m*/
 * @param e
 *            MouseEvent containing coordinates of interest
 * @return Chart coordinates (evaluated as double)
 */
private @Nullable Point2D getChartCoordinates(MouseEvent e) {
    if (chartPanel.getChartRenderingInfo().getChartArea().getHeight() == 0) {
        Utils.log("Cannot translate to chart coordinates", LogLevel.DEBUG);
        return null;
    }
    int mouseX = e.getX();
    int mouseY = e.getY();
    Utils.log("x = " + mouseX + ", y = " + mouseY, LogLevel.DEBUG);
    Point2D p = chartPanel.translateScreenToJava2D(new Point(mouseX, mouseY));
    XYPlot plot = (XYPlot) chart.getPlot();
    Rectangle2D plotArea = this.chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
    ValueAxis domainAxis = plot.getDomainAxis();
    RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
    ValueAxis rangeAxis = plot.getRangeAxis();
    RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
    double chartX = domainAxis.java2DToValue(p.getX(), plotArea, domainAxisEdge);
    double chartY = rangeAxis.java2DToValue(p.getY(), plotArea, rangeAxisEdge);
    return new Point2D.Double(chartX, chartY);
}

From source file:edu.dlnu.liuwenpeng.render.XYBarRenderer.java

/**
 * Draws an item label.  This method is provided as an alternative to
 * {@link #drawItemLabel(Graphics2D, PlotOrientation, XYDataset, int, int, 
 * double, double, boolean)} so that the bar can be used to calculate the 
 * label anchor point. //  w w w. j av  a  2  s  .  co m
 * 
 * @param g2  the graphics device.
 * @param dataset  the dataset.
 * @param series  the series index.
 * @param item  the item index.
 * @param plot  the plot.
 * @param generator  the label generator (<code>null</code> permitted, in 
 *         which case the method does nothing, just returns).
 * @param bar  the bar.
 * @param negative  a flag indicating a negative value.
 */
protected void drawItemLabel(Graphics2D g2, XYDataset dataset, int series, int item, XYPlot plot,
        XYItemLabelGenerator generator, Rectangle2D bar, boolean negative) {

    if (generator == null) {
        return; // nothing to do
    }
    String label = generator.generateLabel(dataset, series, item);
    if (label == null) {
        return; // nothing to do   
    }

    Font labelFont = getItemLabelFont(series, item);
    g2.setFont(labelFont);
    Paint paint = getItemLabelPaint(series, item);
    g2.setPaint(paint);

    // find out where to place the label...
    ItemLabelPosition position = null;
    if (!negative) {
        position = getPositiveItemLabelPosition(series, item);
    } else {
        position = getNegativeItemLabelPosition(series, item);
    }

    // work out the label anchor point...
    Point2D anchorPoint = calculateLabelAnchorPoint(position.getItemLabelAnchor(), bar, plot.getOrientation());

    if (isInternalAnchor(position.getItemLabelAnchor())) {
        Shape bounds = TextUtilities.calculateRotatedStringBounds(label, g2, (float) anchorPoint.getX(),
                (float) anchorPoint.getY(), position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());

        if (bounds != null) {
            if (!bar.contains(bounds.getBounds2D())) {
                if (!negative) {
                    position = getPositiveItemLabelPositionFallback();
                } else {
                    position = getNegativeItemLabelPositionFallback();
                }
                if (position != null) {
                    anchorPoint = calculateLabelAnchorPoint(position.getItemLabelAnchor(), bar,
                            plot.getOrientation());
                }
            }
        }

    }

    if (position != null) {
        TextUtilities.drawRotatedString(label, g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(), position.getRotationAnchor());
    }
}

From source file:pt.lsts.neptus.plugins.sunfish.awareness.SituationAwareness.java

public void paintLabels(Graphics2D g, StateRenderer2D renderer) {
    g.setFont(new Font("Arial", Font.PLAIN, 11));

    for (AssetTrack track : assets.values()) {
        AssetPosition p = track.getLatest(newestTimestampSelection);

        if (p == null || hiddenPosTypes.contains(p.getType()))
            continue;

        if (p.getTimestamp() < oldestTimestampSelection || p.getTimestamp() > newestTimestampSelection)
            continue;

        Point2D pt = renderer.getScreenPosition(p.getLoc());
        g.setColor(track.getColor());/* www. ja  v  a 2s. c om*/

        g.setColor(Color.black);
        String name = p.getAssetName();//assetProperties.containsKey(p.getAssetName()) ? assetProperties.get(p.getAssetName()).friendly : p.getAssetName();
        g.drawString(
                name + " ("
                        + DateTimeUtil.milliSecondsToFormatedString(
                                System.currentTimeMillis() - p.getTimestamp())
                        + ")",
                (int) (pt.getX() + 13), (int) (pt.getY() + 5));
    }
}

From source file:edu.dlnu.liuwenpeng.render.NewXYBarRenderer.java

/**    
* Draws an item label.  This method is provided as an alternative to    
* {@link #drawItemLabel(Graphics2D, PlotOrientation, XYDataset, int, int,     
* double, double, boolean)} so that the bar can be used to calculate the     
* label anchor point.     /*from   w  w w .  ja  v a 2 s .c  om*/
*     
* @param g2  the graphics device.    
* @param dataset  the dataset.    
* @param series  the series index.    
* @param item  the item index.    
* @param plot  the plot.    
* @param generator  the label generator (<code>null</code> permitted, in     
*         which case the method does nothing, just returns).    
* @param bar  the bar.    
* @param negative  a flag indicating a negative value.    
*/
protected void drawItemLabel(Graphics2D g2, XYDataset dataset, int series, int item, XYPlot plot,
        XYItemLabelGenerator generator, Rectangle2D bar, boolean negative) {

    if (generator == null) {
        return; // nothing to do    
    }
    String label = generator.generateLabel(dataset, series, item);
    if (label == null) {
        return; // nothing to do       
    }

    Font labelFont = getItemLabelFont(series, item);
    g2.setFont(labelFont);
    Paint paint = getItemLabelPaint(series, item);
    g2.setPaint(paint);

    // find out where to place the label...    
    ItemLabelPosition position = null;
    if (!negative) {
        position = getPositiveItemLabelPosition(series, item);
    } else {
        position = getNegativeItemLabelPosition(series, item);
    }

    // work out the label anchor point...    
    Point2D anchorPoint = calculateLabelAnchorPoint(position.getItemLabelAnchor(), bar, plot.getOrientation());

    if (isInternalAnchor(position.getItemLabelAnchor())) {
        Shape bounds = TextUtilities.calculateRotatedStringBounds(label, g2, (float) anchorPoint.getX(),
                (float) anchorPoint.getY(), position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());

        if (bounds != null) {
            if (!bar.contains(bounds.getBounds2D())) {
                if (!negative) {
                    position = getPositiveItemLabelPositionFallback();
                } else {
                    position = getNegativeItemLabelPositionFallback();
                }
                if (position != null) {
                    anchorPoint = calculateLabelAnchorPoint(position.getItemLabelAnchor(), bar,
                            plot.getOrientation());
                }
            }
        }

    }

    if (position != null) {
        TextUtilities.drawRotatedString(label, g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(), position.getRotationAnchor());
    }
}

From source file:com.net2plan.gui.utils.viewEditTopolTables.specificTables.AdvancedJTable_node.java

private List<JComponent> getExtraOptions(final int row, final Object itemId) {
    List<JComponent> options = new LinkedList<JComponent>();
    final int numRows = model.getRowCount();
    final List<Node> tableVisibleNodes = getVisibleElementsInTable();

    if (itemId != null) {
        JMenuItem switchCoordinates_thisNode = new JMenuItem("Switch node coordinates from (x,y) to (y,x)");

        switchCoordinates_thisNode.addActionListener(new ActionListener() {
            @Override// w  w  w  .  ja  va  2s.  c  om
            public void actionPerformed(ActionEvent e) {
                NetPlan netPlan = callback.getDesign();
                Node node = netPlan.getNodeFromId((long) itemId);
                Point2D currentPosition = node.getXYPositionMap();
                node.setXYPositionMap(new Point2D.Double(currentPosition.getY(), currentPosition.getX()));
                callback.updateVisualizationAfterChanges(Sets.newHashSet(NetworkElementType.NODE));
                callback.runCanvasOperation(ITopologyCanvas.CanvasOperation.ZOOM_ALL);
                callback.getUndoRedoNavigationManager().addNetPlanChange();
            }
        });

        options.add(switchCoordinates_thisNode);

        JMenuItem xyPositionFromAttributes_thisNode = new JMenuItem("Set node coordinates from attributes");

        xyPositionFromAttributes_thisNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NetPlan netPlan = callback.getDesign();

                Set<String> attributeSet = new LinkedHashSet<String>();
                Node node = netPlan.getNodeFromId((long) itemId);
                attributeSet.addAll(node.getAttributes().keySet());

                try {
                    if (attributeSet.isEmpty())
                        throw new Exception("No attribute to select");

                    final JComboBox latSelector = new WiderJComboBox();
                    final JComboBox lonSelector = new WiderJComboBox();
                    for (String attribute : attributeSet) {
                        latSelector.addItem(attribute);
                        lonSelector.addItem(attribute);
                    }

                    JPanel pane = new JPanel(new MigLayout("", "[][grow]", "[][]"));
                    pane.add(new JLabel("X-coordinate / Longitude: "));
                    pane.add(lonSelector, "growx, wrap");
                    pane.add(new JLabel("Y-coordinate / Latitude: "));
                    pane.add(latSelector, "growx, wrap");

                    while (true) {
                        int result = JOptionPane.showConfirmDialog(null, pane,
                                "Please select the attributes for coordinates", JOptionPane.OK_CANCEL_OPTION,
                                JOptionPane.QUESTION_MESSAGE);
                        if (result != JOptionPane.OK_OPTION)
                            return;

                        try {
                            String latAttribute = latSelector.getSelectedItem().toString();
                            String lonAttribute = lonSelector.getSelectedItem().toString();

                            node.setXYPositionMap(
                                    new Point2D.Double(Double.parseDouble(node.getAttribute(lonAttribute)),
                                            Double.parseDouble(node.getAttribute(latAttribute))));
                            callback.updateVisualizationAfterChanges(Sets.newHashSet(NetworkElementType.NODE));
                            callback.getUndoRedoNavigationManager().addNetPlanChange();
                            break;
                        } catch (Throwable ex) {
                            ErrorHandling.showErrorDialog(ex.getMessage(),
                                    "Error retrieving coordinates from attributes");
                            break;
                        }
                    }
                    callback.runCanvasOperation(ITopologyCanvas.CanvasOperation.ZOOM_ALL);
                } catch (Throwable ex) {
                    ErrorHandling.showErrorDialog(ex.getMessage(),
                            "Error retrieving coordinates from attributes");
                }
            }
        });

        options.add(xyPositionFromAttributes_thisNode);

        JMenuItem nameFromAttribute_thisNode = new JMenuItem("Set node name from attribute");
        nameFromAttribute_thisNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NetPlan netPlan = callback.getDesign();

                Set<String> attributeSet = new LinkedHashSet<String>();
                long nodeId = (long) itemId;
                attributeSet.addAll(netPlan.getNodeFromId(nodeId).getAttributes().keySet());

                try {
                    if (attributeSet.isEmpty())
                        throw new Exception("No attribute to select");

                    final JComboBox selector = new WiderJComboBox();
                    for (String attribute : attributeSet)
                        selector.addItem(attribute);

                    JPanel pane = new JPanel(new MigLayout("", "[][grow]", "[]"));
                    pane.add(new JLabel("Name: "));
                    pane.add(selector, "growx, wrap");

                    while (true) {
                        int result = JOptionPane.showConfirmDialog(null, pane,
                                "Please select the attribute for name", JOptionPane.OK_CANCEL_OPTION,
                                JOptionPane.QUESTION_MESSAGE);
                        if (result != JOptionPane.OK_OPTION)
                            return;

                        try {
                            String name = selector.getSelectedItem().toString();
                            netPlan.getNodeFromId(nodeId)
                                    .setName(netPlan.getNodeFromId(nodeId).getAttribute(name));
                            callback.updateVisualizationAfterChanges(Sets.newHashSet(NetworkElementType.NODE));
                            callback.getUndoRedoNavigationManager().addNetPlanChange();

                            break;
                        } catch (Throwable ex) {
                            ErrorHandling.showErrorDialog(ex.getMessage(),
                                    "Error retrieving name from attribute");
                            break;
                        }
                    }
                } catch (Throwable ex) {
                    ErrorHandling.showErrorDialog(ex.getMessage(), "Error retrieving name from attribute");
                }
            }
        });

        options.add(nameFromAttribute_thisNode);
    }

    if (numRows > 1) {
        if (!options.isEmpty())
            options.add(new JPopupMenu.Separator());

        JMenuItem switchCoordinates_allNodes = new JMenuItem(
                "Switch all table node coordinates from (x,y) to (y,x)");

        switchCoordinates_allNodes.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (Node n : tableVisibleNodes) {
                    Point2D currentPosition = n.getXYPositionMap();
                    double newX = currentPosition.getY();
                    double newY = currentPosition.getX();
                    Point2D newPosition = new Point2D.Double(newX, newY);
                    n.setXYPositionMap(newPosition);
                }
                callback.updateVisualizationAfterChanges(Sets.newHashSet(NetworkElementType.NODE));
                callback.runCanvasOperation(ITopologyCanvas.CanvasOperation.ZOOM_ALL);
                callback.getUndoRedoNavigationManager().addNetPlanChange();
            }
        });

        options.add(switchCoordinates_allNodes);

        JMenuItem xyPositionFromAttributes_allNodes = new JMenuItem(
                "Set all table node coordinates from attributes");

        xyPositionFromAttributes_allNodes.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Set<String> attributeSet = new LinkedHashSet<String>();
                for (Node node : tableVisibleNodes)
                    attributeSet.addAll(node.getAttributes().keySet());

                try {
                    if (attributeSet.isEmpty())
                        throw new Exception("No attribute to select");

                    final JComboBox latSelector = new WiderJComboBox();
                    final JComboBox lonSelector = new WiderJComboBox();
                    for (String attribute : attributeSet) {
                        latSelector.addItem(attribute);
                        lonSelector.addItem(attribute);
                    }

                    JPanel pane = new JPanel(new MigLayout("", "[][grow]", "[][]"));
                    pane.add(new JLabel("X-coordinate / Longitude: "));
                    pane.add(lonSelector, "growx, wrap");
                    pane.add(new JLabel("Y-coordinate / Latitude: "));
                    pane.add(latSelector, "growx, wrap");

                    while (true) {
                        int result = JOptionPane.showConfirmDialog(null, pane,
                                "Please select the attributes for coordinates", JOptionPane.OK_CANCEL_OPTION,
                                JOptionPane.QUESTION_MESSAGE);
                        if (result != JOptionPane.OK_OPTION)
                            return;

                        try {
                            String latAttribute = latSelector.getSelectedItem().toString();
                            String lonAttribute = lonSelector.getSelectedItem().toString();

                            for (Node node : tableVisibleNodes)
                                node.setXYPositionMap(
                                        new Point2D.Double(Double.parseDouble(node.getAttribute(lonAttribute)),
                                                Double.parseDouble(node.getAttribute(latAttribute))));
                            callback.updateVisualizationAfterChanges(Sets.newHashSet(NetworkElementType.NODE));
                            callback.getUndoRedoNavigationManager().addNetPlanChange();
                            break;
                        } catch (Throwable ex) {
                            ErrorHandling.showErrorDialog(ex.getMessage(),
                                    "Error retrieving coordinates from attributes");
                            break;
                        }
                    }
                    callback.runCanvasOperation(ITopologyCanvas.CanvasOperation.ZOOM_ALL);
                } catch (Throwable ex) {
                    ErrorHandling.showErrorDialog(ex.getMessage(),
                            "Error retrieving coordinates from attributes");
                }
            }
        });

        options.add(xyPositionFromAttributes_allNodes);

        JMenuItem nameFromAttribute_allNodes = new JMenuItem("Set all table node names from attribute");
        nameFromAttribute_allNodes.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                Set<String> attributeSet = new LinkedHashSet<String>();
                for (Node node : tableVisibleNodes)
                    attributeSet.addAll(node.getAttributes().keySet());

                try {
                    if (attributeSet.isEmpty())
                        throw new Exception("No attribute to select");

                    final JComboBox selector = new WiderJComboBox();
                    for (String attribute : attributeSet)
                        selector.addItem(attribute);

                    JPanel pane = new JPanel(new MigLayout("", "[][grow]", "[]"));
                    pane.add(new JLabel("Name: "));
                    pane.add(selector, "growx, wrap");

                    while (true) {
                        int result = JOptionPane.showConfirmDialog(null, pane,
                                "Please select the attribute for name", JOptionPane.OK_CANCEL_OPTION,
                                JOptionPane.QUESTION_MESSAGE);
                        if (result != JOptionPane.OK_OPTION)
                            return;

                        try {
                            String name = selector.getSelectedItem().toString();

                            for (Node node : tableVisibleNodes)
                                node.setName(node.getAttribute(name) != null ? node.getAttribute(name) : "");
                            callback.updateVisualizationAfterChanges(Sets.newHashSet(NetworkElementType.NODE));
                            callback.getUndoRedoNavigationManager().addNetPlanChange();
                            break;
                        } catch (Throwable ex) {
                            ErrorHandling.showErrorDialog(ex.getMessage(),
                                    "Error retrieving name from attribute");
                            break;
                        }
                    }
                } catch (Throwable ex) {
                    ErrorHandling.showErrorDialog(ex.getMessage(), "Error retrieving name from attribute");
                }
            }
        });

        options.add(nameFromAttribute_allNodes);
    }

    return options;
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

public Point2D screenToDataCoords(Point2D p) {

    double x = thePlot.getDomainAxis().java2DToValue(p.getX(), data_area, thePlot.getDomainAxisEdge());
    double y = thePlot.getRangeAxis().java2DToValue(p.getY(), data_area, thePlot.getRangeAxisEdge());
    return new Point2D.Double(x, y);
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

public Point2D dataToScreenCoords(Point2D p) {

    double x = thePlot.getDomainAxis().valueToJava2D(p.getX(), data_area, thePlot.getDomainAxisEdge());
    double y = thePlot.getRangeAxis().valueToJava2D(p.getY(), data_area, thePlot.getRangeAxisEdge());
    return new Point2D.Double(x, y);
}