Java 2d 方向鼠标点旋转

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11911316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:53:15  来源:igfitidea点击:

Java 2d rotation in direction mouse point

javarotation2dimage-rotation

提问by 36redsoxfan

So far I have a java app where I draw a circle(player) and then draw a green rectangle on top(gun barrel). I have it so when the player moves, the barrel follows with it. I want it to find where the mouse is pointing and then rotate the barrel accordingly. For an example of what I mean look at this video I found http://www.youtube.com/watch?v=8W7WSkQq5SUSee how the player image reacts when he moves the mouse around?

到目前为止,我有一个 Java 应用程序,我在其中绘制了一个圆圈(玩家),然后在顶部(枪管)绘制了一个绿色矩形。我有它,所以当玩家移动时,枪管随之而来。我希望它找到鼠标指向的位置,然后相应地旋转桶。举个例子,看看这个视频,我发现http://www.youtube.com/watch?v=8W7WSkQq5SU看看当玩家移动鼠标时播放器图像如何反应?

Here's an image of what the game looks like so far:

这是到目前为止游戏的样子:

My Progress

我的进步

So how do I rotate it like this? Btw I don't like using affinetransform or Graphics2D rotation. I was hoping for a better way. Thanks

那么我如何像这样旋转它?顺便说一句,我不喜欢使用仿射变换或 Graphics2D 旋转。我希望有更好的方法。谢谢

回答by Zong

Using the Graphics2Drotation method is indeed the easiest way. Here's a simple implementation:

使用Graphics2D旋转方法确实是最简单的方法。这是一个简单的实现:

int centerX = width / 2;
int centerY = height / 2;
double angle = Math.atan2(centerY - mouseY, centerX - mouseX) - Math.PI / 2;

((Graphics2D)g).rotate(angle, centerX, centerY);

g.fillRect(...); // draw your rectangle

If you want to remove the rotation when you're done so you can continue drawing normally, use:

如果要在完成后删除旋转以便可以继续正常绘制,请使用:

Graphics2D g2d = (Graphics2D)g;
AffineTransform transform = g2d.getTransform();

g2d.rotate(angle, centerX, centerY);

g2d.fillRect(...); // draw your rectangle

g2d.setTransform(transform);

It's a good idea to just use Graphics2Danyway for anti-aliasing, etc.

Graphics2D无论如何,将其用于抗锯齿等是个好主意。

回答by MadProgrammer

Using AffineTransform, sorry, only way I know how :P

使用AffineTransform,抱歉,只有我知道如何使用 :P

public class RotatePane extends javax.swing.JPanel {

    private BufferedImage img;
    private Point mousePoint;

    /**
     * Creates new form RotatePane
     */
    public RotatePane() {

        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();

    }
}

Will produce this effect

会产生这种效果

Rotating

旋转

The red line (point out from the center) will want to follow the cursor.

红线(从中心点出来)将要跟随光标。