Example usage for java.awt Shape getBounds

List of usage examples for java.awt Shape getBounds

Introduction

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

Prototype

public Rectangle getBounds();

Source Link

Document

Returns an integer Rectangle that completely encloses the Shape .

Usage

From source file:FormattedTextFieldExample.java

public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
    a = adjustAllocation(a);//w  ww  .ja  va  2 s. com
    bias[0] = Position.Bias.Forward;

    int x = (int) fx;
    int y = (int) fy;
    Rectangle r = a.getBounds();
    int startOffset = element.getStartOffset();
    int endOffset = element.getEndOffset();

    if (y < r.y || x < r.x) {
        return startOffset;
    } else if (y > r.y + r.height || x > r.x + r.width) {
        return endOffset - 1;
    }

    // The given position is within the bounds of the view.
    int offset = Utilities.getTabbedTextOffset(contentBuff, getFontMetrics(), r.x, x, this, startOffset);
    // The offset includes characters not in the model,
    // so get rid of them to return a true model offset.
    for (int i = 0; i < offsets.length; i++) {
        if (offset <= offsets[i]) {
            offset = i;
            break;
        }
    }

    // Don't return an offset beyond the data
    // actually in the model.
    if (offset > endOffset - 1) {
        offset = endOffset - 1;
    }
    return offset;
}

From source file:gda.plots.TurboXYItemRenderer.java

/**
 * Draws the visual representation of a single data item. This mostly reproduces the code of StandardXYItemRenderer
 * but using the line by line information stored in the SimpleXYSeries instead of the series indexed information
 * stored in the Renderer itself.//from ww  w.  j a  va 2  s . c om
 * 
 * @param g2
 *            the graphics device.
 * @param state
 *            the renderer state.
 * @param dataArea
 *            the area within which the data is being drawn.
 * @param info
 *            collects information about the drawing.
 * @param plot
 *            the plot (can be used to obtain standard color information etc).
 * @param domainAxis
 *            the domain axis.
 * @param rangeAxis
 *            the range axis.
 * @param dataset
 *            the dataset.
 * @param series
 *            the series index (zero-based).
 * @param crosshairState
 *            crosshair information for the plot ( <code>null</code> permitted).
 * @param pass
 *            the pass index.
 */
