Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Zooming an ImageView in a ScrollPane

922927Mar 9 2012 — edited Mar 10 2012
Hi,
I wanted to show an image in a ScrollPane and allow zooming on it with the mouse weel. I am not completely happy with the result as it appears that zooming creates some unwanted vertical and horizontal steps in the image position when the scroll bars are not located on top and left.

For example I expect that the bottom/right pixel of the image will always be visible on the bottom/right position of the ScrollPane when scroll bars are at bottom and right. In my case it is not the case... I already looked at threads here but I don't see exactly the same situation (sometimes not showing ImageView sometimes not about zooming...). I am wondering if I don't just do a stupid mistake.

I give you the code here. Many thanks for your help.
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ScrollEvent;
import javafx.stage.Stage;

public class ZoomExample extends Application {

	private ImageView imageView;
	private ScrollPane scrollPane;
	private DoubleProperty zoom;

	@Override
	public void start(Stage primaryStage) throws Exception {
		zoom = new SimpleDoubleProperty(200);
		InvalidationListener listener = new InvalidationListener() {
			@Override
			public void invalidated(Observable arg0) {
				imageView.setFitWidth(zoom.get()*4);
				imageView.setFitHeight(zoom.get()*3);
				scrollPane.requestLayout();
			}
		};
		zoom.addListener(listener);

		scrollPane = new ScrollPane() {
	        @Override
	        protected void layoutChildren() {
	            super.layoutChildren();
	    		System.out.println("Zoom="+zoom.doubleValue()+" Bounds="+getViewportBounds());
	        }
		};
		scrollPane.setPannable(true);
		scrollPane.addEventFilter(ScrollEvent.ANY,
				new EventHandler<ScrollEvent>() {
					@Override
					public void handle(ScrollEvent event) {
						if (event.getDeltaY() > 0) {
							zoom.set(zoom.get() * 1.1);
						} else if (event.getDeltaY() < 0) {
							zoom.set(zoom.get() / 1.1);
						}
						event.consume();
					}
				});
		imageView = new ImageView(new Image("myImage.jpg")) {{
			setPreserveRatio(true);
		}};
		scrollPane.setContent(imageView);
		Scene scene = new Scene(scrollPane, zoom.get()*4, zoom.get()*3);
		primaryStage.setScene(scene);
		primaryStage.show();
		imageView.setFitWidth(zoom.get()*4);
		imageView.setFitHeight(zoom.get()*3);
		scrollPane.setHvalue(scrollPane.getHmax());
		scrollPane.setVvalue(scrollPane.getVmax());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		launch(args);
	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 7 2012
Added on Mar 9 2012
4 comments
5,479 views