JavaFX 3D Tutorial #5 – More fine mouse control – Zoom in and Zoom out

In this chapter, we will implement the mouse zoom control. Currently we have complete rotation freedom on the mouse. With Zoom in/out effect, we can control the Z-axis.

This zoom control will be based on mouse scrolling. JavaFX provides option to listen for mouse scroll event. We will make use of that.

//Attach a scroll listener
stage.addEventHandler(ScrollEvent.SCROLL, event -> {
        //Get how much scroll was done in Y axis.
	double delta = event.getDeltaY();
        //Add it to the Z-axis location.
	group.translateZProperty().set(group.getTranslateZ() + delta);
});

ScrollEvent.SCROLL allows to track the scrolling. JavaFX provides support for tracking horizontal scrolling (which can be obtained using event.getDeltaX()) as well as vertical scrolling (which can be obtained using event.getDeltaY()). Currently we are interested in vertical scrolling only.

When we scroll upward, the value will be positive. Then the Z axis value will increase and object will go away. When the scroll is downward, Z value will be decreased and the object will come closer.

Visit JavaFX 3D Course Index Page

Muhammed Afsal Villan
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

24 COMMENTS