How to draw image rotated on JavaFX Canvas? -


i want draw image on canvas rotated. drawimage(image, 0, 0) can draw image how can rotate image example 45 degrees , draw , draw other image -50 degrees rotation after previous image on same canvas?

graphiccontext2d not work me because rotate canvas content.

so how can that?

here sample, following similar principles katona's answer, difference rotates images arbitrary pivot points applying custom transform.

rotatedimages

import javafx.application.application; import javafx.geometry.insets; import javafx.scene.scene; import javafx.scene.canvas.*; import javafx.scene.image.image; import javafx.scene.layout.stackpane; import javafx.scene.paint.color; import javafx.scene.transform.rotate; import javafx.stage.stage;  /** rotates images round pivot points , places them in canvas */ public class rotatedimageincanvas extends application {     /**      * sets transform graphicscontext rotate around pivot point.      *      * @param gc graphics context transform applied to.      * @param angle angle of rotation.      * @param px x pivot co-ordinate rotation (in canvas co-ordinates).      * @param py y pivot co-ordinate rotation (in canvas co-ordinates).      */     private void rotate(graphicscontext gc, double angle, double px, double py) {         rotate r = new rotate(angle, px, py);         gc.settransform(r.getmxx(), r.getmyx(), r.getmxy(), r.getmyy(), r.gettx(), r.getty());     }      /**      * draws image on graphics context.      *      * image drawn @ (tlpx, tlpy) rotated angle pivoted around point:      *   (tlpx + image.getwidth() / 2, tlpy + image.getheight() / 2)      *      * @param gc graphics context image drawn on.      * @param angle angle of rotation.      * @param tlpx top left x co-ordinate image plotted (in canvas co-ordinates).      * @param tlpy top left y co-ordinate image plotted (in canvas co-ordinates).      */     private void drawrotatedimage(graphicscontext gc, image image, double angle, double tlpx, double tlpy) {         gc.save(); // saves current state on stack, including current transform         rotate(gc, angle, tlpx + image.getwidth() / 2, tlpy + image.getheight() / 2);         gc.drawimage(image, tlpx, tlpy);         gc.restore(); // original state (before rotation)     }      @override public void start(stage stage) {         image image = new image(             "http://worldpress.org/images/maps/world_600w.jpg", 350, 0, true, true         );          // creates canvas on rotated images rendered.         canvas canvas = new canvas(600, 400);         graphicscontext gc = canvas.getgraphicscontext2d();          drawrotatedimage(gc, image,  40,   0,   0);         drawrotatedimage(gc, image, -50, 400, 200);          // supplies tiled background image on canvas drawn.         stackpane stack = new stackpane();         stack.setmaxsize(canvas.getwidth(), canvas.getheight());         stack.setstyle("-fx-background-image: url('http://1.bp.blogspot.com/_wv5jmd1oisg/tdytyxuxr4i/aaaaaaaavso/a0zt8nwpv8u/s400/louis-vuitton-nice-beautiful.jpg');");         stack.getchildren().add(                 canvas         );          // places resizable padded frame around canvas.         stackpane frame = new stackpane();         frame.setpadding(new insets(20));         frame.getchildren().add(stack);          stage.setscene(new scene(frame, color.burlywood));         stage.show();     }      public static void main(string[] args) { launch(rotatedimageincanvas.class); } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -