package custompatterns; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.beans.property.SimpleDoubleProperty; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.PixelWriter; import javafx.scene.image.WritableImage; import javafx.scene.paint.Color; import javafx.scene.paint.ImagePattern; import javafx.scene.paint.Paint; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; public class CustomPatterns extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Group root = new Group(); // Size for a metal texture pattern final WritableImage bgimg = new WritableImage(6, 6); Paint imgpat = new ImagePattern(bgimg, 0, 0, 30, 30, false); final Rectangle r = new Rectangle(0, 0, 640, 480); r.setFill(imgpat); root.getChildren().add(r); ImageView view = new ImageView(bgimg); view.setFitWidth(150); view.setFitHeight(150); root.getChildren().add(view); final SimpleDoubleProperty hue = new SimpleDoubleProperty(0.0); hue.addListener(new InvalidationListener() { @Override public void invalidated(Observable o) { makeMetalPattern(bgimg, hue.get()); // r.setFill(new ImagePattern(bgimg, 0, 0, 30, 30, false)); } }); makeMetalPattern(bgimg, hue.get()); final Scene scene = new Scene(root, 640, 480); stage.setScene(scene); stage.setTitle("Animated Metal"); stage.show(); KeyValue kv = new KeyValue(hue, 360); KeyFrame kf = new KeyFrame(Duration.seconds(6), kv); Timeline tl = new Timeline(kf); tl.setCycleCount(Timeline.INDEFINITE); tl.play(); } void makeMetalPattern(WritableImage img, double hue) { Color c1 = Color.hsb(hue, 0.5, 0.3); Color c2 = Color.hsb(hue, 0.3, 0.1); PixelWriter pw = img.getPixelWriter(); for (int y = 0; y < 6; y++) { for (int x = 0; x < 6; x++) { pw.setColor(x, y, c2); } } pw.setColor(0, 0, c1); pw.setColor(1, 1, c1); pw.setColor(2, 2, c1); pw.setColor(3, 5, c1); pw.setColor(4, 4, c1); pw.setColor(5, 3, c1); } }