Java Code Examples for java.awt.geom.Rectangle2D#getX()

The following examples show how to use java.awt.geom.Rectangle2D#getX() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: jMutRepair_0038_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Draws an axis line at the current cursor position and edge.
 * 
 * @param g2  the graphics device.
 * @param cursor  the cursor position.
 * @param dataArea  the data area.
 * @param edge  the edge.
 */
protected void drawAxisLine(Graphics2D g2, double cursor,
        Rectangle2D dataArea, RectangleEdge edge) {
    
    Line2D axisLine = null;
    if (edge == RectangleEdge.TOP) {
        axisLine = new Line2D.Double(dataArea.getX(), cursor, 
                dataArea.getMaxX(), cursor);  
    }
    else if (edge == RectangleEdge.BOTTOM) {
        axisLine = new Line2D.Double(dataArea.getX(), cursor, 
                dataArea.getMaxX(), cursor);  
    }
    else if (edge == RectangleEdge.LEFT) {
        axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor, 
                dataArea.getMaxY());  
    }
    else if (edge == RectangleEdge.RIGHT) {
        axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor, 
                dataArea.getMaxY());  
    }
    g2.setPaint(this.axisLinePaint);
    g2.setStroke(this.axisLineStroke);
    g2.draw(axisLine);
    
}
 
Example 2
Source File: DialCap.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Draws the background to the specified graphics device.  If the dial
 * frame specifies a window, the clipping region will already have been
 * set to this window before this method is called.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param plot  the plot (ignored here).
 * @param frame  the dial frame (ignored here).
 * @param view  the view rectangle (<code>null</code> not permitted).
 */
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
        Rectangle2D view) {

    g2.setPaint(this.fillPaint);

    Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius,
            this.radius);
    Ellipse2D e = new Ellipse2D.Double(f.getX(), f.getY(), f.getWidth(),
            f.getHeight());
    g2.fill(e);
    g2.setPaint(this.outlinePaint);
    g2.setStroke(this.outlineStroke);
    g2.draw(e);

}
 
Example 3
Source File: TextLayout.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private GeneralPath leftShape(Rectangle2D bounds) {

        double[] path0;
        if (isVerticalLine) {
            path0 = new double[] { bounds.getX(), bounds.getY(),
                                       bounds.getX() + bounds.getWidth(),
                                       bounds.getY() };
        } else {
            path0 = new double[] { bounds.getX(),
                                       bounds.getY() + bounds.getHeight(),
                                       bounds.getX(), bounds.getY() };
        }

        double[] path1 = getCaretPath(0, bounds, true);

        return boundingShape(path0, path1);
    }
 
Example 4
Source File: ModuloAxis.java    From opensim-gui with Apache License 2.0 6 votes vote down vote up
/**
 * A regular translation from a data value to a Java2D value.
 * 
 * @param value  the value.
 * @param area  the data area.
 * @param edge  the edge along which the axis lies.
 * 
 * @return The Java2D coordinate.
 */
private double trans(double value, Rectangle2D area, RectangleEdge edge) {
    double min = 0.0;
    double max = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        min = area.getX();
        max = area.getX() + area.getWidth();
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        min = area.getMaxY();
        max = area.getMaxY() - area.getHeight();
    }
    if (isInverted()) {
        return max - ((value - this.displayStart) 
               / (this.displayEnd - this.displayStart)) * (max - min);
    }
    else {
        return min + ((value - this.displayStart) 
               / (this.displayEnd - this.displayStart)) * (max - min);
    }

}
 
Example 5
Source File: ShapeUtilities.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Checks, whether the given rectangle1 fully contains rectangle 2
 * (even if rectangle 2 has a height or width of zero!).
 *
 * @param rect1  the first rectangle.
 * @param rect2  the second rectangle.
 *
 * @return A boolean.
 */
public static boolean contains(final Rectangle2D rect1,
                               final Rectangle2D rect2) {

    final double x0 = rect1.getX();
    final double y0 = rect1.getY();
    final double x = rect2.getX();
    final double y = rect2.getY();
    final double w = rect2.getWidth();
    final double h = rect2.getHeight();

    return ((x >= x0) && (y >= y0)
            && ((x + w) <= (x0 + rect1.getWidth()))
            && ((y + h) <= (y0 + rect1.getHeight())));

}
 
