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:edu.uci.ics.jung.visualization.util.VertexShapeFactory.java

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

    // generate coordinates
    double theta = (2 * Math.PI) / num_points;
    double angle = -theta / 2;
    thePolygon.reset();
    thePolygon.moveTo(0, 0);
    float delta_x = width * (float) Math.cos(angle);
    float delta_y = width * (float) Math.sin(angle);
    Point2D prev = thePolygon.getCurrentPoint();
    thePolygon.lineTo((float) prev.getX() + delta_x, (float) prev.getY() + delta_y);
    for (int i = 1; i < num_points; i++) {
        angle += theta;
        delta_x = width * (float) Math.cos(angle);
        delta_y = width * (float) Math.sin(angle);
        prev = thePolygon.getCurrentPoint();
        thePolygon.lineTo((float) prev.getX() + delta_x, (float) prev.getY() + delta_y);
        angle -= theta * 2;
        delta_x = width * (float) Math.cos(angle);
        delta_y = width * (float) Math.sin(angle);
        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:de.hs.mannheim.modUro.controller.diagram.fx.interaction.ZoomHandlerFX.java

private double percentW(double x, Rectangle2D r) {
    return (x - r.getMinX()) / r.getWidth();
}

From source file:com.willwinder.universalgcodesender.uielements.helpers.Overlay.java

/** Updates the Overlay. It is assumed this method will be called only
    once per frame./*  ww  w  .  j ava  2s . com*/
*/
public void draw(String text) {
    if (StringUtils.isNotBlank(text)) {
        text = text.trim();

        renderer.beginRendering(drawable.getSurfaceWidth(), drawable.getSurfaceHeight());

        Rectangle2D bounds = renderer.getBounds(text);
        int width = (int) bounds.getWidth();
        int height = (int) bounds.getHeight();
        int offset = (int) (height * 0.5f);

        // Figure out the location at which to draw the text
        int x = 0;
        int y = 0;
        switch (textLocation) {
        case UPPER_LEFT:
            x = offset;
            y = drawable.getSurfaceHeight() - height - offset;
            break;

        case UPPER_RIGHT:
            x = drawable.getSurfaceWidth() - width - offset;
            y = drawable.getSurfaceHeight() - height - offset;
            break;

        case LOWER_LEFT:
            x = offset;
            y = offset;
            break;

        case LOWER_RIGHT:
            x = drawable.getSurfaceWidth() - width - offset;
            y = offset;
            break;

        default:
            break;
        }

        renderer.draw(text, x, y);
        renderer.endRendering();
    }
}

From source file:com.projity.pm.graphic.xbs.XbsLayout.java

protected int updateBounds(Point2D origin, Rectangle2D ref) {//cache in current version isn't a tree
    double x = origin.getX() + ref.getWidth() / 2;
    double y = origin.getY() + ref.getHeight() / 2;
    GraphicNode node, previous = null;/*  w  w  w  .  j a v a 2 s  .  c o  m*/
    int maxLevel = 0;
    for (ListIterator i = cache.getIterator(); i.hasNext();) {
        node = (GraphicNode) i.next();
        if (node.getLevel() > maxLevel)
            maxLevel = node.getLevel();
        if (previous != null && node.getLevel() <= previous.getLevel()) {
            setShape(previous, ref, x, y + (previous.getLevel() - 1) * (ref.getMaxY()));
            x += ref.getMaxX();
        }
        previous = node;
    }
    if (previous != null) {
        setShape(previous, ref, x, y + (previous.getLevel() - 1) * (ref.getMaxY()));
    }
    return maxLevel;
}

From source file:org.apache.fop.render.svg.SVGRenderer.java

/** {@inheritDoc} */
public void renderPage(PageViewport pageViewport) throws IOException {
    log.debug("Rendering page: " + pageViewport.getPageNumberString());
    // Get a DOMImplementation
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();

    // Create an instance of org.w3c.dom.Document
    this.document = domImpl.createDocument(null, "svg", null);

    // Create an SVGGeneratorContext to customize SVG generation
    SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(this.document);
    ctx.setComment("Generated by " + userAgent.getProducer() + " with Batik SVG Generator");
    ctx.setEmbeddedFontsOn(true);//www  . j a  v a  2 s .  c  o m

    // Create an instance of the SVG Generator
    this.svgGenerator = new SVGGraphics2D(ctx, true);
    Rectangle2D viewArea = pageViewport.getViewArea();
    Dimension dim = new Dimension();
    dim.setSize(viewArea.getWidth() / 1000, viewArea.getHeight() / 1000);
    this.svgGenerator.setSVGCanvasSize(dim);

    AffineTransform at = this.svgGenerator.getTransform();
    this.state = new Java2DGraphicsState(this.svgGenerator, this.fontInfo, at);
    try {
        //super.renderPage(pageViewport);
        renderPageAreas(pageViewport.getPage());
    } finally {
        this.state = null;
    }
    writeSVGFile(pageViewport.getPageIndex());

    this.svgGenerator = null;
    this.document = null;

}

From source file:org.nuxeo.pdf.service.PDFTransformationServiceImpl.java

public Point2D computeTranslationVector(double pageWidth, double watermarkWidth, double pageHeight,
        double watermarkHeight, WatermarkProperties properties) {
    double xTranslation;
    double yTranslation;
    double xRotationOffset = 0;
    double yRotationOffset = 0;

    if (properties.getTextRotation() != 0) {
        Rectangle2D rectangle2D = new Rectangle2D.Double(0, -watermarkHeight, watermarkWidth, watermarkHeight);
        AffineTransform at = AffineTransform.getRotateInstance(-Math.toRadians(properties.getTextRotation()), 0,
                0);//from w  ww  .j  a va2  s .  c o  m
        Shape shape = at.createTransformedShape(rectangle2D);
        Rectangle2D rotated = shape.getBounds2D();

        watermarkWidth = rotated.getWidth();
        if (!properties.isInvertX() || properties.isRelativeCoordinates()) {
            xRotationOffset = -rotated.getX();
        } else {
            xRotationOffset = rotated.getX();
        }

        watermarkHeight = rotated.getHeight();
        if (!properties.isInvertY() || properties.isRelativeCoordinates()) {
            yRotationOffset = rotated.getY() + rotated.getHeight();
        } else {
            yRotationOffset = -(rotated.getY() + rotated.getHeight());
        }

    }

    if (properties.isRelativeCoordinates()) {
        xTranslation = (pageWidth - watermarkWidth) * properties.getxPosition() + xRotationOffset;
        yTranslation = (pageHeight - watermarkHeight) * properties.getyPosition() + yRotationOffset;
    } else {
        xTranslation = properties.getxPosition() + xRotationOffset;
        yTranslation = properties.getyPosition() + yRotationOffset;
        if (properties.isInvertX())
            xTranslation = pageWidth - watermarkWidth - xTranslation;
        if (properties.isInvertY())
            yTranslation = pageHeight - watermarkHeight - yTranslation;
    }
    return new Point2D.Double(xTranslation, yTranslation);
}

From source file:de.hs.mannheim.modUro.controller.diagram.fx.interaction.PanHandlerFX.java

/**
 * Handles a mouse pressed event by recording the initial mouse pointer
 * location.//from  w  w w . j a v  a2  s .co m
 * 
 * @param canvas  the JavaFX canvas (<code>null</code> not permitted).
 * @param e  the mouse event (<code>null</code> not permitted).
 */
@Override
public void handleMousePressed(ChartCanvas canvas, MouseEvent e) {
    Plot plot = canvas.getChart().getPlot();
    if (!(plot instanceof Pannable)) {
        canvas.clearLiveHandler();
        return;
    }
    Pannable pannable = (Pannable) plot;
    if (pannable.isDomainPannable() || pannable.isRangePannable()) {
        Point2D point = new Point2D.Double(e.getX(), e.getY());
        Rectangle2D dataArea = canvas.findDataArea(point);
        if (dataArea != null && dataArea.contains(point)) {
            this.panW = dataArea.getWidth();
            this.panH = dataArea.getHeight();
            this.panLast = point;
            canvas.setCursor(javafx.scene.Cursor.MOVE);
        }
    }
    // the actual panning occurs later in the mouseDragged() method
}

From source file:Chart.java

 public void paintComponent(Graphics g)
{
   Graphics2D g2 = (Graphics2D) g;

   // compute the minimum and maximum values
   if (values == null) return;
   double minValue = 0;
   double maxValue = 0;
   for (double v : values)
   {/* w  w  w  . j a  va2  s  .co  m*/
      if (minValue > v) minValue = v;
      if (maxValue < v) maxValue = v;
   }
   if (maxValue == minValue) return;

   int panelWidth = getWidth();
   int panelHeight = getHeight();

   Font titleFont = new Font("SansSerif", Font.BOLD, 20);
   Font labelFont = new Font("SansSerif", Font.PLAIN, 10);

   // compute the extent of the title
   FontRenderContext context = g2.getFontRenderContext();
   Rectangle2D titleBounds = titleFont.getStringBounds(title, context);
   double titleWidth = titleBounds.getWidth();
   double top = titleBounds.getHeight();

   // draw the title
   double y = -titleBounds.getY(); // ascent
   double x = (panelWidth - titleWidth) / 2;
   g2.setFont(titleFont);
   g2.drawString(title, (float) x, (float) y);

   // compute the extent of the bar labels
   LineMetrics labelMetrics = labelFont.getLineMetrics("", context);
   double bottom = labelMetrics.getHeight();

   y = panelHeight - labelMetrics.getDescent();
   g2.setFont(labelFont);

   // get the scale factor and width for the bars
   double scale = (panelHeight - top - bottom) / (maxValue - minValue);
   int barWidth = panelWidth / values.length;

   // draw the bars
   for (int i = 0; i < values.length; i++)
   {
      // get the coordinates of the bar rectangle
      double x1 = i * barWidth + 1;
      double y1 = top;
      double height = values[i] * scale;
      if (values[i] >= 0) y1 += (maxValue - values[i]) * scale;
      else
      {
         y1 += maxValue * scale;
         height = -height;
      }

      // fill the bar and draw the bar outline
      Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 2, height);
      g2.setPaint(Color.RED);
      g2.fill(rect);
      g2.setPaint(Color.BLACK);
      g2.draw(rect);

      // draw the centered label below the bar
      Rectangle2D labelBounds = labelFont.getStringBounds(names[i], context);

      double labelWidth = labelBounds.getWidth();
      x = x1 + (barWidth - labelWidth) / 2;
      g2.drawString(names[i], (float) x, (float) y);
   }
}

