Example usage for java.awt Shape getBounds2D

List of usage examples for java.awt Shape getBounds2D

Introduction

In this page you can find the example usage for java.awt Shape getBounds2D.

Prototype

public Rectangle2D getBounds2D();

Source Link

Document

Returns a high precision and more accurate bounding box of the Shape than the getBounds method.

Usage

From source file:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java

/**
 *
 * @param entity//from  w ww .j  a v  a 2s .  c o m
 * @param chartPanel
 * @param dataset
 * @param seriesIndex
 * @param itemIndex
 * @return
 */
public static Shape toView(Shape entity, ChartPanel chartPanel, Dataset dataset, int seriesIndex,
        int itemIndex) {
    if (dataset instanceof XYDataset) {
        XYDataset xyds = (XYDataset) dataset;
        double x1 = xyds.getXValue(seriesIndex, itemIndex);
        double y1 = xyds.getYValue(seriesIndex, itemIndex);
        AffineTransform toPosition = getModelToViewTransformXY(chartPanel, x1, y1);
        toPosition.concatenate(
                getTranslateInstance(-entity.getBounds2D().getCenterX(), -entity.getBounds2D().getCenterY()));
        return toPosition.createTransformedShape(entity);
    } else if (dataset instanceof CategoryDataset) {
        CategoryDataset cds = (CategoryDataset) dataset;
        double y1 = cds.getValue(seriesIndex, itemIndex).doubleValue();
        AffineTransform toPosition = getModelToViewTransformCategory(chartPanel, itemIndex, y1);
        toPosition.concatenate(
                getTranslateInstance(-entity.getBounds2D().getCenterX(), -entity.getBounds2D().getCenterY()));
        return toPosition.createTransformedShape(entity);
    }
    throw new IllegalArgumentException("Unsupported dataset type: " + dataset.getClass());
}

From source file:net.sf.maltcms.chromaui.charts.events.XYAnnotationAdder.java

/**
 *
 * @param s/*from  w w w . ja  va  2  s . c o  m*/
 * @return
 */
public Point2D getCenter(Shape s) {
    Rectangle2D r2 = s.getBounds2D();
    Point2D.Double p = new Point2D.Double(r2.getCenterX(), r2.getCenterY());
    return p;
}

From source file:edu.uci.ics.jung.visualization.picking.ShapePickSupport.java

/**
 * Retrieves the shape template for <code>e</code> and
 * transforms it according to the positions of its endpoints
 * in <code>layout</code>./*from  w  w w.  ja  va2  s  .  com*/
 * @param layout the <code>Layout</code> which specifies
 * <code>e</code>'s endpoints' positions
 * @param e the edge whose shape is to be returned
 * @return
 */
private Shape getTransformedEdgeShape(Layout<V, E> layout, E e) {
    Pair<V> pair = layout.getGraph().getEndpoints(e);
    V v1 = pair.getFirst();
    V v2 = pair.getSecond();
    boolean isLoop = v1.equals(v2);
    Point2D p1 = vv.getRenderContext().getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v1));
    Point2D p2 = vv.getRenderContext().getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v2));
    if (p1 == null || p2 == null)
        return null;
    float x1 = (float) p1.getX();
    float y1 = (float) p1.getY();
    float x2 = (float) p2.getX();
    float y2 = (float) p2.getY();

    // translate the edge to the starting vertex
    AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);

    Shape edgeShape = vv.getRenderContext().getEdgeShapeTransformer()
            .transform(Context.<Graph<V, E>, E>getInstance(vv.getGraphLayout().getGraph(), e));
    if (isLoop) {
        // make the loops proportional to the size of the vertex
        Shape s2 = vv.getRenderContext().getVertexShapeTransformer().transform(v2);
        Rectangle2D s2Bounds = s2.getBounds2D();
        xform.scale(s2Bounds.getWidth(), s2Bounds.getHeight());
        // move the loop so that the nadir is centered in the vertex
        xform.translate(0, -edgeShape.getBounds2D().getHeight() / 2);
    } else {
        float dx = x2 - x1;
        float dy = y2 - y1;
        // rotate the edge to the angle between the vertices
        double theta = Math.atan2(dy, dx);
        xform.rotate(theta);
        // stretch the edge to span the distance between the vertices
        float dist = (float) Math.sqrt(dx * dx + dy * dy);
        xform.scale(dist, 1.0f);
    }

    // transform the edge to its location and dimensions
    edgeShape = xform.createTransformedShape(edgeShape);
    return edgeShape;
}

From source file:net.sf.maltcms.chromaui.charts.renderer.XYNoBlockRenderer.java

