Rashin
Rashin

Reputation: 806

How to firing Zoomevent in Javafx 8

I'm working on a very simple program. I have an ImageView and I want to firing to this ImageView a ZoomEvent that I created. In Javafx 8 there 2 constructor that I can use to create ZoomEvent. But the listeners doesn't catch the event.

    StackPane root = new StackPane();
    final ImageView img = new ImageView(url);
    img.setOnZoom(new EventHandler<ZoomEvent>() {

        @Override
        public void handle(ZoomEvent t) {
            System.out.println("Zoom!");
        }
    });

    img.setOnZoomStarted(new EventHandler<ZoomEvent>() {

        @Override
        public void handle(ZoomEvent t) {
            System.out.println("Zoom Started!");
        }
    });

    img.setOnZoomFinished(new EventHandler<ZoomEvent>() {

        @Override
        public void handle(ZoomEvent t) {
            System.out.println("Zoom finished!");
        }
    });
    ZoomEvent evt = new ZoomEvent(ZoomEvent.ANY, 200,200,200,200,false,false,false,false,false, false, 5.0,5.0, null);

    root.getChildren().add(img);
    Scene scene = new Scene(root, 600, 600);
    primaryStage.setTitle("Zoom Sample");
    primaryStage.setScene(scene);
    primaryStage.show();

This is my code so far and I can't figure out what is wrong. There isn't any output.

Upvotes: 3

Views: 1760

Answers (1)

jewelsea
jewelsea

Reputation: 159436

  1. You never fired the event with ZoomEvent.fireEvent(target, event) or stage.fireEvent(event).
  2. You created the wrong type of event (create a ZoomEvent.ZOOM, not a ZoomEvent.ANY).
  3. You didn't set the event screen co-ordinates correctly.

Working sample:

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.input.ZoomEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Zoom extends Application {

    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage stage) {
        final ImageView img = new ImageView(
                "http://icons.iconarchive.com/icons/kirei-kaze/kikis-delivery-service/256/jiji-icon.png"   // image license: License: Free for non-commercial use. The products or characters depicted in these icons are © by Studio Ghibli.  Linkback: http://www.iconarchive.com/artist/kirei-kaze.html
        );
        img.setOnZoom(e -> System.out.println("Zoom!"));

        StackPane layout = new StackPane(img);
        stage.setScene(new Scene(layout, 600, 600));
        stage.show();

        Point2D screenCoords = layout.localToScreen(300, 300);
        ZoomEvent evt = new ZoomEvent(
                ZoomEvent.ZOOM,
                300, 300,
                screenCoords.getX(), screenCoords.getY(),
                false,false,false,false,false, false, 5.0,5.0, null
        );
        ZoomEvent.fireEvent(img, evt);
    }
}

Upvotes: 2

Related Questions