/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ package test.scenegraph.app; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.beans.value.ObservableDoubleValue; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Orientation; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.Slider; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.FlowPane; import javafx.scene.paint.ImagePattern; import javafx.scene.shape.Circle; import javafx.scene.shape.Ellipse; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Shape; import javafx.scene.text.Text; import test.javaclient.shared.InteroperabilityApp; import test.javaclient.shared.Utils; /** * * @author Aleksandr Sakharuk */ public class ImagePatternTilingApp extends InteroperabilityApp { /** * @param args the command line arguments */ public static void main(String[] args) { Utils.launch(ImagePatternTilingApp.class, args); } @Override protected Scene getScene() { shapesCombo.setId(SHAPES_COMBO_ID); image = new Image("file:resources/test/scenegraph/resources/square.png"); slider.setId(SLIDER_ID); slider.setMin(0); slider.setMax(3); slider.setValue(0.5); System.out.println("Image status: " + image.getProgress()); final FlowPane root = new FlowPane(Orientation.VERTICAL); shapesCombo.setOnAction(new EventHandler() { public void handle(ActionEvent t) { int idx = root.getChildren().indexOf(shape); root.getChildren().remove(idx); shape = shapesCombo.getValue().getShape(); shape.setId(SHAPE_ID); double q = slider.getValue(); shape.setFill(new ImagePattern(image, 0, 0, q, q, true)); root.getChildren().add(idx, shape); } }); slider.valueProperty().addListener(new InvalidationListener() { @Override public void invalidated(Observable ov) { double q = ((ObservableDoubleValue) ov).get(); shape.setFill(new ImagePattern(image, 0, 0, q, q, true)); } }); root.setVgap(10); root.setPadding(new Insets(10)); shape = Shapes.CIRCLE.getShape(); root.getChildren().addAll(shapesCombo, shape, slider); shapesCombo.setValue(Shapes.CIRCLE); return new Scene(root, 200, 200); } private ComboBox shapesCombo = new ComboBox(FXCollections.observableArrayList(Shapes.values())); private Shape shape; private Image image; private Slider slider = new Slider(); public static final String SHAPES_COMBO_ID = "shapes_combo"; public static final String SHAPE_ID = "shape"; public static final String SLIDER_ID = "slider"; public interface ShapeFactory { Shape createShape(); } public enum Shapes { CIRCLE(new ShapeFactory() { public Shape createShape() { return new Circle(50); } }), ELLIPSE(new ShapeFactory() { public Shape createShape() { return new Ellipse(50, 35); } }), POLYGON(new ShapeFactory() { public Shape createShape() { return new Polygon(50, 20, 30, 50, 70, 80, 110, 50, 90, 20); // TODO: Five angle polygon } }), RECTANGLE(new ShapeFactory() { public Shape createShape() { return new Rectangle(100, 100); } }), TEXT(new ShapeFactory() { public Shape createShape() { Text text = new Text("Text"); text.setStyle("-fx-font-size: 60; -fx-font-family: sans-serif; -fx-font-weight: bold;"); return text; } }) ; private Shapes(ShapeFactory factory) { this.factory = factory; } public Shape getShape() { return factory.createShape(); } private ShapeFactory factory; } }