@Override
protected void addEntity(EntityCollection entities, Shape area, XYDataset dataset, int series, int item,
        double entityX, double entityY) {
    if (!getItemCreateEntity(series, item)) {
        return;//  w  ww.  j  a va2s.  co  m
    }
    Shape hotspot = area;
    if (hotspot == null) {
        double r = getDefaultEntityRadius();
        double w = r * 2;
        if (getPlot().getOrientation() == PlotOrientation.VERTICAL) {
            hotspot = new Ellipse2D.Double(entityX - r, entityY - r, w, w);
        } else {
            hotspot = new Ellipse2D.Double(entityY - r, entityX - r, w, w);
        }
    } else if (hotspot.getBounds2D().getWidth() < 5.0 || hotspot.getBounds2D().getHeight() < 5.0) {
        if (getPlot().getOrientation() == PlotOrientation.VERTICAL) {
            hotspot = new Rectangle2D.Double(entityX - 2.5, entityY - 2.5, 5, 5);
        } else {
            hotspot = new Rectangle2D.Double(entityY - 2.5, entityX - 2.5, 5, 5);
        }
    }
    String tip = null;
    XYToolTipGenerator generator = getToolTipGenerator(series, item);
    if (generator != null) {
        tip = generator.generateToolTip(dataset, series, item);
    }
    String url = null;
    if (getURLGenerator() != null) {
        url = getURLGenerator().generateURL(dataset, series, item);
    }
    XYItemEntity entity = new XYItemEntity(hotspot, dataset, series, item, tip, url);
    entities.add(entity);
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.legend.CustomLegendGraphic.java

@Override
public void draw(Graphics2D g2, Rectangle2D area) {

    area = trimMargin(area);/*from ww  w.  jav  a  2 s  .c  om*/
    drawBorder(g2, area);
    area = trimBorder(area);
    area = trimPadding(area);

    if (isLineVisible()) {
        Point2D location = RectangleAnchor.coordinates(area, getShapeLocation());
        Shape aLine = ShapeUtilities.createTranslatedShape(getLine(), getShapeAnchor(), location.getX(),
                location.getY());
        g2.setPaint(getLinePaint());
        g2.setStroke(getLineStroke());
        g2.draw(aLine);
    }

    if (isShapeVisible()) {
        Point2D location = RectangleAnchor.coordinates(area, getShapeLocation());

        Shape s = ShapeUtilities.createTranslatedShape(getShape(), getShapeAnchor(), location.getX(),
                location.getY());
        if (isShapeFilled()) {
            Paint p = getFillPaint();
            if (p instanceof GradientPaint) {
                GradientPaint gp = (GradientPaint) getFillPaint();
                p = getFillPaintTransformer().transform(gp, s);
            } else if (p instanceof LinearGradientPaint) {
                LinearGradientPaint gradient = (LinearGradientPaint) p;
                Rectangle2D bounds = s.getBounds2D();
                p = getTranslatedLinearGradientPaint(gradient,
                        new Point2D.Double(bounds.getMinX(), bounds.getMinY()),
                        new Point2D.Double(bounds.getMaxX(), bounds.getMaxY()), false);
            }
            g2.setPaint(p);
            g2.fill(s);
        }
        if (isShapeOutlineVisible()) {
            g2.setPaint(getOutlinePaint());
            g2.setStroke(getOutlineStroke());
            g2.draw(s);
        }
    }

}

From source file:org.csa.rstb.dat.toolviews.HaAlphaPlotPanel.java

private ChartPanel createChartPanel(JFreeChart chart) {
    densityPlotDisplay = new ChartPanel(chart);

    MaskSelectionToolSupport maskSelectionToolSupport = new MaskSelectionToolSupport(this, densityPlotDisplay,
            "scatter_plot_area", "Mask generated from selected scatter plot area", Color.RED,
            PlotAreaSelectionTool.AreaType.ELLIPSE) {
        @Override// w  w  w  .j a va2 s . c  o m
        protected String createMaskExpression(PlotAreaSelectionTool.AreaType areaType, Shape shape) {
            Rectangle2D bounds = shape.getBounds2D();
            return createMaskExpression(bounds.getCenterX(), bounds.getCenterY(), 0.5 * bounds.getWidth(),
                    0.5 * bounds.getHeight());
        }

        protected String createMaskExpression(double x0, double y0, double dx, double dy) {
            return String.format("sqrt(sqr((%s - %s)/%s) + sqr((%s - %s)/%s)) < 1.0",
                    BandArithmetic.createExternalName(dataSourceConfig.xBand.getName()), x0, dx,
                    BandArithmetic.createExternalName(dataSourceConfig.yBand.getName()), y0, dy);
        }
    };

    densityPlotDisplay.getPopupMenu().addSeparator();
    densityPlotDisplay.getPopupMenu().add(maskSelectionToolSupport.createMaskSelectionModeMenuItem());
    densityPlotDisplay.getPopupMenu().add(maskSelectionToolSupport.createDeleteMaskMenuItem());
    densityPlotDisplay.getPopupMenu().addSeparator();
    densityPlotDisplay.getPopupMenu().add(createCopyDataToClipboardMenuItem());
    return densityPlotDisplay;
}

From source file:gov.nih.nci.cma.web.graphing.CMAPrincipalComponentAnalysisPlot.java

/**
 * Get a collection of entities with the area shape equal to the bounding rectangle
 * for the shape of original entity. This is necessary because the Javascript for the sample
 * selection lasso can only handle rect objects.
 * @param entities//w w  w . j ava 2 s  . c o m
 * @return a collection of entities containing the bounding rectangles of the original entities
 */
private Collection<ChartEntity> getBoundingEntities(Collection entities) {
    ChartEntity entity;
    ChartEntity boundingEntity;
    Shape shape;
    Rectangle2D boundingRect;
    Collection<ChartEntity> boundingEntities = new ArrayList<ChartEntity>();
    for (Iterator i = entities.iterator(); i.hasNext();) {
        entity = (ChartEntity) i.next();
        shape = entity.getArea();
        boundingRect = shape.getBounds2D();
        boundingEntity = new ChartEntity(boundingRect, entity.getToolTipText(), entity.getURLText());
        boundingEntities.add(boundingEntity);
    }
    return boundingEntities;
}

From source file:org.esa.snap.rcp.statistics.ProfilePlotPanel.java

private ChartPanel createChartPanel(JFreeChart chart) {
    profilePlotDisplay = new ChartPanel(chart);

    MaskSelectionToolSupport maskSelectionToolSupport = new MaskSelectionToolSupport(this, profilePlotDisplay,
            "profile_plot_area", "Mask generated from selected profile plot area", Color.RED,
            PlotAreaSelectionTool.AreaType.Y_RANGE) {

        @Override/*from  ww w . j  a va  2  s  .c om*/
        protected String createMaskExpression(PlotAreaSelectionTool.AreaType areaType, Shape shape) {
            Rectangle2D bounds = shape.getBounds2D();
            return createMaskExpression(bounds.getMinY(), bounds.getMaxY());
        }

        protected String createMaskExpression(double x1, double x2) {
            String bandName = BandArithmetic.createExternalName(getRaster().getName());
            return String.format("%s >= %s && %s <= %s", bandName, x1, bandName, x2);
        }
    };

    profilePlotDisplay.addChartMouseListener(new XYPlotMarker(profilePlotDisplay, new XYPlotMarker.Listener() {
        @Override
        public void pointSelected(XYDataset xyDataset, int seriesIndex, Point2D dataPoint) {
            if (profileData != null) {
                GeoPos[] geoPositions = profileData.getGeoPositions();
                int index = (int) dataPoint.getX();
                if (index >= 0 && index < geoPositions.length) {
                    if (cursorSynchronizer == null) {
                        cursorSynchronizer = new DefaultCursorSynchronizer();
                    }
                    if (!cursorSynchronizer.isEnabled()) {
                        cursorSynchronizer.setEnabled(true);
                    }
                    cursorSynchronizer.updateCursorOverlays(geoPositions[index]);
                }
            }
        }

        @Override
        public void pointDeselected() {
            cursorSynchronizer.setEnabled(false);
        }
    }));
    profilePlotDisplay.setInitialDelay(200);
    profilePlotDisplay.setDismissDelay(1500);
    profilePlotDisplay.setReshowDelay(200);
    profilePlotDisplay.setZoomTriggerDistance(5);
    profilePlotDisplay.getPopupMenu().addSeparator();
    profilePlotDisplay.getPopupMenu().add(maskSelectionToolSupport.createMaskSelectionModeMenuItem());
    profilePlotDisplay.getPopupMenu().add(maskSelectionToolSupport.createDeleteMaskMenuItem());
    profilePlotDisplay.getPopupMenu().addSeparator();
    profilePlotDisplay.getPopupMenu().add(createCopyDataToClipboardMenuItem());

    return profilePlotDisplay;
}

From source file:org.esa.beam.visat.toolviews.stat.HistogramPanel.java

private ChartPanel createChartPanel(JFreeChart chart) {
    XYPlot plot = chart.getXYPlot();/*w ww .  j  a v  a 2  s. co  m*/

    plot.setForegroundAlpha(0.85f);
    plot.setNoDataMessage(NO_DATA_MESSAGE);
    plot.setAxisOffset(new RectangleInsets(5, 5, 5, 5));

    ChartPanel chartPanel = new ChartPanel(chart);

    MaskSelectionToolSupport maskSelectionToolSupport = new MaskSelectionToolSupport(this, chartPanel,
            "histogram_plot_area", "Mask generated from selected histogram plot area", Color.RED,
            PlotAreaSelectionTool.AreaType.X_RANGE) {

        @Override
        protected String createMaskExpression(PlotAreaSelectionTool.AreaType areaType, Shape shape) {
            Rectangle2D bounds = shape.getBounds2D();
            return createMaskExpression(bounds.getMinX(), bounds.getMaxX());
        }

        protected String createMaskExpression(double x1, double x2) {
            String bandName = BandArithmetic.createExternalName(getRaster().getName());
            HistogramPanelModel.HistogramConfig currentConfig = createHistogramConfig();
            return String.format("%s >= %s && %s <= %s", bandName,
                    model.hasStx(currentConfig)
                            ? model.getStx(currentConfig).getHistogramScaling().scaleInverse(x1)
                            : x1,
                    bandName,
                    model.hasStx(currentConfig)
                            ? model.getStx(currentConfig).getHistogramScaling().scaleInverse(x2)
                            : x2);
        }
    };

    chartPanel.getPopupMenu().addSeparator();
    chartPanel.getPopupMenu().add(maskSelectionToolSupport.createMaskSelectionModeMenuItem());
    chartPanel.getPopupMenu().add(maskSelectionToolSupport.createDeleteMaskMenuItem());
    chartPanel.getPopupMenu().addSeparator();
    chartPanel.getPopupMenu().add(createCopyDataToClipboardMenuItem());
    return chartPanel;
}

From source file:org.esa.beam.visat.toolviews.stat.ProfilePlotPanel.java

private ChartPanel createChartPanel(JFreeChart chart) {
    profilePlotDisplay = new ChartPanel(chart);

    MaskSelectionToolSupport maskSelectionToolSupport = new MaskSelectionToolSupport(this, profilePlotDisplay,
            "profile_plot_area", "Mask generated from selected profile plot area", Color.RED,
            PlotAreaSelectionTool.AreaType.Y_RANGE) {

        @Override/*from w ww  .  j ava 2s. com*/
        protected String createMaskExpression(PlotAreaSelectionTool.AreaType areaType, Shape shape) {
            Rectangle2D bounds = shape.getBounds2D();
            return createMaskExpression(bounds.getMinY(), bounds.getMaxY());
        }

        protected String createMaskExpression(double x1, double x2) {
            String bandName = BandArithmetic.createExternalName(getRaster().getName());
            return String.format("%s >= %s && %s <= %s", bandName, x1, bandName, x2);
        }
    };

    profilePlotDisplay.addChartMouseListener(new XYPlotMarker(profilePlotDisplay, new XYPlotMarker.Listener() {
        @Override
        public void pointSelected(XYDataset xyDataset, int seriesIndex, Point2D dataPoint) {
            if (profileData != null) {
                GeoPos[] geoPositions = profileData.getGeoPositions();
                int index = (int) dataPoint.getX();
                if (index >= 0 && index < geoPositions.length) {
                    if (cursorSynchronizer == null) {
                        cursorSynchronizer = new CursorSynchronizer(VisatApp.getApp());
                    }
                    if (!cursorSynchronizer.isEnabled()) {
                        cursorSynchronizer.setEnabled(true);
                    }
                    cursorSynchronizer.updateCursorOverlays(geoPositions[index]);
                }
            }
        }

        @Override
        public void pointDeselected() {
            cursorSynchronizer.setEnabled(false);
        }
    }));
    profilePlotDisplay.setInitialDelay(200);
    profilePlotDisplay.setDismissDelay(1500);
    profilePlotDisplay.setReshowDelay(200);
    profilePlotDisplay.setZoomTriggerDistance(5);
    profilePlotDisplay.getPopupMenu().addSeparator();
    profilePlotDisplay.getPopupMenu().add(maskSelectionToolSupport.createMaskSelectionModeMenuItem());
    profilePlotDisplay.getPopupMenu().add(maskSelectionToolSupport.createDeleteMaskMenuItem());
    profilePlotDisplay.getPopupMenu().addSeparator();
    profilePlotDisplay.getPopupMenu().add(createCopyDataToClipboardMenuItem());

    return profilePlotDisplay;
}