/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.LinkedList; import java.util.List; import javafx.animation.Animation; import javafx.animation.Interpolator; import javafx.animation.PathTransition; import javafx.animation.PathTransition.OrientationType; import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Point2D; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.layout.Pane; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.scene.transform.Scale; import javafx.scene.transform.ScaleBuilder; import javafx.stage.Stage; import javafx.util.Duration; /** * * @author Jose */ public class ScalingCausingInvisibleImages extends Application { ///////////////////////////////////////////////////////////////////////// //variables final int WIDTH = 300; final int HEIGHT = 300; final Duration PATH_DURATION = Duration.seconds(15); final double INITIAL_RATE = 1; // Pane root = new Pane(); Group animateGroup = new Group(); EventHandler keyEventHandler; int scaleFactor = 0; private Scene scene; private Stage stage; private double initialStageWidth = 0; private double initialStageHeight = 0; ////////////////////////////////////////////////////////////////////////// //public functions public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { stage = primaryStage; scene = new Scene(root, WIDTH, HEIGHT); stage.setTitle("Slow PathTransition Test"); stage.setScene(scene); stage.show(); // addUnit(); root.getChildren().add(animateGroup); // makeKeyEventHandler(); root.setOnKeyPressed(keyEventHandler); root.requestFocus(); } /////////////////////////////////////////////////////////////////////////// //private functions void addUnit() { Image img = getImage(); ImageView animatedIV = new ImageView(img); PathTransition pt = getPathTransition(); pt.setNode(animatedIV); animateGroup.getChildren().add(animatedIV); pt.playFromStart(); } void removeUnit() { if (animateGroup.getChildren().isEmpty() == false) { animateGroup.getChildren().remove(0); } } Image getImage() { return new Image("https://s3.amazonaws.com/turretmaster/images/soldier1.png"); } PathTransition getPathTransition() { int OFFSET = 50; // List points = new LinkedList<>(); points.add(new Point2D(OFFSET, OFFSET)); points.add(new Point2D(WIDTH - OFFSET, OFFSET)); points.add(new Point2D(WIDTH - OFFSET, HEIGHT - OFFSET)); points.add(new Point2D(OFFSET, HEIGHT - OFFSET)); points.add(new Point2D(OFFSET, OFFSET)); // PathTransition pathTransition = new PathTransition(); pathTransition.setDuration(PATH_DURATION); pathTransition.setPath(getPath(points)); pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT); pathTransition.setInterpolator(Interpolator.LINEAR); pathTransition.setCycleCount(Animation.INDEFINITE); pathTransition.setRate(INITIAL_RATE); return pathTransition; } Path getPath(List points) { System.out.println("in getPath"); Path path = new Path(); Point2D initialPoint = points.remove(0); path.getElements().add(new MoveTo(initialPoint.getX(), initialPoint.getY())); for (Point2D p : points) { System.out.println("...p.X = " + p.getX() + ", p.Y = " + p.getY()); path.getElements().add(new LineTo(p.getX(), p.getY())); } return path; } void makeKeyEventHandler() { keyEventHandler = new EventHandler< KeyEvent>() { @Override public void handle(KeyEvent event) { KeyCode kc = event.getCode(); if (KeyCode.DOWN.equals(kc)) { scaleDown(); } else if (KeyCode.UP.equals(kc)) { scaleUp(); } else if (KeyCode.RIGHT.equals(kc)) { addUnit(); } else if (KeyCode.LEFT.equals(kc)) { removeUnit(); } } }; } void scaleUp() { scaleFactor++; resize(); } void scaleDown() { if (scaleFactor > 0) { scaleFactor--; resize(); } } void resize() { if (initialStageWidth == 0 && initialStageHeight == 0) { initialStageWidth = stage.getWidth(); initialStageHeight = stage.getHeight(); } double scaleAmount = 1.0 + .1 * scaleFactor; Scale scale = ScaleBuilder.create().pivotX(0).pivotY(0).x(scaleAmount).y(scaleAmount).build(); root.getTransforms().setAll(scale); stage.setHeight(scaleAmount * initialStageHeight); stage.setWidth(scaleAmount * initialStageWidth); stage.show(); stage.setX(0); stage.setY(0); } }