为什么我的图像偏离中心?

时间:2012-09-26 21:00:33

标签: java image rotation mouse slick2d

我正在使用Java / Slick 2D来播放图形并使用鼠标旋转图像。但有些奇怪的事情发生了:图像不一定面向鼠标。它与法线成45度角,但距离越远,越远。请看下面的图像(白色圆圈是鼠标,文字是角度): Image at 80 degrees Image at 45 degrees

以下是我使用的轮换代码:

int mX = Mouse.getX();
        int mY = HEIGHT - Mouse.getY();
        int pX = sprite.x;
        int pY = sprite.y;
        int tempY, tempX;
        double mAng, pAng = sprite.angle;
        double angRotate=0;

        if(mX!=pX){
            mAng = Math.toDegrees(Math.atan2(mY - pY, mX - pX));
            if(mAng==0 && mX<=pX)
                mAng=180;
        }
        else{
            if(mY>pY)
                mAng=90;
            else
                mAng=270;
        }

        sprite.angle = mAng;
        sprite.image.setRotation((float) mAng);     

任何想法发生了什么?我假设它与图像坐标来自左上角的事实有关,但我不知道如何对抗它。仅供参考:屏幕640x460,图像128x128并以窗口为中心。

编辑:不幸的是,没有任何真正有用的东西。这是一张包含更多信息的图片:

Arrow at 35 degrees

EDIT2:找到了答案!不得不改变:int px / py = sprite.x / y to

        int pX = sprite.x+sprite.image.getWidth()/2;
    int pY = sprite.y+sprite.image.getHeight()/2;

2 个答案:

答案 0 :(得分:2)

看起来你从左边获取了鼠标的价值,并设置了与旋转的距离......这里有一些可能有用的东西:

http://www.instructables.com/id/Using-Java-to-Rotate-an-Object-to-Face-the-Mouse/?ALLSTEPS

答案 1 :(得分:0)

这是我写的类似问题的一些示例代码,可能有所帮助。

现在它不使用光滑,它使用SwingGraphics2D,但它可能会帮助您获得一些想法。

public class TestRotatePane extends JPanel {

    private BufferedImage img;
    private Point mousePoint;

    public TestRotatePane() {

        try {
            img = ImageIO.read(getClass().getResource("/MT02.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseMoved(MouseEvent e) {

                mousePoint = e.getPoint();

                repaint();

            }

        });

    }

    @Override
    public Dimension getPreferredSize() {

        return new Dimension(img.getWidth(), img.getHeight());

    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g.create();

        double rotation = 0f;

        int width = getWidth() - 1;
        int height = getHeight() - 1;

        if (mousePoint != null) {

            int x = width / 2;
            int y = height / 2;

            int deltaX = mousePoint.x - x;
            int deltaY = mousePoint.y - y;

            rotation = -Math.atan2(deltaX, deltaY);

            rotation = Math.toDegrees(rotation) + 180;

        }

        int x  = (width - img.getWidth()) / 2;
        int y  = (height - img.getHeight()) / 2;

        g2d.rotate(Math.toRadians(rotation), width / 2, height / 2);
        g2d.drawImage(img, x, y, this);

        x = width / 2;
        y = height / 2;
        g2d.setStroke(new BasicStroke(3));
        g2d.setColor(Color.RED);
        g2d.drawLine(x, y, x, y - height / 4);
        g2d.dispose();

    }

}

enter image description here

显然,您需要提供自己的图像;)