Example 6
Source File: jKali_0032_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Draws an axis line at the current cursor position and edge.
 * 
 * @param g2  the graphics device.
 * @param cursor  the cursor position.
 * @param dataArea  the data area.
 * @param edge  the edge.
 */
protected void drawAxisLine(Graphics2D g2, double cursor,
        Rectangle2D dataArea, RectangleEdge edge) {
    
    Line2D axisLine = null;
    if (edge == RectangleEdge.TOP) {
        axisLine = new Line2D.Double(dataArea.getX(), cursor, 
                dataArea.getMaxX(), cursor);  
    }
    else if (edge == RectangleEdge.BOTTOM) {
        axisLine = new Line2D.Double(dataArea.getX(), cursor, 
                dataArea.getMaxX(), cursor);  
    }
    else if (edge == RectangleEdge.LEFT) {
        axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor, 
                dataArea.getMaxY());  
    }
    else if (edge == RectangleEdge.RIGHT) {
        axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor, 
                dataArea.getMaxY());  
    }
    g2.setPaint(this.axisLinePaint);
    g2.setStroke(this.axisLineStroke);
    g2.draw(axisLine);
    
}
 
Example 7
Source File: GridElement.java    From orson-charts with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Performs a layout of this table element, returning a list of bounding
 * rectangles for the element and its subelements.
 * 
 * @param g2  the graphics target.
 * @param bounds  the bounds.
 * @param constraints  the constraints (if any).
 * 
 * @return A list of bounding rectangles. 
 */
@Override
public List<Rectangle2D> layoutElements(Graphics2D g2, Rectangle2D bounds, 
        Map<String, Object> constraints) {
    double[][] cellDimensions = findCellDimensions(g2, bounds);
    double[] widths = cellDimensions[0];
    double[] heights = cellDimensions[1];
    List<Rectangle2D> result = new ArrayList<>(
            this.elements.getRowCount() * this.elements.getColumnCount());
    double y = bounds.getY() + getInsets().top;
    for (int r = 0; r < elements.getRowCount(); r++) {
        double x = bounds.getX() + getInsets().left;
        for (int c = 0; c < this.elements.getColumnCount(); c++) {
            Rectangle2D cellBounds = new Rectangle2D.Double(x, y, widths[c], heights[r]);
            TableElement element = this.elements.getValue(r, c);
            element.layoutElements(g2, cellBounds, null);
            result.add(cellBounds);
            x += widths[c];
        }
        y = y + heights[r];
    }
    return result;
}
 
Example 8
Source File: MarkerAxisBand.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * A utility method that draws a string inside a rectangle.
 *
 * @param g2  the graphics device.
 * @param bounds  the rectangle.
 * @param font  the font.
 * @param text  the text.
 */
private void drawStringInRect(Graphics2D g2, Rectangle2D bounds, Font font,
                              String text) {

    g2.setFont(font);
    FontMetrics fm = g2.getFontMetrics(font);
    Rectangle2D r = TextUtilities.getTextBounds(text, g2, fm);
    double x = bounds.getX();
    if (r.getWidth() < bounds.getWidth()) {
        x = x + (bounds.getWidth() - r.getWidth()) / 2;
    }
    LineMetrics metrics = font.getLineMetrics(
        text, g2.getFontRenderContext()
    );
    g2.drawString(
        text, (float) x, (float) (bounds.getMaxY()
            - this.bottomInnerGap - metrics.getDescent())
    );
}
 
Example 9
Source File: DrawPictureShape.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resize this picture to the default size.
 *
 * For PNG and JPEG resizes the image to 100%,
 * for other types, if the size can't be determined it will be 200x200 pixels.
 */
public void resize() {
    PictureShape<?,?> ps = getShape();
    Dimension dim = ps.getPictureData().getImageDimension();

    Rectangle2D origRect = ps.getAnchor();
    double x = origRect.getX();
    double y = origRect.getY();
    double w = dim.getWidth();
    double h = dim.getHeight();
    ps.setAnchor(new Rectangle2D.Double(x, y, w, h));
}
 
