4

Rectangleを使用して拡大およびText縮小するオブジェクトを含むグループを作成しようとしていScrollEventます。スクロールのデルタから使用する正しいスケールを取得するのに問題があります。次のコードは、私が現時点で持っているものです。どんな助けでも大歓迎です。

ありがとう

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();
                    }
                });

    }
}
4

1 に答える 1

9

|デルタY| すべてのスクロールイベントで同じです。また、deltaY は下にスクロールする場合は負であり、スケーリングは常に 1.0 で正規化されることに注意してください。したがって、スケーリングを決定する「ズーム係数」を使用することをお勧めします。次のようなものを試してください:

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();
              }
            });

  }
于 2012-12-05T08:32:22.357 に答える