@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int _item,
        CrosshairState crosshairState, int pass) {

    if (_item > 0)
        return;
    SimpleXYSeries sxys = (SimpleXYSeries) ((SimpleXYSeriesCollection) dataset).getSeries(series);
    if (!sxys.isVisible()) {
        return;
    }

    PlotOrientation orientation = plot.getOrientation();
    g2.setPaint(sxys.getPaint());
    g2.setStroke(sxys.getStroke());

    RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
    RectangleEdge yAxisLocation = plot.getRangeAxisEdge();

    try {
        int x0 = -1; // the x position in pixels of the previous point
        int y0 = -1; // the y position in pixels of the previous point
        int x1 = -1; // the x position in pixels of the current point
        int y1 = -1; // the y position in pixels of the current point
        int xmin = (int) dataArea.getMinX();
        int xmax = (int) dataArea.getMaxX();
        int ymin = (int) dataArea.getMinY();
        int ymax = (int) dataArea.getMaxY();
        GeneralPath path = null;

        /*
         * To remove the time spent repeatedly calling domainAxis.valueToJava2D for linear axes use simple linear
         * maths
         */
        double xl = 0., mx = Double.NaN, cx = 0.;
        if (domainAxis instanceof SimpleNumberAxis) {
            xl = domainAxis.getRange().getLowerBound();
            mx = dataArea.getWidth() / (domainAxis.getRange().getUpperBound() - xl);
            cx = xmin;
        }
        double yl = 0., my = Double.NaN, cy = 0.;
        if (rangeAxis instanceof SimpleNumberAxis) {
            yl = rangeAxis.getRange().getLowerBound();
            my = -dataArea.getHeight() / (rangeAxis.getRange().getUpperBound() - yl);
            cy = ymax;
        }
        List<XYDataItem> list = sxys.getData();

        boolean MX_MY_NaN = Double.isNaN(mx) || Double.isNaN(my);
        Paint paint = sxys.getPaint();
        Stroke stroke = sxys.getStroke();
        Paint paint_symbol = sxys.getSymbolPaint();
        Stroke stroke_symbol = new BasicStroke();
        drawLines = sxys.isDrawLines();
        boolean filled = sxys.getFilled();
        Shape shape = sxys.getSymbol();
        boolean drawMarkers = sxys.isDrawMarkers() & shape != null;
        int tooltipThresholdCounts = -1; /* number of points to be shown below which markers are also to be drawn */
        if (drawLines && drawMarkers && shape != null && tooltipThreshold != 0) {
            Rectangle shapeBoundingBox = shape.getBounds();
            tooltipThresholdCounts = (int) dataArea.getWidth()
                    / (Math.max(1, shapeBoundingBox.width) * tooltipThreshold);
        }

        java.util.Vector<ddouble> markerPositions = null;
        Shape entityArea = null;
        EntityCollection entities = null;
        if (info != null) {
            entities = info.getOwner().getEntityCollection();
        }
        boolean prevLineAdded = false;
        // In case the iterator does not work then use the TODO comment iterator use and why not always
        // comment variables
        synchronized (list) {
            Iterator<XYDataItem> iter = list.iterator();
            /*
             * loop over all points calculating X1 and Y1. Store previous points positions into X0 and Y0 The
             * variable addThis determines if the current point is to be added to the path If previous line was
             * added that the current must be even if off the screen - but in this case the flag prevLineAdded is
             * set false so that the next does not have to be added.
             */
            for (int item = 0; iter.hasNext(); item++, x0 = x1, y0 = y1, x1 = -1, y1 = -1) {
                XYDataItem dataitem = iter.next();
                double x = dataitem.getX().doubleValue();
                double y = dataitem.getY().doubleValue();
                x = xValueTransformer.transformValue(x);

                x1 = MX_MY_NaN ? (int) domainAxis.valueToJava2D(x, dataArea, xAxisLocation)
                        : (int) ((x - xl) * mx + cx);
                y1 = MX_MY_NaN ? (int) rangeAxis.valueToJava2D(y, dataArea, yAxisLocation)
                        : (int) ((y - yl) * my + cy);

                boolean addThis = true;
                if (item == 0) {
                    x0 = x1;
                    y0 = y1;
                    if ((x1 < xmin) || (x1 > xmax) || (y1 < ymin) || (y1 > ymax)) {
                        addThis = false;
                    }
                } else {
                    if (x1 == x0 && y1 == y0) {
                        addThis = false;
                    }
                    if ((x1 < xmin && x0 < xmin) || (x1 > xmax && x0 > xmax) || (y1 < ymin && y0 < ymin)
                            || (y1 > ymax && y0 > ymax)) {
                        if (prevLineAdded) {
                            path = addPointToLine(path, orientation, x1, y1);
                        }
                        addThis = false;
                    }
                }
                if (addThis) {
                    /*
                     * If the current point is to be added then ensure previous one is as well to prevent lines
                     * not crossing the edge of the screen
                     */
                    if (!prevLineAdded) {
                        path = addPointToLine(path, orientation, x0, y0);
                    }
                    path = addPointToLine(path, orientation, x1, y1);
                    prevLineAdded = true;
                }
                prevLineAdded = addThis;
                if (addThis && drawMarkers) {
                    if (markerPositions == null) {
                        markerPositions = new java.util.Vector<ddouble>();
                    }
                    markerPositions.add(new ddouble(item, x1, y1));
                    if (tooltipThresholdCounts != -1 && markerPositions.size() > tooltipThresholdCounts) {
                        drawMarkers = false;
                        markerPositions = null;
                    }
                }
            }
            if (path != null) {
                g2.setStroke(stroke);
                g2.setPaint(paint);
                g2.draw(path);
            }
            if (markerPositions != null) {
                if (drawMarkers) {
                    g2.setPaint(paint_symbol);
                    g2.setStroke(stroke_symbol);
                    for (ddouble dd : markerPositions) {
                        Shape shape_item = ShapeUtilities.createTranslatedShape(shape, dd.x, dd.y);
                        if (filled) {
                            g2.fill(shape_item);
                        } else {
                            g2.draw(shape_item);
                        }
                        entityArea = shape_item;
                        // add an entity for the item...
                        if (entities != null) {
                            addEntity(entities, entityArea, dataset, series, dd.item, dd.x, dd.y);
                        }
                    }
                    g2.setPaint(paint);
                    g2.setStroke(stroke);
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.hexidec.ekit.component.RelativeImageView.java

/**
  * Paints the image.//from  www.  j a  v  a2  s .  c  o  m
  *
  * @param g the rendering surface to use
  * @param a the allocated region to render into
  * @see View#paint
  */
public void paint(Graphics g, Shape a) {
    Color oldColor = g.getColor();
    fBounds = a.getBounds();
    int border = getBorder();
    int x = fBounds.x + border + getSpace(X_AXIS);
    int y = fBounds.y + border + getSpace(Y_AXIS);
    int width = fWidth;
    int height = fHeight;
    int sel = getSelectionState();

    // If no pixels yet, draw gray outline and icon
    if (!hasPixels(this)) {
        g.setColor(Color.lightGray);
        g.drawRect(x, y, width - 1, height - 1);
        g.setColor(oldColor);
        loadImageStatusIcons();
        Icon icon = ((fImage == null) ? sMissingImageIcon : sPendingImageIcon);
        if (icon != null) {
            icon.paintIcon(getContainer(), g, x, y);
        }
    }

    // Draw image
    if (fImage != null) {
        g.drawImage(fImage, x, y, width, height, this);
    }

    // If selected exactly, we need a black border & grow-box
    Color bc = getBorderColor();
    if (sel == 2) {
        // Make sure there's room for a border
        int delta = 2 - border;
        if (delta > 0) {
            x += delta;
            y += delta;
            width -= delta << 1;
            height -= delta << 1;
            border = 2;
        }
        bc = null;
        g.setColor(Color.black);
        // Draw grow box
        g.fillRect(x + width - 5, y + height - 5, 5, 5);
    }

    // Draw border
    if (border > 0) {
        if (bc != null) {
            g.setColor(bc);
        }
        // Draw a thick rectangle:
        for (int i = 1; i <= border; i++) {
            g.drawRect(x - i, y - i, width - 1 + i + i, height - 1 + i + i);
        }
        g.setColor(oldColor);
    }
}

From source file:org.csstudio.utility.batik.SVGHandler.java

/**
 * @return document size after applying matrix.
 *///  w ww . jav a2s .c  om
public Dimension getDocumentSize() {
    Shape aoi = calculateShape();
    double docWidth = aoi.getBounds().getWidth();
    double docHeight = aoi.getBounds().getHeight();
    return new Dimension((int) Math.round(docWidth), (int) Math.round(docHeight));
}

From source file:org.csstudio.utility.batik.SVGHandler.java

private void updateMatrix() {
    Shape newAOI = calculateShape();
    double newX = newAOI.getBounds().x;
    double newY = newAOI.getBounds().y;
    double newWidth = newAOI.getBounds().width;
    double newHeight = newAOI.getBounds().height;

    // set the width and height attributes on the root element
    svgRootNode.setAttributeNS(null, "width", String.valueOf(newWidth));
    svgRootNode.setAttributeNS(null, "height", String.valueOf(newHeight));
    String vbs = newX + " " + newY + " " + newWidth + " " + newHeight;
    svgRootNode.setAttributeNS(null, "viewBox", vbs);
    svgRootNode.setAttributeNS(null, "preserveAspectRatio", "none");

    // current Transformation Matrix
    double[][] CTM = { { matrix[0][0], matrix[0][1], 0 }, { matrix[1][0], matrix[1][1], 0 }, { 0, 0, 1 } };
    // create the transform matrix
    StringBuilder sb = new StringBuilder();
    sb.append("matrix(");
    sb.append(CTM[0][0] + ",");
    sb.append(CTM[1][0] + ",");
    sb.append(CTM[0][1] + ",");
    sb.append(CTM[1][1] + ",");
    sb.append(CTM[0][2] + ",");
    sb.append(CTM[1][2] + ")");

    mainGraphicNode.setAttributeNS(null, "transform", sb.toString());
}

From source file:tilt.image.page.Line.java

/**
 * Add a shape (polygon or rectangle) to the line. Keep it sorted.
 * @param s the shape to add/*from   ww  w .java  2 s  .  c o m*/
 */
public void add(Polygon s) {
    int i;
    Rectangle sBounds = s.getBounds();
    for (i = 0; i < shapes.size(); i++) {
        Shape t = shapes.get(i);
        Rectangle bounds = t.getBounds();
        if (bounds.x >= sBounds.x) {
            shapes.add(i, s);
            break;
        }
    }
    if (i == shapes.size())
        shapes.add(s);
    total++;
}

From source file:org.squidy.designer.shape.ZoomShape.java

/**
 * @param paintContext/*  w  w w . ja v  a2  s.  c o  m*/
 */
protected void paintShapeZoomedIn(PPaintContext paintContext) {

    Shape shapeZoomedIn = getZoomedInShape();
    if (shapeZoomedIn != null) {
        Graphics2D g = paintContext.getGraphics();
        Rectangle bounds = shapeZoomedIn.getBounds();

        g.setPaint(getZoomedInFillPaint());
        if (isRenderPrimitiveRect())
            g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.fill(shapeZoomedIn);

        g.setStroke(STROKE_ZOOMED_IN);
        g.setPaint(getZoomedInDrawPaint());
        if (isRenderPrimitiveRect())
            g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.draw(shapeZoomedIn);
    }
}

From source file:org.squidy.designer.shape.ZoomShape.java

/**
 * @param paintContext/*from ww  w.  ja  v a2  s.  c  o m*/
 */
protected void paintShapeZoomedOut(PPaintContext paintContext) {

    Shape shapeZoomedOut = getZoomedOutShape();
    if (shapeZoomedOut != null) {
        Graphics2D g = paintContext.getGraphics();
        Rectangle bounds = shapeZoomedOut.getBounds();

        g.setPaint(getZoomedOutFillPaint());
        if (isRenderPrimitiveRect())
            g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.fill(shapeZoomedOut);

        g.setStroke(STROKE_ZOOMED_OUT);
        g.setPaint(getZoomedOutDrawPaint());
        if (isRenderPrimitiveRect())
            g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.draw(shapeZoomedOut);
    }
}

From source file:com.hexidec.ekit.component.RelativeImageView.java

/** Provides a mapping from the document model coordinate space
  * to the coordinate space of the view mapped to it.
  *//from w  w  w.  j  ava2  s  .  c  om
  * @param pos the position to convert
  * @param a the allocated region to render into
  * @return the bounding box of the given position
  * @exception BadLocationException if the given position does not represent a
  *   valid location in the associated document
  * @see View#modelToView
  */
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
    int p0 = getStartOffset();
    int p1 = getEndOffset();
    if ((pos >= p0) && (pos <= p1)) {
        Rectangle r = a.getBounds();
        if (pos == p1) {
            r.x += r.width;
        }
        r.width = 0;
        return r;
    }
    return null;
}

From source file:org.pmedv.core.components.RelativeImageView.java

/**
 * Paints the image.//from  w w  w  .  j  a  va  2 s .  c om
 * 
 * @param g
 *            the rendering surface to use
 * @param a
 *            the allocated region to render into
 * @see View#paint
 */
public void paint(Graphics g, Shape a) {

    Color oldColor = g.getColor();
    fBounds = a.getBounds();
    int border = getBorder();
    int x = fBounds.x + border + getSpace(X_AXIS);
    int y = fBounds.y + border + getSpace(Y_AXIS);
    int width = fWidth;
    int height = fHeight;
    int sel = getSelectionState();

    // If no pixels yet, draw gray outline and icon
    if (!hasPixels(this)) {
        g.setColor(Color.lightGray);
        g.drawRect(x, y, width - 1, height - 1);
        g.setColor(oldColor);
        loadImageStatusIcons();
        Icon icon = ((fImage == null) ? sMissingImageIcon : sPendingImageIcon);
        if (icon != null) {
            icon.paintIcon(getContainer(), g, x, y);
        }
    }

    // Draw image
    if (fImage != null) {
        g.drawImage(fImage, x, y, width, height, this);
    }

    // If selected exactly, we need a black border & grow-box
    Color bc = getBorderColor();
    if (sel == 2) {
        // Make sure there's room for a border
        int delta = 2 - border;
        if (delta > 0) {
            x += delta;
            y += delta;
            width -= delta << 1;
            height -= delta << 1;
            border = 2;
        }
        bc = null;
        g.setColor(Color.black);
        // Draw grow box
        g.fillRect(x + width - 5, y + height - 5, 5, 5);
    }

    // Draw border
    if (border > 0) {
        if (bc != null) {
            g.setColor(bc);
        }
        // Draw a thick rectangle:
        for (int i = 1; i <= border; i++) {
            g.drawRect(x - i, y - i, width - 1 + i + i, height - 1 + i + i);
        }
        g.setColor(oldColor);
    }
}