Example 10
Source File: SunGraphics2D.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected static Shape transformShape(AffineTransform tx, Shape clip) {
    if (clip == null) {
        return null;
    }

    if (clip instanceof Rectangle2D &&
        (tx.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0)
    {
        Rectangle2D rect = (Rectangle2D) clip;
        double matrix[] = new double[4];
        matrix[0] = rect.getX();
        matrix[1] = rect.getY();
        matrix[2] = matrix[0] + rect.getWidth();
        matrix[3] = matrix[1] + rect.getHeight();
        tx.transform(matrix, 0, matrix, 0, 2);
        fixRectangleOrientation(matrix, rect);
        return new Rectangle2D.Double(matrix[0], matrix[1],
                                      matrix[2] - matrix[0],
                                      matrix[3] - matrix[1]);
    }

    if (tx.isIdentity()) {
        return cloneShape(clip);
    }

    return tx.createTransformedShape(clip);
}
 
Example 11
Source File: AxisSpace.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates the reserved area.
 *
 * @param area  the area.
 * @param edge  the edge.
 *
 * @return The reserved area.
 */
public Rectangle2D reserved(Rectangle2D area, RectangleEdge edge) {
    Rectangle2D result = null;
    if (edge == RectangleEdge.TOP) {
        result = new Rectangle2D.Double(
            area.getX(), area.getY(), area.getWidth(), this.top
        );
    }
    else if (edge == RectangleEdge.BOTTOM) {
        result = new Rectangle2D.Double(
            area.getX(), area.getMaxY() - this.top,
            area.getWidth(), this.bottom
        );
    }
    else if (edge == RectangleEdge.LEFT) {
        result = new Rectangle2D.Double(
            area.getX(), area.getY(), this.left, area.getHeight()
        );
    }
    else if (edge == RectangleEdge.RIGHT) {
        result = new Rectangle2D.Double(
            area.getMaxX() - this.right, area.getY(),
            this.right, area.getHeight()
        );
    }
    return result;
}
 
Example 12
Source File: JGenProg2017_00127_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Creates a rectangle that is aligned to the frame.
 * 
 * @param dimensions
 * @param frame
 * @param hAlign
 * @param vAlign
 * 
 * @return A rectangle.
 */
private Rectangle2D createAlignedRectangle2D(Size2D dimensions, 
        Rectangle2D frame, HorizontalAlignment hAlign, 
        VerticalAlignment vAlign) {
    double x = Double.NaN;
    double y = Double.NaN;
    if (hAlign == HorizontalAlignment.LEFT) {
        x = frame.getX();   
    }
    else if (hAlign == HorizontalAlignment.CENTER) {
        x = frame.getCenterX() - (dimensions.width / 2.0);   
    }
    else if (hAlign == HorizontalAlignment.RIGHT) {
        x = frame.getMaxX() - dimensions.width;   
    }
    if (vAlign == VerticalAlignment.TOP) {
        y = frame.getY();   
    }
    else if (vAlign == VerticalAlignment.CENTER) {
        y = frame.getCenterY() - (dimensions.height / 2.0);   
    }
    else if (vAlign == VerticalAlignment.BOTTOM) {
        y = frame.getMaxY() - dimensions.height;   
    }
    
    return new Rectangle2D.Double(x, y, dimensions.width, 
            dimensions.height);
}
 
Example 13
Source File: ModuloAxis.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a data value to a Java2D value for the first section of the
 * axis.
 *
 * @param value  the value.
 * @param area  the data area.
 * @param edge  the edge along which the axis lies.
 * @param length1  the length of the first section.
 * @param length2  the length of the second section.
 *
 * @return The Java2D coordinate.
 */
private double transStart(double value, Rectangle2D area,
                          RectangleEdge edge,
                          double length1, double length2) {
    double min = 0.0;
    double max = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        min = area.getX();
        max = area.getX() + area.getWidth() * length1 / (length1 + length2);
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        min = area.getMaxY();
        max = area.getMaxY() - area.getHeight() * length1
              / (length1 + length2);
    }
    if (isInverted()) {
        return max - ((value - this.displayStart)
            / (this.fixedRange.getUpperBound() - this.displayStart))
            * (max - min);
    }
    else {
        return min + ((value - this.displayStart)
            / (this.fixedRange.getUpperBound() - this.displayStart))
            * (max - min);
    }

}
 
