1

わかった。そのため、位置に応じてカーソル画像またはカーソル自体を回転させようとしています。使ってみました

    Graphics2D g2d = (Graphics2D)g;
    AffineTransform old = g2d.getTransform();
    g2d.rotate(Math.toRadians(degrees));
    Toolkit toolkit = Toolkit.getDefaultToolkit();   //Get the default toolkit  
    Image image = toolkit.getImage("pictures/skills/skill" +InfoReader.SkillData("CastImage") +".png");   //Load an image for the cursor  
    Cursor cursor = toolkit.createCustomCursor(image, new Point(0, 0), "Cursor");
    setCursor(cursor);
    g2d.setTransform(old);

だから私はこれが画像を回転させるべきだと思っていましたが、 g2d.rotate() はカーソルに影響を与えないようですか? 画像自体に影響があるかどうかは 100% わかりません。少なくともカーソル画像は私が望んでいるものです。

編集: これはビデオの例です:) (私の場合、常に同じ場所にとどまる特定のポイントを中心に回転させたいだけです)。https://www.youtube.com/watch?v=TQ71QXa-Bs

4

2 に答える 2

0

同様の問題の解決策を検索して尋ねているときに、あなたの質問とそれに対する答えを見つけました。これは私のプログラムで使用するコードであり、動作します。このメソッドは一度だけ呼び出されるように設計されており、常に呼び出すには最適化が必要になる場合があることに注意してください。また、私は今日 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/ (このチュートリアルにはマウス リスナーもあります)

于 2014-10-21T15:39:12.037 に答える