Example usage for java.awt.geom Rectangle2D getWidth

List of usage examples for java.awt.geom Rectangle2D getWidth

Introduction

In this page you can find the example usage for java.awt.geom Rectangle2D getWidth.

Prototype

public abstract double getWidth();

Source Link

Document

Returns the width of the framing rectangle in double precision.

Usage

From source file:org.jfree.chart.demo.CircleDrawer.java

/**
 * Draws the circle.//from   www  .j ava 2  s .c  o m
 * 
 * @param g2  the graphics device.
 * @param area  the area in which to draw.
 */
public void draw(final Graphics2D g2, final Rectangle2D area) {
    final Ellipse2D ellipse = new Ellipse2D.Double(area.getX(), area.getY(), area.getWidth(), area.getHeight());
    if (this.fillPaint != null) {
        g2.setPaint(this.fillPaint);
        g2.fill(ellipse);
    }
    if (this.outlinePaint != null && this.outlineStroke != null) {
        g2.setPaint(this.outlinePaint);
        g2.setStroke(this.outlineStroke);
        g2.draw(ellipse);
    }

    g2.setPaint(Color.black);
    g2.setStroke(new BasicStroke(1.0f));
    final Line2D line1 = new Line2D.Double(area.getCenterX(), area.getMinY(), area.getCenterX(),
            area.getMaxY());
    final Line2D line2 = new Line2D.Double(area.getMinX(), area.getCenterY(), area.getMaxX(),
            area.getCenterY());
    g2.draw(line1);
    g2.draw(line2);
}

From source file:de.codesourcery.planning.swing.PlanningCanvas.java

protected static BoundingBox createBoundingBox(int x, int y, Rectangle2D stringBounds) {
    return BoundingBox.createFromCoordinates(new Rectangle2D.Float(x, y,
            x + Math.round(stringBounds.getWidth()), y + Math.round(stringBounds.getHeight())));
}

From source file:net.sf.mzmine.chartbasics.ChartLogicsFX.java

/**
 * Calculates the size of a chart for a given fixed plot width and height
 * /*from w  ww  .  ja  va 2 s.c  om*/
 * @param chart
 * @param plotWidth
 * @return
 */