Example 14
Source File: NamedButtonRegionPainter.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
private Paint decodeGradient4(Shape s) {
    Rectangle2D bounds = s.getBounds2D();
    float x = (float) bounds.getX();
    float y = (float) bounds.getY();
    float w = (float) bounds.getWidth();
    float h = (float) bounds.getHeight();
    return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y,
            new float[]{0.05f, 0.5f, 0.95f},
            new Color[]{color18,
                    decodeColor(color18, color19, 0.5f),
                    color19});
}
 
Example 15
Source File: WaferMapPlot.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates the location of the waferedge.
 *
 * @param plotArea  the plot area.
 *
 * @return The wafer edge.
 */
protected Ellipse2D getWaferEdge(Rectangle2D plotArea) {
    Ellipse2D edge = new Ellipse2D.Double();
    double diameter = plotArea.getWidth();
    double upperLeftX = plotArea.getX();
    double upperLeftY = plotArea.getY();
    //get major dimension
    if (plotArea.getWidth() != plotArea.getHeight()) {
        double major, minor;
        if (plotArea.getWidth() > plotArea.getHeight()) {
            major = plotArea.getWidth();
            minor = plotArea.getHeight();
        }
        else {
            major = plotArea.getHeight();
            minor = plotArea.getWidth();
        }
        //ellipse diameter is the minor dimension
        diameter = minor;
        //set upperLeft point
        if (plotArea.getWidth() == minor) { // x is minor
            upperLeftY = plotArea.getY() + (major - minor) / 2;
        }
        else { // y is minor
            upperLeftX = plotArea.getX() + (major - minor) / 2;
        }
    }
    edge.setFrame(upperLeftX, upperLeftY, diameter, diameter);
    return edge;
}
 
Example 16
Source File: Cardumen_0077_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Creates a rectangle that is aligned to the frame.
 * 
 * @param dimensions  the dimensions for the rectangle.
 * @param frame  the frame to align to.
 * @param hAlign  the horizontal alignment.
 * @param vAlign  the vertical alignment.
 * 
 * @return A rectangle.
 */
private Rectangle2D createAlignedRectangle2D(Size2D dimensions, 
        Rectangle2D frame, HorizontalAlignment hAlign, 
        VerticalAlignment vAlign) {
    double x = Double.NaN;
    double y = Double.NaN;
    if (hAlign == HorizontalAlignment.LEFT) {
        x = frame.getX();   
    }
    else if (hAlign == HorizontalAlignment.CENTER) {
        x = frame.getCenterX() - (dimensions.width / 2.0);   
    }
    else if (hAlign == HorizontalAlignment.RIGHT) {
        x = frame.getMaxX() - dimensions.width;   
    }
    if (vAlign == VerticalAlignment.TOP) {
        y = frame.getY();   
    }
    else if (vAlign == VerticalAlignment.CENTER) {
        y = frame.getCenterY() - (dimensions.height / 2.0);   
    }
    else if (vAlign == VerticalAlignment.BOTTOM) {
        y = frame.getMaxY() - dimensions.height;   
    }
    
    return new Rectangle2D.Double(x, y, dimensions.width, 
            dimensions.height);
}
 
Example 17
Source File: DateAxis.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Translates a Java2D coordinate into the corresponding data value.  To 
 * perform this translation, you need to know the area used for plotting 
 * data, and which edge the axis is located on.
 *
 * @param java2DValue  the coordinate in Java2D space.
 * @param area  the rectangle (in Java2D space) where the data is to be 
 *              plotted.
 * @param edge  the axis location.
 *
 * @return A data value.
 */
