同様の問題の解決策を検索して尋ねているときに、あなたの質問とそれに対する答えを見つけました。これは私のプログラムで使用するコードであり、動作します。このメソッドは一度だけ呼び出されるように設計されており、常に呼び出すには最適化が必要になる場合があることに注意してください。また、私は今日 AffineTransform を学びましたが、いくつかの間違いを犯した可能性があります (コードは機能しますが)。
基本的に、画像を回転させ、そこから新しい画像を作成し、新しい画像をカーソルとして設定します。私の「cursor.png」はデータフォルダーにあります。
private void rotateCursor(double rotation) {
// Get the default toolkit
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Load an image for the cursor
BufferedImage image = null;
try {
image = ImageIO.read(this.getClass().getResource("/data/cursor.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
AffineTransform at = new AffineTransform();
// affineTransform applies the added transformations in the reverse order
// 3. translate it back to the center of the picture
at.translate(image.getWidth() / 2, image.getWidth() / 2);
at.rotate(rotation);//2- adds rotation to at (they are not degrees)
//1- translate the object so that you rotate it around the center
at.translate(-image.getWidth() / 2, -image.getHeight() / 2);
BufferedImage rotated = null; // creates new image that will be the transformed image
// makes this: at + image= rotated
AffineTransformOp affineTransformOp = new AffineTransformOp(at,
AffineTransformOp.TYPE_BILINEAR);
image2 = affineTransformOp.filter(image, rotated);
// Create the hotSpot for the cursor
Point hotSpot = new Point(10, 0); // click position of the cursor(ex: + shape's is middle)
Cursor cursor = toolkit.createCustomCursor(rotated, hotSpot, "cursor");
// Use the custom cursor
this.setCursor(cursor);
}
mouseListener を使用している場合は、マウスの位置を取得するためにwindow.getMousePosition().x;
とを使用できます。window.getMousePosition().y;
rotateCursor()
正しい double でメソッドを呼び出す必要があります。正しい double を計算する方法は、私がお手伝いできないことです。
お役に立てば幸いです。
私はこれらの情報源からこれらを学びました:
変換された BufferedImage を Java に保存する
http://www.codebeach.com/2008/02/using-custom-cursors-in-java.html
BufferedImage インスタンスの回転
http://stumpygames.wordpress.com/2012/07/22/particles-tutorial-foundation/ (このチュートリアルにはマウス リスナーもあります)