【问题标题】:Java: rotating image so that it points at the mouse cursorJava:旋转图像使其指向鼠标光标
【发布时间】:2013-04-14 13:41:40
【问题描述】:

我希望玩家图像指向鼠标光标。我使用这段代码来获取鼠标光标的位置:

private int cursorX = MouseInfo.getPointerInfo().getLocation().x;
private int cursorY = MouseInfo.getPointerInfo().getLocation().y;

注意:默认播放器图像指向上方

【问题讨论】:

  • 旋转图像意味着将其从其轴上移动而不改变其位置,这不是您想要做的,您想将图像移动到与光标相同的位置吗?
  • 不,我希望球员图像面对鼠标光标

标签: java image swing rotation awt


【解决方案1】:

虽然这是两年前问的......

如果您需要鼠标在窗口中不断更新鼠标位置,请参阅mouseMotionListener。用于获取鼠标位置的电流是相对于整个屏幕的。请记住这一点。

要不然,这是我用的一种方法,

public double angleInRelation(int x1, int y1, int x2, int y2) {
    // Point 1 in relation to point 2
    Point point1 = new Point(x1, y1);
    Point point2 = new Point(x2, y2);
    int xdiff = Math.abs(point2.x - point1.x);
    int ydiff = Math.abs(point2.y - point1.y);
    double deg = 361;
    if ( (point2.x > point1.x) && (point2.y < point1.y) ) {
        // Quadrant 1
        deg = -Math.toDegrees(Math.atan(Math.toRadians(ydiff) / Math.toRadians(xdiff)));

    } else if ( (point2.x > point1.x) && (point2.y > point1.y) ) {
        // Quadrant 2
        deg = Math.toDegrees(Math.atan(Math.toRadians(ydiff) / Math.toRadians(xdiff)));

    } else if ( (point2.x < point1.x) && (point2.y > point1.y) ) {
        // Quadrant 3
        deg = 90 + Math.toDegrees(Math.atan(Math.toRadians(xdiff) / Math.toRadians(ydiff)));

    } else if ( (point2.x < point1.x) && (point2.y < point1.y) ) {
        // Quadrant 4
        deg = 180 + Math.toDegrees(Math.atan(Math.toRadians(ydiff) / Math.toRadians(xdiff)));

    } else if ((point2.x == point1.x) && (point2.y < point1.y)){
        deg = -90;
    } else if ((point2.x == point1.x) && (point2.y > point1.y)) {
        deg = 90;
    } else if ((point2.y == point1.y) && (point2.x > point1.x)) {
        deg = 0;
    } else if ((point2.y == point2.y) && (point2.x < point1.x)) {
        deg = 180;
    }
    if (deg == 361) {
        deg = 0;
    }
    return deg;
}

换句话说,你得到每个 θs 的角度,如下图所示,并检查 x 或 y 是否为 0,并为此做一个特殊情况。

原点是图片的中间,每个点(用手绘十字标记)就是鼠标所在的位置。

【讨论】:

    【解决方案2】:

    要找到从坐标 (0,0) 到另一个坐标 (x,y) 的角度,我们可以使用三角函数 tan^-1(y/x)。

    Java 的Math 类指定了一个静态方法atan2,它充当tan^-1 函数(也称为“反正切”,因此称为“atan”)并以弧度返回角度. (有一个方法 atan 接受一个参数。请参阅链接的 Javadoc。)

    为了找到从“玩家”坐标到鼠标光标坐标的角度(我假设您提到的这个“玩家”有 x 和 y 坐标),我们需要做这样的事情:

    double theta = Math.atan2(cursorY - player.getY(), cursorX - player.getX());
    

    还需要注意的是,零弧度的角度表示鼠标直接位于玩家的右侧。您提到“默认播放器图像”指向上方;如果您的意思是在旋转之前,您的图像面向玩家朝上,那么对于几何和atan2 的 Java 实现来说,让您的玩家“默认”朝右会更传统。

    【讨论】:

    • 我应该创建一个扩展 Math.atan2 的类并使用它还是你的意思是写theta = Math.atan2?我将如何在我的g2D.drawImage(image,x,y,this); 中实现它,我应该以其他方式实现它吗?
    • 我的错误。是的,您可以通过写Math.atan2(...) 找到角度。它是一种静态方法。我编辑了我的答案以反映这一点。您不会(也不能)编写扩展 Math.atan2 的类。对于 Graphics2D,您可以调用方法g2D.rotate(theta)然后绘制您的图像。
    • atan2() 返回弧度,而不是度数。
    【解决方案3】:

    您必须使用trigonometry 来计算旋转角度。为此,您首先需要获取图像和光标的位置。我无法告诉您如何获得图像的位置,因为这可能会有所不同。对于这个例子(改编自here),我假设imageXimageY 是您图像的xy 位置:

    float xDistance = cursorX - imageX;
    float yDistance = cursorY - imageY;
    double rotationAngle = Math.toDegrees(Math.atan2(yDistance, xDistance));
    

    【讨论】:

    • 我将如何在 g2D.drawImage(image,x,y,this) 中实现它,以便它绘制旋转图像而不是默认图像,或者我会以其他方式实现它吗?
    • 使用.rotate(rotationAngle);
    • 我使用的图像类没有.rotate
    • ImageIcon ii = new ImageIcon("Player.png"); image = ii.getImage();
    • 我不太确定我做对了,因为当我用这样的paint方法写它double theta = Math.toDegrees(Math.atan2(cursorY - player.getY(), cursorX - player.getX())); if(player.isAlive()){ g2D.rotate(theta); g2D.drawImage(player.getImage(),player.getX(),player.getY(),this); }时敌人和玩家都面对45度并且他们移动在圆形中非常快,当我按下 w 或 d 时它们都会向上移动,如果我按下 s 或 a 则会向下移动
    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多