public double java2DToValue(double java2DValue, Rectangle2D area, 
                            RectangleEdge edge) {
    
    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerDate());
    double axisMax = this.timeline.toTimelineValue(range.getUpperDate());

    double min = 0.0;
    double max = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        min = area.getX();
        max = area.getMaxX();
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        min = area.getMaxY();
        max = area.getY();
    }

    double result;
    if (isInverted()) {
         result = axisMax - ((java2DValue - min) / (max - min) 
                  * (axisMax - axisMin));
    }
    else {
         result = axisMin + ((java2DValue - min) / (max - min) 
                  * (axisMax - axisMin));
    }

    return this.timeline.toMillisecond((long) result); 
}
 
Example 18
Source File: TextTitle.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Draws a the title horizontally within the specified area.  This method 
 * will be called from the {@link #draw(Graphics2D, Rectangle2D) draw} 
 * method.
 * 
 * @param g2  the graphics device.
 * @param area  the area for the title.
 */
protected void drawHorizontal(Graphics2D g2, Rectangle2D area) {
    Rectangle2D titleArea = (Rectangle2D) area.clone();
    g2.setFont(this.font);
    g2.setPaint(this.paint);
    TextBlockAnchor anchor = null;
    float x = 0.0f;
    HorizontalAlignment horizontalAlignment = getHorizontalAlignment();
    if (horizontalAlignment == HorizontalAlignment.LEFT) {
        x = (float) titleArea.getX();
        anchor = TextBlockAnchor.TOP_LEFT;
    }
    else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
        x = (float) titleArea.getMaxX();
        anchor = TextBlockAnchor.TOP_RIGHT;
    }
    else if (horizontalAlignment == HorizontalAlignment.CENTER) {
        x = (float) titleArea.getCenterX();
        anchor = TextBlockAnchor.TOP_CENTER;
    }
    float y = 0.0f;
    RectangleEdge position = getPosition();
    if (position == RectangleEdge.TOP) {
        y = (float) titleArea.getY();
    }
    else if (position == RectangleEdge.BOTTOM) {
        y = (float) titleArea.getMaxY();
        if (horizontalAlignment == HorizontalAlignment.LEFT) {
            anchor = TextBlockAnchor.BOTTOM_LEFT;
        }
        else if (horizontalAlignment == HorizontalAlignment.CENTER) {
            anchor = TextBlockAnchor.BOTTOM_CENTER;
        }
        else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
            anchor = TextBlockAnchor.BOTTOM_RIGHT;
        }
    }
    this.content.draw(g2, x, y, anchor);
}
 
