Java的:旋转图像,以便它指向的鼠标光标 [英] Java: rotating image so that it points at the mouse cursor

查看:132
本文介绍了Java的:旋转图像,以便它指向的鼠标光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望玩家形象走向鼠标指针指向。我用这个code,以获得鼠标光标的当前位置:

I want the player image to point towards the mouse cursor. I use this code to get the postion of the mouse cursor:

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

注意:的默认播放器影像朝上

Note: The default player image points upwards

推荐答案

要找到从角坐标为(0,0)到另一个坐标(X,Y),我们可以使用三角函数谭^ -1(Y / X)。

To find the angle from a coordinate (0,0) to another coordinate (x,y), we can use the trigonometric function tan^-1(y/x).

Java的 数学 类指定充当棕褐色^ -1功能(也被称为反正切的静态方法 ATAN2 ,因此,反正切),并在弧度的返回的角度。 (有一种方法反正切这需要一个参数。见链接的Javadoc。)

Java's Math class specifies a static method atan2 which acts as a tan^-1 function (also known as "arctangent", hence "atan") and returns the angle in radians. (There is a method atan which takes one argument. See the linked Javadoc.)

为了找到您的玩家的坐标,以鼠标光标的协调度的角度,(我假设这个玩家您提到有X和Y坐标),我们需要做这样的事情:

In order to find the angle in degrees from the coordinate of your "player" to the coordinate of the mouse cursor, (I'll assume this "player" you make mention of has x and y coordinates), we need to do something like this:

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

这也是值得注意的是零弧度的角度将表明鼠标直接将右键的玩家。你提到的默认播放器影像指出向上;如果你的意思是旋转前,图像朝上的球员,这将是更传统的以几何形状和Java实现 ATAN2 中有你的球员的脸权默认

It is also of note that an angle of zero radians would indicate that the mouse is directly to the right of the player. You mention that the "default player image" points upwards; if you mean that before rotation, your image faces upward for the player, it would be more conventional to geometry and the Java implementation of atan2 to have your player face right "by default".

这篇关于Java的:旋转图像,以便它指向的鼠标光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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