Skip to content

Commit

Permalink
add in rotate image method
Browse files Browse the repository at this point in the history
  • Loading branch information
chengenzhao committed Oct 7, 2017
1 parent cd913e9 commit 1d96cda
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main/java/com/whitewoodcity/core/node/canvas/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.beans.property.DoubleProperty;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.transform.Rotate;

public class Canvas implements Node {
private javafx.scene.canvas.Canvas body;
Expand Down Expand Up @@ -175,4 +176,36 @@ public void square(double x, double y, double side){
public void square(double x, double y, double side, boolean filled){
rect(x,y,side,side, filled);
}

/**
* Draws an image on a graphics context.
*
* The image is drawn at (tlpx, tlpy) rotated by angle pivoted around the point:
* (tlpx + image.getWidth() / 2, tlpy + image.getHeight() / 2)
*
* @param gc the graphics context the image is to be drawn on.
* @param angle the angle of rotation.
* @param tlx the top left x co-ordinate where the image will be plotted (in canvas co-ordinates).
* @param tly the top left y co-ordinate where the image will be plotted (in canvas co-ordinates).
* @param px the x pivot co-ordinate for the rotation (in canvas co-ordinates).
* @param py the y pivot co-ordinate for the rotation (in canvas co-ordinates).
*/
Rotate rotate = new Rotate();

public void rotateImage(Image image, double tlx, double tly, double width, double height, double angle, double px, double py) {
javafx.scene.canvas.GraphicsContext gc = body.getGraphicsContext2D();
gc.save(); // saves the current state on stack, including the current transform
rotate.setAngle(angle);
rotate.setPivotX(px);
rotate.setPivotY(py);
gc.setTransform(rotate.getMxx(), rotate.getMyx(), rotate.getMxy(), rotate.getMyy(), rotate.getTx(), rotate.getTy());
gc.drawImage(image, tlx, tly, width, height);
gc.restore(); // back to original state (before rotation)
}
public void rotate_image(Image image, double tlx, double tly, double width, double height, double angle, double px, double py) {
rotateImage(image,tlx,tly,width,height,angle,px,py);
}
public void rotateimage(Image image, double tlx, double tly, double width, double height, double angle, double px, double py) {
rotateImage(image,tlx,tly,width,height,angle,px,py);
}
}

0 comments on commit 1d96cda

Please # to comment.