Example 19
Source File: StructureMapLayer.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Draws a structure on the map.
  * @param isSVG true if using a SVG image.
  * @param g2d the graphics2D context.
  * @param xLoc the X location from center of settlement (meters).
  * @param yLoc the y Location from center of settlement (meters).
  * @param width the structure width (meters).
  * @param length the structure length (meters).
  * @param facing the structure facing (degrees from North clockwise).
  * @param svg the SVG graphics node.
  * @param patternSVG the pattern SVG graphics node (null if no pattern).
  * @param color the color to display the rectangle if no SVG image.
  */
 private void drawStructure(
         boolean isSVG, Graphics2D g2d, double xLoc, double yLoc,
         double width, double length, double facing, GraphicsNode svg,
         GraphicsNode patternSVG, Color color) {

     // Save original graphics transforms.
     AffineTransform saveTransform = g2d.getTransform();
     // Save original stroke
     Stroke oldStroke = g2d.getStroke();
     
     // Determine bounds.
     Rectangle2D bounds = null;
     if (isSVG) bounds = svg.getBounds();
     else bounds = new Rectangle2D.Double(0, 0, width, length);

     // Determine transform information.
     double scalingWidth = width / bounds.getWidth() * scale;
     double scalingLength = length / bounds.getHeight() * scale;
     double boundsPosX = bounds.getX() * scalingWidth;
     double boundsPosY = bounds.getY() * scalingLength;
     double centerX = width * scale / 2D;
     double centerY = length * scale / 2D;
     double translationX = (-1D * xLoc * scale) - centerX - boundsPosX;
     double translationY = (-1D * yLoc * scale) - centerY - boundsPosY;
     double facingRadian = facing / 180D * Math.PI;

     AffineTransform newTransform = new AffineTransform();
     AffineTransform newTransform1 = new AffineTransform();
     
     // Apply graphic transforms for structure.		
     newTransform.translate(translationX, translationY);
     newTransform.rotate(facingRadian, centerX + boundsPosX, centerY + boundsPosY);
 
     if (isSVG) {
         // Draw buffered image of structure.
         BufferedImage image = getBufferedImage(svg, width, length, patternSVG);
         if (image != null) {
             g2d.transform(newTransform);
             
             if (mapPanel != null) {              	
             	g2d.drawImage(image, 0, 0, mapPanel);      
             }
         }
     }
     else {
         // Else draw colored rectangle for construction site.

         // Draw filled rectangle.
         newTransform.scale(scalingWidth, scalingLength);
         g2d.transform(newTransform);
         g2d.setColor(color);
         g2d.fill(bounds);
         
     	if (color.equals(SELECTED_CONSTRUCTION_SITE_COLOR)) {
             // Draw the dashed border
             g2d.setPaint(SITE_BORDER_COLOR);
             g2d.setStroke(dashed);
             g2d.draw(bounds);
             g2d.setStroke(oldStroke);
     	}
     }

     if (selected) {
	
     	newTransform1.scale(scalingWidth, scalingLength);
         g2d.transform(newTransform1);
      
// Draw the dashed border over the selected building
g2d.setPaint(SELECTED_BUILDING_BORDER_COLOR);
g2d.setStroke(THICK_DASHES);                                           
g2d.draw(bounds);
// Restore the stroke
g2d.setStroke(oldStroke);
     }
     
     // Restore original graphic transforms.
     g2d.setTransform(saveTransform);
 }
 
Example 20
Source File: DrawPictureShape.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Fit picture shape into the target rectangle, maintaining the aspect ratio
 * and repositioning within the target rectangle based on the specified
 * alignment (gravity).
 *
 * @param target    The target rectangle
 * @param align
 *            The alignment within the target rectangle when resizing.
 *            A null value corresponds to RectAlign.CENTER
 */
public void resize(Rectangle2D target, RectAlign align) {
    PictureShape<?,?> ps = getShape();
    Dimension dim = ps.getPictureData().getImageDimension();
    if (dim.width <= 0 || dim.height <= 0) {
        // nothing useful to be done for this case
        ps.setAnchor(target);
        return;
    }

    double w = target.getWidth();
    double h = target.getHeight();

    // scaling
    double sx = w / dim.width;
    double sy = h / dim.height;

    // position adjustments
    double dx = 0, dy = 0;

    if (sx > sy) {
        // use y-scaling for both, reposition x accordingly
        w  = sy * dim.width;
        dx = target.getWidth() - w;
    } else if (sy > sx) {
        // use x-scaling for both, reposition y accordingly
        h  = sx * dim.height;
        dy = target.getHeight() - h;
    } else {
        // uniform scaling, can use target values directly
        ps.setAnchor(target);
        return;
    }

    // the positioning
    double x = target.getX();
    double y = target.getY();
    switch (align) {
        case TOP:           // X=balance, Y=ok
            x += dx/2;
            break;
        case TOP_RIGHT:     // X=shift, Y=ok
            x += dx;
            break;
        case RIGHT:         // X=shift, Y=balance
            x += dx;
            y += dy/2;
            break;
        case BOTTOM_RIGHT:  // X=shift, Y=shift
            x += dx;
            y += dy;
            break;
        case BOTTOM:        // X=balance, Y=shift
            x += dx/2;
            y += dy;
            break;
        case BOTTOM_LEFT:   // X=ok, Y=shift
            y += dy;
            break;
        case LEFT:          // X=ok, Y=balance
            y += dy/2;
            break;
        case TOP_LEFT:      // X=ok, Y=ok
            /* no-op */
            break;
        default:            // CENTER: X=balance, Y=balance
            x += dx/2;
            y += dy/2;
            break;
    }

    ps.setAnchor(new Rectangle2D.Double(x, y, w, h));
}