import java.nio.IntBuffer; import javafx.animation.AnimationTimer; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.image.PixelWriter; import javafx.scene.image.WritablePixelFormat; import javafx.scene.paint.Color; import javafx.scene.paint.CycleMethod; import javafx.scene.paint.LinearGradient; import javafx.scene.paint.Stop; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.stage.StageStyle; import javafx.util.Duration; public class LostBackBuffer extends Application { static final int W = 400; static final int H = 400; @Override public void start(Stage stage) { Group root = new Group(); Rectangle bg = new Rectangle(W, H, new LinearGradient(0, 0, W, H, false, CycleMethod.NO_CYCLE, new Stop(0, Color.BLUE), new Stop(1, Color.GREEN))); root.getChildren().add(bg); Rectangle r = new Rectangle(50, 50, Color.RED); r.setX(100); r.setY(100); Timeline t = new Timeline(new KeyFrame(Duration.seconds(1), new KeyValue(r.xProperty(), 300))); t.setAutoReverse(true); t.setCycleCount(Timeline.INDEFINITE); t.play(); root.getChildren().add(r); stage.setScene(new Scene(root, W, H)); stage.setX(0); stage.show(); final Canvas cv = new Canvas(2000, 2000); cv.setTranslateX(0); cv.setTranslateY(200); Stage stage2 = new Stage(StageStyle.DECORATED); stage2.setScene(new Scene(new Group(cv), W, H)); stage2.setX(W+100); Platform.runLater(() -> stage2.show()); final int buf[] = new int[2000 * 2000+100]; for (int i = 0; i < buf.length; i++) { buf[i] = 0xff000000 | (int) (Math.random() * 0xffffff); } new AnimationTimer() { int off; @Override public void handle(long now) { WritablePixelFormat wpf = WritablePixelFormat.getIntArgbPreInstance(); PixelWriter pw = cv.getGraphicsContext2D().getPixelWriter(); pw.setPixels(0, 0, 2000, 2000, wpf, buf, off, 2000); off++; if (off >= 100) off = 0; } }.start(); } }