自定义形状的旋转问题 [英] custom shape rotation issue

查看:172
本文介绍了自定义形状的旋转问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to rotate a custom shape around its center, but can not get the result as expected.

我想是

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

*shape should be rotated around its center without moving itself.*

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

what my solution is currently doing is rotating a whole shape around its center , by every rotation its changing its position.

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

I have multiple shapes so i have created a class to encapsulate a shape with its transform in following class

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

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

I am calculating angle of rotation using following method

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;
    }

在画布上的鼠标监听鼠标preSS事件

in mouse press event of the canvas mouse listener

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

我打电话所选形状的onMouse preSS法

i am just calling selected shape's onMousePress method

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

and in my mouse drag method of the canvas mouse listener , i am just calling the selected shape's onMouseDrag method which updates the rotation angle as you can see from the very first class

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

,你可以看到各个形状的绘制方法,绘制按照目前的改造,我是来自调用的paintComponent像

and you can see the draw method of the individual shape , to draw the shape according to current transform , i am calling from paintComponent like

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

        while (shoeIter.hasNext()) {

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

        }

在这里shoeShapeMap包含了所有的自定义形状的当前画布上。

where shoeShapeMap contains all of the custom shapes currently on the canvas.

是我在做什么,在计算角度或确定锚点的错误?我目前的解决方案旋转通过检查所有条件[90度等],你可以在上面提到的方法请参阅形状360度。

is i am doing mistake in calculating angle or determining anchor point ? my current solution rotates shape 360 degree by checking all the conditions[90 degree etc.] as you can see in the above mentioned method.

我想要的形状应该围绕其中心旋转,而不调整其大小,位置?
在字也很难解释,所以请给我建议任何更好的方法来这里展示一下我想达到什么目的?

i want the shape should be rotated around its center without resizing its positions ? in the word it is difficult to explain , so please suggest me any better way to show here what i want to accomplish ?

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

i think i have mentioned all the things related to this issue. if you have any doubts please feel free to ask me.

我发现这里2相关的帖子,但我无法从他们身上找到多少信息。

i found 2 related posts here but i could not find much information from them.

推荐答案

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

I think that the solution may be to (either/and):


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

这篇关于自定义形状的旋转问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