Skip to content

Instantly share code, notes, and snippets.

@miho
Last active January 23, 2016 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miho/ab1fff7f483c7f7409b4 to your computer and use it in GitHub Desktop.
Save miho/ab1fff7f483c7f7409b4 to your computer and use it in GitHub Desktop.
This JavaFX applications demonstrates strage rendering artifacts when enabling node caching.
//package cachebug01;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.CacheHint;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* This JavaFX applications demonstrates strange rendering artifacts.
*
* Enabling a regions {@code cache} property and resizing the region causes
* serious rendering bugs. Te reproduce the behavior just press the toggle
* button (e.g. once a second).
*
* @author Michael Hoffer <info@michaelhoffer.de>
*/
public class CacheBug01 extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// we print the java version to be sure that we use the right jdk
System.out.println(
"java-version: "
+ System.getProperty("java.version")
);
// create and initialize region
// this region isn't updated and causes rendering artifacts (see below)
Region r = new Region();
r.setCacheHint(CacheHint.SCALE);
r.setPrefWidth(30);
r.setPrefHeight(30);
r.setStyle(""
+ "-fx-border-color: red;"
+ "-fx-background-color: rgba(0,0,0,0.4);"
+ "-fx-border-color: red;");
// resize animation (prefWidth/prefHeight properties)
Timeline l = new Timeline(
new KeyFrame(Duration.ZERO,
new KeyValue(r.prefWidthProperty(), 30),
new KeyValue(r.prefHeightProperty(), 30)
),
new KeyFrame(Duration.seconds(2),
new KeyValue(r.prefWidthProperty(), 400),
new KeyValue(r.prefHeightProperty(), 400)
)
);
l.setAutoReverse(true);
l.setCycleCount(Timeline.INDEFINITE);
l.play();
// region container
Pane pane = new Pane(r);
pane.setPrefSize(400, 400);
// cache toggle btn
String cacheBtnString = "Toggle Cache: r.isCache() -> ";
ToggleButton cacheBtn = new ToggleButton(cacheBtnString + r.isCache());
cacheBtn.setPrefWidth(300);
r.cacheProperty().bind(cacheBtn.selectedProperty());
r.cacheProperty().addListener((ov) -> {
cacheBtn.setText(cacheBtnString + r.isCache());
});
// root box
VBox root = new VBox(cacheBtn, pane);
root.setAlignment(Pos.CENTER);
// usual scene/stage setup
Scene scene = new Scene(root, 400, 450);
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.setTitle("Cache Bug 01");
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment