Java FX中的鼠标滚动

Car*_*org 8 java javafx

我想用来mouse scroll使圆形更大(mouse-scroll-up)和圆形更小(mouse-scroll-down).

我现有的代码已经用鼠标中键改变圆半径,但我的问题是:

如何使用鼠标向上滚动和鼠标向下滚动来执行相同的操作?

我用google搜索,可以用某种方式完成ScrollEvent,但我无法理解这一点.

Ita*_*iha 11

回答你的问题

如何使用鼠标向上滚动和鼠标向下滚动来执行相同的操作?

  • ScrollListener在圆圈上使用a 并使用ScrollEvent引用来获取DeltaY.
  • 创建一个zoomfactor以计算您想要的缩放系数.
  • 将此因子应用于节点的"缩放"属性.

完整的例子

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class ZoomInOutCircles extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 350, 300);
        primaryStage.setTitle("Dots");
        primaryStage.setScene(scene);

        Circle circle = new Circle(175, 150, 10, Color.BLUE);
        addMouseScrolling(circle);
        root.getChildren().add(circle);

        primaryStage.show();
    }

    public void addMouseScrolling(Node node) {
        node.setOnScroll((ScrollEvent event) -> {
            // Adjust the zoom factor as per your requirement
            double zoomFactor = 1.05;
            double deltaY = event.getDeltaY();
            if (deltaY < 0){
                zoomFactor = 2.0 - zoomFactor;
            }
            node.setScaleX(node.getScaleX() * zoomFactor);
            node.setScaleY(node.getScaleY() * zoomFactor);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)