How to draw image rotated on JavaFX Canvas?

后端 未结 4 906
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 20:21

I want to draw image on canvas rotated. with drawImage(image, 0, 0) I can draw image but how can I rotate that image for example 45 degrees and draw it, then dr

4条回答
  •  一个人的身影
    2020-12-06 20:38

    Well I have never used JavaFX, but browsing it's API documentation, I came up with this solution (I have not actually tried it so it may be wrong):

    Canvas canvas = ...
    Image img = ...
    GraphicsContext gc = canvas.getGraphicsContext2D();
    
    gc.save(); // saves the current state on stack, including the current transform
    gc.rotate(45);
    gc.drawImage(img);
    gc.restore(); // back to original state (before rotation)
    
    gc.save();
    gc.rotate(-50);
    gc.drawImage(img);
    gc.restore();
    

    I don't know if it works here, but the idea (transformation stack) is borrowed from other drawing API (like OpenGL).

提交回复
热议问题