I tried to programm a windmill with rotating rotor/head. The stem itself is fixed and drawn as a background onto a canvas (javafx). The rotor/head itself is another image. I thought I could rotate the image itself and draw it onto the graphicscontext. (Didn't work) Then I tried various things:
a. Drawing the image onto another canvas, rotate the canvas, make a snapshot of that canvas. (Didn't work)
b. Make an imageview, rotate the imageview, snapshot it and draw it (Didn't work) and
c. I tried to make a RotateTransition (Didn't work). Now I am back to b: an ImageView of the image which I rotate.
b sort of works, sort of! It somehow "bounces" and I don`t know why because the doc says that ImageView.setRotate(..) rotates the image around the center. The image itself has same height and length therefore it should bounce as if it were a rectangle.. I just want this bounce to stop.. see here (SO didnt allow me to post a gif)
All of these failed tries came from readings of this forum.
Source Code here or as text:
package sample;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private Pane root;
private Canvas canvas;
private GraphicsContext gc;
SnapshotParameters params = new SnapshotParameters();
private final Image stem = new Image(getClass().getResource("windrad.png").toExternalForm());
private final ImageView wheel = new ImageView(new Image(getClass().getResource("rad.png").toExternalForm()));
private final int height = 720;
private final int width = 720;
private final double distanceWidth = 400.0 / 720.0;
private final double distanceHeight = 240.0 / 720.0;
private int degrees = 0;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
params.setFill(Color.TRANSPARENT);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("sample.fxml"));
try {
root = fxmlLoader.load();
canvas = (Canvas) fxmlLoader.getNamespace().get("canvas");
canvas.setHeight(height);
canvas.setWidth(width);
primaryStage.setHeight(height);
primaryStage.setWidth(width);
gc = canvas.getGraphicsContext2D();
} catch (IOException e) {
e.printStackTrace();
}
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
animation().start();
}
private AnimationTimer animation() {
return new AnimationTimer() {
@Override
public void handle(long now) {
gc.clearRect(0,0, width, height);
gc.drawImage(stem, 0, 0);
degrees = degrees >= 360 ? 0 : ++degrees;
wheel.setRotate(degrees);
gc.drawImage(wheel.snapshot(params, null), width * distanceWidth - (int) wheel.getImage().getWidth() / 2, height * distanceHeight - (int) wheel.getImage().getHeight() / 2);
}
};
}
}
Ok, so I got it working! @James_D helped me a lot. Instead of drawing it onto a canvas I placed all objects in a scenegraph and moved it there. Since my rotor/head is a imageview I was able to use imageview.setRotate(). My issue (for which I posted this thread) originated by the use of the canvas. I still don`t know how the bouncing bug occured but my project aim is achieved. The new source code is in the answers.