⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shapeutilities.java

📁 该源代码为一些通用的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param transY  the y translation (in Java2D space).
     * 
     * @return The translated shape.
     */
    public static Shape createTranslatedShape(final Shape shape,
                                              final double transX, 
                                              final double transY) {
        if (shape == null) {
            throw new IllegalArgumentException("Null 'shape' argument.");
        }
        final AffineTransform transform = AffineTransform.getTranslateInstance(
            transX, transY
        );
        return transform.createTransformedShape(shape);
    }
    
    /**
     * Translates a shape to a new location such that the anchor point 
     * (relative to the rectangular bounds of the shape) aligns with the 
     * specified (x, y) coordinate in Java2D space.
     *  
     * @param shape  the shape (<code>null</code> not permitted).
     * @param anchor  the anchor (<code>null</code> not permitted).
     * @param locationX  the x-coordinate (in Java2D space).
     * @param locationY  the y-coordinate (in Java2D space).
     * 
     * @return A new and translated shape.
     */
    public static Shape createTranslatedShape(final Shape shape, 
                                              final RectangleAnchor anchor, 
                                              final double locationX,
                                              final double locationY) {
        if (shape == null) {
            throw new IllegalArgumentException("Null 'shape' argument.");
        }        
        if (anchor == null) {
            throw new IllegalArgumentException("Null 'anchor' argument.");
        }
        Point2D anchorPoint = RectangleAnchor.coordinates(
            shape.getBounds2D(), anchor
        );
        final AffineTransform transform = AffineTransform.getTranslateInstance(
            locationX - anchorPoint.getX(), locationY - anchorPoint.getY()
        );
        return transform.createTransformedShape(shape);   
    }

    /**
     * Rotates a shape about the specified coordinates.
     * 
     * @param base  the shape (<code>null</code> permitted, returns 
     *              <code>null</code>).
     * @param angle  the angle (in radians).
     * @param x  the x coordinate for the rotation point (in Java2D space).
     * @param y  the y coordinate for the rotation point (in Java2D space).
     * 
     * @return the rotated shape.
     */
    public static Shape rotateShape(final Shape base, final double angle,
                                    final float x, final float y) {
        if (base == null) {
            return null;
        }
        final AffineTransform rotate = AffineTransform.getRotateInstance(
            angle, x, y
        );
        final Shape result = rotate.createTransformedShape(base);
        return result;
    }

    /**
     * Draws a shape with the specified rotation about <code>(x, y)</code>.
     *
     * @param g2  the graphics device (<code>null</code> not permitted).
     * @param shape  the shape (<code>null</code> not permitted).
     * @param angle  the angle (in radians).
     * @param x  the x coordinate for the rotation point.
     * @param y  the y coordinate for the rotation point.
     */
    public static void drawRotatedShape(final Graphics2D g2, final Shape shape,
                                        final double angle,
                                        final float x, final float y) {

        final AffineTransform saved = g2.getTransform();
        final AffineTransform rotate = AffineTransform.getRotateInstance(
            angle, x, y
        );
        g2.transform(rotate);
        g2.draw(shape);
        g2.setTransform(saved);

    }

    /** A useful constant used internally. */
    private static final float SQRT2 = (float) Math.pow(2.0, 0.5);

    /**
     * Creates a diagonal cross shape.
     * 
     * @param l  the length of each 'arm'.
     * @param t  the thickness.
     * 
     * @return A diagonal cross shape.
     */
    public static Shape createDiagonalCross(final float l, final float t) {
        final GeneralPath p0 = new GeneralPath();
        p0.moveTo(-l - t, -l + t);
        p0.lineTo(-l + t, -l - t);
        p0.lineTo(0.0f, -t * SQRT2);
        p0.lineTo(l - t, -l - t);
        p0.lineTo(l + t, -l + t);
        p0.lineTo(t * SQRT2, 0.0f);
        p0.lineTo(l + t, l - t);
        p0.lineTo(l - t, l + t);
        p0.lineTo(0.0f, t * SQRT2);
        p0.lineTo(-l + t, l + t);
        p0.lineTo(-l - t, l - t);
        p0.lineTo(-t * SQRT2, 0.0f);
        p0.closePath();
        return p0;
    }
    
    /**
     * Creates a diagonal cross shape.
     * 
     * @param l  the length of each 'arm'.
     * @param t  the thickness.
     * 
     * @return A diagonal cross shape.
     */
    public static Shape createRegularCross(final float l, final float t) {
        final GeneralPath p0 = new GeneralPath();
        p0.moveTo(-l, t);
        p0.lineTo(-t, t);
        p0.lineTo(-t, l);
        p0.lineTo(t, l);
        p0.lineTo(t, t);
        p0.lineTo(l, t);
        p0.lineTo(l, -t);
        p0.lineTo(t, -t);
        p0.lineTo(t, -l);
        p0.lineTo(-t, -l);
        p0.lineTo(-t, -t);
        p0.lineTo(-l, -t);
        p0.closePath();
        return p0;
    }
    
    /**
     * Creates a diamond shape.
     * 
     * @param s  the size factor (equal to half the height of the diamond).
     * 
     * @return A diamond shape.
     */
    public static Shape createDiamond(final float s) {
        final GeneralPath p0 = new GeneralPath();
        p0.moveTo(0.0f, -s);
        p0.lineTo(s, 0.0f);
        p0.lineTo(0.0f, s);
        p0.lineTo(-s, 0.0f);
        p0.closePath();
        return p0;
    }
    
    /**
     * Creates a triangle shape that points upwards.
     * 
     * @param s  the size factor (equal to half the height of the triangle).
     * 
     * @return A triangle shape.
     */
    public static Shape createUpTriangle(final float s) {
        final GeneralPath p0 = new GeneralPath();
        p0.moveTo(0.0f, -s);
        p0.lineTo(s, s);
        p0.lineTo(-s, s);
        p0.closePath();
        return p0;
    }

    /**
     * Creates a triangle shape that points downwards.
     * 
     * @param s  the size factor (equal to half the height of the triangle).
     * 
     * @return A triangle shape.
     */
    public static Shape createDownTriangle(final float s) {
        final GeneralPath p0 = new GeneralPath();
        p0.moveTo(0.0f, s);
        p0.lineTo(s, -s);
        p0.lineTo(-s, -s);
        p0.closePath();
        return p0;
    }

    /**
     * Creates a region surrounding a line segment by 'widening' the line 
     * segment.  A typical use for this method is the creation of a 
     * 'clickable' region for a line that is displayed on-screen.
     * 
     * @param line  the line (<code>null</code> not permitted).
     * @param width  the width of the region.
     * 
     * @return A region that surrounds the line.
     */
    public static Shape createLineRegion(final Line2D line, final float width) {
        final GeneralPath result = new GeneralPath();
        final float x1 = (float) line.getX1();
        final float x2 = (float) line.getX2();
        final float y1 = (float) line.getY1();
        final float y2 = (float) line.getY2();
        if ((x2 - x1) != 0.0) {
            final double theta = Math.atan((y2 - y1) / (x2 - x1));
            final float dx = (float) Math.sin(theta) * width;
            final float dy = (float) Math.cos(theta) * width;
            result.moveTo(x1 - dx, y1 + dy);
            result.lineTo(x1 + dx, y1 - dy);
            result.lineTo(x2 + dx, y2 - dy);
            result.lineTo(x2 - dx, y2 + dy);
            result.closePath();
        }
        else {
            // special case, vertical line
            result.moveTo(x1 - width / 2.0f, y1);
            result.lineTo(x1 + width / 2.0f, y1);
            result.lineTo(x2 + width / 2.0f, y2);
            result.lineTo(x2 - width / 2.0f, y2);
            result.closePath();
        }
        return result;    
    }
        
    /**
     * Returns a point based on (x, y) but constrained to be within the bounds 
     * of a given rectangle.
     *
     * @param x  the x-coordinate.
     * @param y  the y-coordinate.
     * @param area  the constraining rectangle (<code>null</code> not 
     *              permitted).
     *
     * @return A point within the rectangle.
     * 
     * @throws NullPointerException if <code>area</code> is <code>null</code>.
     */
    public static Point2D getPointInRectangle(double x, double y, 
                                              final Rectangle2D area) {

        x = Math.max(area.getMinX(), Math.min(x, area.getMaxX()));
        y = Math.max(area.getMinY(), Math.min(y, area.getMaxY()));
        return new Point2D.Double(x, y);

    }

    /**
     * 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())));
    
    }
    

    /**
     * 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 intersects (final Rectangle2D rect1, 
                                      final Rectangle2D rect2) {

      final double x0 = rect1.getX();
      final double y0 = rect1.getY();

      final double x = rect2.getX();
      final double width = rect2.getWidth();
      final double y = rect2.getY();
      final double height = rect2.getHeight();
      return (x + width >= x0 && y + height >= y0 && x <= x0 + rect1.getWidth()
              && y <= y0 + rect1.getHeight());
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -