1

Okay. So I'm trying to rotate the cursor image or the cursor itself depending on it's position. I have tried using

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

So I was thinking that this should rotate the image, but g2d.rotate() doesen't seem to have any effect on cursor? I'm not 100% sure if it has affect on the image itself. Atleast the cursor image is what I want it to be though.

EDIT: Here's an example video :) (In my case, I just want to rotate it around a certain point which stays on the same spot all the time). https://www.youtube.com/watch?v=TQ71QXa-B-s

user3738243
  • 159
  • 2
  • 13

2 Answers2

0

It seems a bit confusing to me, what are you trying to rotate?

Lets imagine you have a BufferedImage object, you may get a Graphics2D object from it, and by operationg over it, you may get what you want.

java.awt.image.BufferedImage buffImage = null;

try {
    java.io.InputStream imageStream = 
            MyClass.class.getResourceAsStream( "pictures/skills/skill" +InfoReader.SkillData("CastImage") +".png" );
    //MyClass is anyclass that you use as relative path...
    //use ClassLoader.getSystemClassLoader().getResourceAsStream( ... ) 
    //for a absolute path
    buffImage = javax.imageio.ImageIO.read( imageStream );
}
catch ( java.io.IOException | IllegalArgumentException ex ) {
    //It may throw IllegalArgumentException if imageStream is null.
}

Graphics2D g2d = buffImage.createGraphics();
try {
    AffineTransform old = g2d.getTransform();
    g2d.rotate(Math.toRadians(degrees));
    g2d.setTransform(old);
}finally {
    g2d.dispose();
}

Toolkit toolkit = Toolkit.getDefaultToolkit();   //Get the default toolkit  
Cursor cursor = toolkit.createCustomCursor(image, new Point(0, 0), "Cursor");
setCursor(cursor);

Now, if you intend to rotate it while you use it, I'm not sure how, but I hope I have helped a bit.

EDIT: Try to see this link, it might help you: https://www.google.com/#q=java+rotate+Cursor

EDIT 2: I see now what you want exaclty, I don't know how to help you, but try to see the link I gave you (yes it is from google). Even if you don't find much, it might help you in your quest.

saclyr
  • 161
  • 5
0

While searching and asking around for the solution of a similar problem I've found your question and also the answer to it. This is the code I use in my program and it works. Note that this method was designed to be called only once and calling it constantly might require optimization. Also I've learned AffineTransform today and might have made some mistakes(even though code works).

Basically I rotate an image, create a new image from it and set the new image as the cursor. My "cursor.png" is in the data folder.

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

}

You can use window.getMousePosition().x; and window.getMousePosition().y; for getting mouse position if you are using a mouseListener.

You need to call rotateCursor() method with the correct double. How to calculate the correct double is something I can't help you with.

I hope it helps.

I've learned these from these sources:

storing transformed BufferedImage in Java

http://www.codebeach.com/2008/02/using-custom-cursors-in-java.html

Rotating BufferedImage instances

http://stumpygames.wordpress.com/2012/07/22/particles-tutorial-foundation/ (this tutorial also has a mouse listener)

Community
  • 1
  • 1
WVrock
  • 1,725
  • 3
  • 22
  • 30