public static Dimension calcSizeForPlotSize(ChartViewer myChart, double plotWidth, double plotHeight,
        int iterations) {
    makeChartResizable(myChart);

    // estimate plotwidth / height
    double estimatedChartWidth = plotWidth + 200;
    double estimatedChartHeight = plotHeight + 200;

    double lastW = estimatedChartWidth;
    double lastH = estimatedChartHeight;

    // paint and get closer
    try {
        for (int i = 0; i < iterations; i++) {
            // paint on ghost panel with estimated height (if copy panel==true)
            myChart.getCanvas().setWidth((int) estimatedChartWidth);
            myChart.getCanvas().setHeight((int) estimatedChartHeight);
            myChart.getCanvas().draw();

            // rendering info
            ChartRenderingInfo info = myChart.getRenderingInfo();
            Rectangle2D dataArea = info.getPlotInfo().getDataArea();
            Rectangle2D chartArea = info.getChartArea();

            // // calc title space: will be added later to the right plot size
            // double titleWidth = chartArea.getWidth()-dataArea.getWidth();
            // double titleHeight = chartArea.getHeight()-dataArea.getHeight();

            // calc width and height
            estimatedChartWidth = estimatedChartWidth - dataArea.getWidth() + plotWidth;
            estimatedChartHeight = estimatedChartHeight - dataArea.getHeight() + plotHeight;

            if ((int) lastW == (int) estimatedChartWidth && (int) lastH == (int) estimatedChartHeight)
                break;
            else {
                lastW = estimatedChartWidth;
                lastH = estimatedChartHeight;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return new Dimension((int) estimatedChartWidth, (int) estimatedChartHeight);
}

From source file:net.sf.mzmine.chartbasics.ChartLogics.java

/**
 * Calculates the size of a chart for a given fixed plot width and height
 * /*w  ww  .  jav  a2 s .  c om*/
 * @param chart
 * @param plotWidth
 * @return
 */
public static Dimension calcSizeForPlotSize(ChartPanel myChart, double plotWidth, double plotHeight,
        int iterations) {
    makeChartResizable(myChart);

    // estimate plotwidth / height
    double estimatedChartWidth = plotWidth + 200;
    double estimatedChartHeight = plotHeight + 200;

    double lastW = estimatedChartWidth;
    double lastH = estimatedChartHeight;

    // paint and get closer
    try {
        for (int i = 0; i < iterations; i++) {
            // paint on ghost panel with estimated height (if copy panel==true)
            myChart.setSize((int) estimatedChartWidth, (int) estimatedChartHeight);
            myChart.paintImmediately(myChart.getBounds());

            // rendering info
            ChartRenderingInfo info = myChart.getChartRenderingInfo();
            Rectangle2D dataArea = info.getPlotInfo().getDataArea();
            Rectangle2D chartArea = info.getChartArea();

            // // calc title space: will be added later to the right plot size
            // double titleWidth = chartArea.getWidth()-dataArea.getWidth();
            // double titleHeight = chartArea.getHeight()-dataArea.getHeight();

            // calc width and height
            estimatedChartWidth = estimatedChartWidth - dataArea.getWidth() + plotWidth;
            estimatedChartHeight = estimatedChartHeight - dataArea.getHeight() + plotHeight;

            if ((int) lastW == (int) estimatedChartWidth && (int) lastH == (int) estimatedChartHeight)
                break;
            else {
                lastW = estimatedChartWidth;
                lastH = estimatedChartHeight;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return new Dimension((int) estimatedChartWidth, (int) estimatedChartHeight);
}

From source file:com.rapidminer.gui.graphs.ExtendedVertexShapeTransformer.java

@Override
public Shape transform(V object) {
    if (graphCreator.isLeaf(object)) {
        // leaf/*from   w  w w  .  jav a2  s .co  m*/
        String text = graphCreator.getVertexName(object);
        Rectangle2D stringBounds = GraphViewer.VERTEX_BOLD_FONT.getStringBounds(text,
                new FontRenderContext(null, false, false));
        float width = (float) stringBounds.getWidth();
        float height = (float) stringBounds.getHeight();
        int minWidth = graphCreator.getMinLeafWidth();
        int minHeight = graphCreator.getMinLeafHeight();
        width = Math.max(width, minWidth);
        height = Math.max(height, minHeight);
        return new Rectangle2D.Float(-width / 2.0f - 6.0f, -height / 2.0f - 2.0f, width + 8.0f, height + 4.0f);
    } else {
        // inner nodes
        String text = graphCreator.getVertexName(object);
        Rectangle2D stringBounds = GraphViewer.VERTEX_PLAIN_FONT.getStringBounds(text,
                new FontRenderContext(null, false, false));
        float width = (float) stringBounds.getWidth();
        float height = (float) stringBounds.getHeight();
        RoundRectangle2D.Float shape = new RoundRectangle2D.Float(-width / 2.0f - 6.0f, -height / 2.0f - 4.0f,
                width + 10.0f, height + 8.0f, 10.0f, 10.0f);
        return shape;
    }
}

From source file:edu.umn.ecology.populus.plot.ChartRendererWithOrientatedShapes.java

@Override
protected void drawSecondaryPass(Graphics2D g2, XYPlot plot, XYDataset dataset, int pass, int series, int item,
        ValueAxis domainAxis, Rectangle2D dataArea, ValueAxis rangeAxis, CrosshairState crosshairState,
        EntityCollection entities) {//  w  ww.  j  a v a2  s  . c  o m

    //Orient the shapes first
    double graphicsRatio = dataArea.getHeight() / dataArea.getWidth();
    double dataRatio = plot.getRangeAxis().getRange().getLength() / plot.getDomainAxis().getRange().getLength();
    double ratio = graphicsRatio / dataRatio;
    bpInfo.updateDirectedSymbolsJFC(this, ratio);

    //Now call this to do the rest
    super.drawSecondaryPass(g2, plot, dataset, pass, series, item, domainAxis, dataArea, rangeAxis,
            crosshairState, entities);
}

From source file:org.springframework.cloud.stream.app.image.recognition.processor.ImageRecognitionOutputMessageBuilder.java

/**
 * Augment the input image by adding the recognized classes.
 *
 * @param imageBytes input image as byte array
 * @param result computed recognition labels
 * @return the image augmented with recognized labels.
 *//*from w ww.  j  av a2  s  . c o m*/
private byte[] drawLabels(byte[] imageBytes, Object result) {
    try {
        if (result != null) {
            BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageBytes));

            Graphics2D g = originalImage.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            FontMetrics fm = g.getFontMetrics();

            Tuple resultTuple = new JsonStringToTupleConverter().convert(result.toString());
            ArrayList<Tuple> labels = (ArrayList) resultTuple.getValues().get(0);

            int x = 1;
            int y = 1;
            for (Tuple l : labels) {

                String labelName = l.getFieldNames().get(0);
                int probability = (int) (100 * l.getFloat(0));
                String title = labelName + ": " + probability + "%";

                Rectangle2D rect = fm.getStringBounds(title, g);

                g.setColor(bgColor);
                g.fillRect(x, y, (int) rect.getWidth() + 6, (int) rect.getHeight());

                g.setColor(textColor);
                g.drawString(title, x + 3, (int) (y + rect.getHeight() - 3));
                y = (int) (y + rect.getHeight() + 1);
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(originalImage, IMAGE_FORMAT, baos);
            baos.flush();
            imageBytes = baos.toByteArray();
            baos.close();
        }
    } catch (IOException e) {
        logger.error("Failed to draw labels in the input image", e);
    }

    return imageBytes;
}

From source file:edu.uci.ics.jung.visualization.util.VertexShapeFactory.java

/**
 * Returns a regular <code>num_sides</code>-sided 
 * <code>Polygon</code> whose bounding 
 * box's width and height are defined by this instance's size and
 * aspect ratio functions for this vertex.
 * @param num_sides the number of sides of the polygon; must be >= 3.
 *//*from   w w w.j a  va 2 s .co m*/
public Shape getRegularPolygon(V v, int num_sides) {
    if (num_sides < 3)
        throw new IllegalArgumentException("Number of sides must be >= 3");
    Rectangle2D frame = getRectangle(v);
    float width = (float) frame.getWidth();
    float height = (float) frame.getHeight();

    // generate coordinates
    double angle = 0;
    thePolygon.reset();
    thePolygon.moveTo(0, 0);
    thePolygon.lineTo(width, 0);
    double theta = (2 * Math.PI) / num_sides;
    for (int i = 2; i < num_sides; i++) {
        angle -= theta;
        float delta_x = (float) (width * Math.cos(angle));
        float delta_y = (float) (width * Math.sin(angle));
        Point2D prev = thePolygon.getCurrentPoint();
        thePolygon.lineTo((float) prev.getX() + delta_x, (float) prev.getY() + delta_y);
    }
    thePolygon.closePath();

    // scale polygon to be right size, translate to center at (0,0)
    Rectangle2D r = thePolygon.getBounds2D();
    double scale_x = width / r.getWidth();
    double scale_y = height / r.getHeight();
    float translationX = (float) (r.getMinX() + r.getWidth() / 2);
    float translationY = (float) (r.getMinY() + r.getHeight() / 2);

    AffineTransform at = AffineTransform.getScaleInstance(scale_x, scale_y);
    at.translate(-translationX, -translationY);

    Shape shape = at.createTransformedShape(thePolygon);
    return shape;
}

From source file:edu.uci.ics.jung.visualization.util.VertexShapeFactory.java

/**
 * Returns a <code>RoundRectangle2D</code> whose width and 
 * height are defined by this instance's size and
 * aspect ratio functions for this vertex.  The arc size is
 * set to be half the minimum of the height and width of the frame.
 *//*from www .  j  av a  2s .c o  m*/
public RoundRectangle2D getRoundRectangle(V v) {
    Rectangle2D frame = getRectangle(v);
    float arc_size = (float) Math.min(frame.getHeight(), frame.getWidth()) / 2;
    theRoundRectangle.setRoundRect(frame.getX(), frame.getY(), frame.getWidth(), frame.getHeight(), arc_size,
            arc_size);
    return theRoundRectangle;
}

From source file:net.sourceforge.processdash.ui.lib.chart.PaddedAxisHelper.java

public Rectangle2D removePadding(Rectangle2D area, RectangleEdge edge) {
    boolean inverted = axis.isInverted();
    if (RectangleEdge.isTopOrBottom(edge)) {
        double xPad = inverted ? upperPad : lowerPad;
        double width = area.getWidth() - upperPad - lowerPad;
        return new Rectangle2D.Double(area.getX() + xPad, area.getY(), width, area.getHeight());
    } else {/*from  w  w  w .jav  a  2 s. c  o m*/
        double yPad = inverted ? lowerPad : upperPad;
        double height = area.getHeight() - upperPad - lowerPad;
        return new Rectangle2D.Double(area.getX(), area.getY() + yPad, area.getWidth(), height);
    }
}