简体   繁体   English

使用Scroll Event放大和缩小

[英]Zooming in and out using Scroll Event

I am trying to make a group which contains Rectangle and Text objects to scale up and down using the ScrollEvent . 我正在尝试创建一个包含RectangleText对象的组,以使用ScrollEvent向上和向下扩展。 I am having trouble getting the correct scale to use from the delta of the scroll. 我无法从滚动的三角形中获取正确的比例。 The following code is what I have at the moment. 以下代码是我目前所拥有的。 Any help will be appreciated. 任何帮助将不胜感激。

Thanks 谢谢

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

// demonstrates scaling a test pane with content in it.
public class ScaleTest extends Application {
    Group page;
    double textScale =1.0;
    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("scale test");
        Group root = new Group();
        page= new Group();
        Rectangle r = new Rectangle(300,300);
        Text t = new Text("asodjoijdjiasjdoijaisjdijasijdiajsidjoiasjiodjiajosijdiojaisjdoijaoisjdiojaoisjdjaoisjdiojsoidj\njdoasijdoajsoidjoaisjoidjaoisjdiojs");
        t.setFill(Color.FLORALWHITE);
        t.setWrappingWidth(280);
        t.setX(10);
        t.setY(20);
        page.getChildren().addAll(r,t);
        root.getChildren().add(page);
        Scene scene = new Scene(root,800,600);
        this.setSceneEvents(scene);
        stage.setScene(scene);
        stage.show();
    }
    private void setSceneEvents(final Scene scene) {
        //handles mouse scrolling
        scene.setOnScroll(
                new EventHandler<ScrollEvent>() {
                    @Override
                    public void handle(ScrollEvent event) {
                        double xscale = page.getScaleX() * event.getDeltaY()/35;
                        double yscale= page.getScaleY() * event.getDeltaY()/35;
                        System.out.println("xscale: "+ xscale);
                        System.out.println("yscale: "+ yscale);
                        //page.setScaleX(page.getScaleX() * event.getDeltaY()/35);
                       // page.setScaleY(page.getScaleY() * event.getDeltaY()/35);
                        event.consume();
                    }
                });

    }
}

|deltaY| |移动deltaY | is for all scroll events the same. 适用于所有滚动事件。 Also note that deltaY is negative for scrolling down, and the scaling always is normalized at 1.0. 另请注意,deltaY对于向下滚动为负,并且缩放始终标准化为1.0。 So i suggest to use a "zoom factor" which determines the scaling. 所以我建议使用“缩放因子”来确定缩放比例。 Try something like: 尝试类似的东西:

private void setSceneEvents(final Scene scene) {
    //handles mouse scrolling
    scene.setOnScroll(
            new EventHandler<ScrollEvent>() {
              @Override
              public void handle(ScrollEvent event) {
                double zoomFactor = 1.05;
                double deltaY = event.getDeltaY();
                if (deltaY < 0){
                  zoomFactor = 2.0 - zoomFactor;
                }
                System.out.println(zoomFactor);
                page.setScaleX(page.getScaleX() * zoomFactor);
                page.setScaleY(page.getScaleY() * zoomFactor);
                event.consume();
              }
            });

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM