自定义形状的旋转问题(custom shape rotation issue)

2019-08-16 21:51发布

我试图绕其中心的自定义形状,但如预期不能得到结果。

我想是

* 形状应围绕其中心不动,本身旋转。 *

目前有什么我的解决方案正在做的是旋转围绕其中心的整体形状,由每转其改变其立场。

我有多个形状,所以我已经创建了一个类来封装的形状,其在下面的类变换

public abstract class Shoe implements Shape, ShoeShape {

    // variable declaration

    /**
     * 
     */
    public Shoe() {
        position = new Point();
        lastPosition = new Point();
    }




    public void draw(Graphics2D g2, AffineTransform transform, boolean firstTime) {

        AffineTransform af = firstTime ? getInitTransform()
                : getCompositeTransform();

        if (af != null) {


                Shape s = af.createTransformedShape(this);

                if (getFillColor() != null) {
                    g2.setColor(getFillColor());
                    g2.fill(s);
                } else {
                    g2.draw(s);
                }
            }

        }



    }




    public AffineTransform getCompositeTransform() {
            AffineTransform af = new AffineTransform();
        af.setToIdentity();
        af.translate(position.getX(), position.getY());
        Point2D centerP = calculateShapeCenter();
        af.rotate(orientation, centerP.getX(), centerP.getY());
        return af;
    }







    public void onMouseDrag(MouseEvent me, Rectangle2D canvasBoundary,
            int selectionOperation) {

        // shape operation can be either resize , rotate , translate ,
        switch (selectionOperation) {
        case MmgShoeViewer.SHAPE_OPERATION_MOVE:
            // MOVEMENT
            break;
        case MmgShoeViewer.SHAPE_OPERATION_ROTATE:

            Point2D origin = calculateShapeCenter();
            Point2D.Double starting = new Point2D.Double(me.getX(), me.getY());
            currentAngle = RotationHelper.getAngle(origin, starting);
            rotationAngle = currentAngle - startingAngle;
            rotate(rotationAngle);
            break;
        case MmgShoeViewer.SHAPE_OPERATION_RESIZE:
            break;
        default:
            System.out.println(" invalid select operation");
        }
    }



    public void onMousePress(MouseEvent me, Rectangle2D canvasBoundary,
            int selectionOperation) {

        // shape operation can be either resize , rotate , translate ,
        switch (selectionOperation) {
        case MmgShoeViewer.SHAPE_OPERATION_MOVE:
            break;
        case MmgShoeViewer.SHAPE_OPERATION_ROTATE:
            Point2D origin =  calculateShapeCenter();
            Point2D.Double starting = new Point2D.Double(me.getX(), me.getY());
            startingAngle = RotationHelper.getAngle(origin, starting);
            setShapeOperation(selectionOperation);
            break;
        case MmgShoeViewer.SHAPE_OPERATION_RESIZE:
            break;
        default:
            System.out.println(" invalid select operation");
        }
    }

    public void onMouseRelease(MouseEvent me, Rectangle2D canvasBoundary,
            int selectionOperation) {

        // shape operation can be either resize , rotate , translate ,
        switch (selectionOperation) {
        case MmgShoeViewer.SHAPE_OPERATION_MOVE:
            break;
        case MmgShoeViewer.SHAPE_OPERATION_ROTATE:
            // FIXME rotation angle computation
            setShapeOperation(-1);
            break;
        case MmgShoeViewer.SHAPE_OPERATION_RESIZE:
            break;
        default:
            System.out.println(" invalid select operation");
        }
    }

    public void rotate(double angle) {
        orientation = (float) angle;
    }


    public void translate(double deltaX, double deltaY) {

        position.setLocation(deltaX, deltaY);
        lastPosition.setLocation(deltaX, deltaY);
    }







    // another getter and setter

我使用下面的方法计算的旋转角度

public static double getAngle(Point2D origin, Point2D other) {

        double dy = other.getY() - origin.getY();
        double dx = other.getX() - origin.getX();
        double angle;

        if (dx == 0) {// special case
            angle = dy >= 0 ? Math.PI / 2 : -Math.PI / 2;
        } else {
            angle = Math.atan(dy / dx);
            if (dx < 0) // hemisphere correction
                angle += Math.PI;
        }
        // all between 0 and 2PI
        if (angle < 0) // between -PI/2 and 0
            angle += 2 * Math.PI;
        return angle;
    }

在画布鼠标侦听器的鼠标新闻发布会

selectedShape.onMousePress(me, canvasBoundary, shoeViewer
                .getShapeOperation());

我打电话选定形状的onMousePress方法

并在画布鼠标监听我的鼠标拖动的方法,我打电话所选形状的onMouseDrag方法,更新旋转角度,你可以从第一类看

selectedShape.onMouseDrag(me, canvasBoundary, shoeViewer
                .getShapeOperation());

你可以看到各个造型的绘制方法,绘制根据当前变换,我打电话从类似的paintComponent的形状

Iterator<Shoe> shoeIter = shoeShapeMap.values().iterator();

        while (shoeIter.hasNext()) {

            Shoe shoe = shoeIter.next();
            shoe.draw(g2, firstTime);

        }

其中shoeShapeMap包含当前在画布上所有的自定义形状。

就是我在做计算的角度或确定锚点的错误? 我的当前的解决方案旋转通过检查所有的条件[90度等]作为可以在上述方法中看到形状360度。

我想要的形状应该围绕其中心旋转,不调整自己的立场? 它是很难解释的话,那么请给我建议什么更好的办法在这里展示我要完成?

我想我已经提到的所有与这个问题有关的事情。 如果您有任何疑问,请随时问我。

我发现这里2个相关的职位,但我无法从他们那里得到很多信息。

Answer 1:

我认为,解决方案可能是(或/和):

  • 控制反转的AffineTransform操作的顺序,把旋转后翻译
  • 使用-x和-y为您的翻译值


文章来源: custom shape rotation issue