From source file:ru.runa.wfe.graph.image.figure.AbstractFigure.java

private int drawText(Graphics2D graphics, String text, int hOffset) {
    Rectangle r = getTextBoundsRectangle();
    Rectangle2D textBounds = graphics.getFontMetrics().getStringBounds(text, graphics);
    if (textBounds.getWidth() > r.getWidth() - 4) {
        int y = coords[1] + hOffset;
        AttributedString attributedString = new AttributedString(text);
        attributedString.addAttribute(TextAttribute.FONT, graphics.getFont());
        AttributedCharacterIterator characterIterator = attributedString.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, graphics.getFontRenderContext());
        while (measurer.getPosition() < characterIterator.getEndIndex()) {
            TextLayout textLayout = measurer.nextLayout((float) r.getWidth() - 4);
            y += textLayout.getAscent();
            float x = (float) (r.getCenterX() + 2 - textLayout.getBounds().getCenterX());
            textLayout.draw(graphics, x, y);
            y += textLayout.getDescent() + textLayout.getLeading();
        }/*from w  w  w .jav  a 2s.c o m*/
        return y - coords[1];
    } else {
        graphics.drawString(text, (float) (r.getCenterX() + 2 - textBounds.getCenterX()),
                (float) (coords[1] + textBounds.getHeight() + hOffset));
        return (int) (textBounds.getHeight() + hOffset + 3);
    }
}

From source file:org.locationtech.udig.mapgraphic.scale.ScaleDenomMapGraphic.java

public void draw(MapGraphicContext context) {

    ViewportGraphics g = context.getGraphics();
    IBlackboard mapblackboard = context.getMap().getBlackboard();

    double scaleDenom = context.getViewportModel().getScaleDenominator();

    // scale may be set by printing engine
    Object value = mapblackboard.get("scale"); //$NON-NLS-1$

    if (value != null && value instanceof Double) {
        scaleDenom = ((Double) value).doubleValue();
    }/*w  w w  . j  a va 2  s  . c  o  m*/
    FontStyle fs = getFontStyle(context);
    g.setFont(fs.getFont());

    Point loc = getGraphicLocation(context);

    ScaleDenomStyle background = getStyle(context);

    String denomStr = NUMBER_FORMAT.format(scaleDenom);
    String str = "1:" + denomStr; //$NON-NLS-1$
    //check if a prefix is provided
    if (StringUtils.trimToNull(background.getLabel()) != null) {
        str = background.getLabel() + " " + str;
    }
    Rectangle2D bnds = g.getStringBounds(str);

    int x = loc.x;
    int y = loc.y;

    if (x < 0) {
        x = context.getMapDisplay().getWidth() + x - (int) bnds.getWidth();
    }
    if (y < 0) {
        y = context.getMapDisplay().getHeight() + y - (int) bnds.getHeight();
    }

    //draw rectangle      
    if (background.getColor() != null) {
        context.getGraphics().setColor(background.getColor());
        g.fillRect(x - 2, y - 2, (int) bnds.getWidth() + 4, (int) bnds.getHeight() + 4);
    }

    g.setColor(fs.getColor());
    g.drawString(str, x + (int) bnds.getWidth() / 2, y + (g.getFontHeight() - g.getFontAscent()),
            ViewportGraphics.ALIGN_MIDDLE, ViewportGraphics.ALIGN_MIDDLE);

}