10

ScrollPane内でズームを必要とするアプリケーションがありますが、現在のアプローチでは、まだ2つの課題に直面しています。問題を再現するために、下にコードが表示される小さなアプリケーションZoomAppを作成しました。その制限された機能により、任意の形状で(Ctrl +マウスホイールを使用して)ズームインおよびズームアウトできます。ズームされたコンテンツがウィンドウの境界を超えて大きくなると、スクロールバーが表示されます。

課題1.innerGroupのサイズが拡大 した結果としてスクロールバーが表示されると、ScrollEventがZoomHandlerに到達しなくなります。代わりに、ウィンドウが一番下に達するまでウィンドウを下にスクロールし始め、ズームが再び期待どおりに機能するようにします。多分思った

scrollPane.setPannable(false);

違いはありますが、違います。この望ましくない動作をどのように回避する必要がありますか?

課題2.innerGroupの左上隅にあるピクセルを正方形に希望のデルタでペイントせずに、 scrollPane内でinnerGroup を中央に配置するにはどうすればよいですか?

補足として、JavaDoc for ScrollPaneによると、「アプリケーションが(スケーリングされたコンテンツなどの)ノードの視覚的境界に基づいてスクロールすることを望む場合、スクロールノードをグループにラップする必要があります」。これが、 ScrollPane内にinnerGroup outerGroupがある理由です。

私を解決策に導く提案は、このJavaFX初心者に大いに感謝されます。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 * Demo of a challenge I have with zooming inside a {@code ScrollPane}.
 * <br>
 * I am running JavaFx 2.2 on a Mac. {@code java -version} yields:
 * <pre>
 * java version "1.7.0_09"
 * Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
 * Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
 * </pre>
 * 6 rectangles are drawn, and can be zoomed in and out using either
 * <pre>
 * Ctrl + Mouse Wheel
 * or Ctrl + 2 fingers on the pad.
 * </pre>
 * It reproduces a problem I experience inside an application I am writing.
 * If you magnify to {@link #MAX_SCALE}, an interesting problem occurs when you try to zoom back to {@link #MIN_SCALE}. In the beginning
 * you will see that the {@code scrollPane} scrolls and consumes the {@code ScrollEvent} until we have scrolled to the bottom of the window.
 * Once the bottom of the window is reached, it behaves as expected (or at least as I was expecting).
 *
 * @author Skjalg Bjørndal
 * @since 2012.11.05
 */
public class ZoomApp extends Application {

    private static final int WINDOW_WIDTH = 800;
    private static final int WINDOW_HEIGHT = 600;

    private static final double MAX_SCALE = 2.5d;
    private static final double MIN_SCALE = .5d;

    private class ZoomHandler implements EventHandler<ScrollEvent> {

        private Node nodeToZoom;

        private ZoomHandler(Node nodeToZoom) {
            this.nodeToZoom = nodeToZoom;
        }

        @Override
        public void handle(ScrollEvent scrollEvent) {
            if (scrollEvent.isControlDown()) {
                final double scale = calculateScale(scrollEvent);
                nodeToZoom.setScaleX(scale);
                nodeToZoom.setScaleY(scale);
                scrollEvent.consume();
            }
        }

        private double calculateScale(ScrollEvent scrollEvent) {
            double scale = nodeToZoom.getScaleX() + scrollEvent.getDeltaY() / 100;

            if (scale <= MIN_SCALE) {
                scale = MIN_SCALE;
            } else if (scale >= MAX_SCALE) {
                scale = MAX_SCALE;
            }
            return scale;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {

        final Group innerGroup = createSixRectangles();
        final Group outerGroup = new Group(innerGroup);

        final ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(outerGroup);
        scrollPane.setOnScroll(new ZoomHandler(innerGroup));

        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(scrollPane);

        Scene scene = SceneBuilder.create()
                .width(WINDOW_WIDTH)
                .height(WINDOW_HEIGHT)
                .root(stackPane)
                .build();

        stage.setScene(scene);
        stage.show();
    }

    private Group createSixRectangles() {
        return new Group(
                createRectangle(0, 0), createRectangle(110, 0), createRectangle(220, 0),
                createRectangle(0, 110), createRectangle(110, 110), createRectangle(220, 110),
                createRectangle(0, 220), createRectangle(110, 220), createRectangle(220, 220)
        );
    }

    private Rectangle createRectangle(int x, int y) {
        Rectangle rectangle = new Rectangle(x, y, 100, 100);
        rectangle.setStroke(Color.ORANGERED);
        rectangle.setFill(Color.ORANGE);
        rectangle.setStrokeWidth(3d);
        return rectangle;
    }
}
4

3 に答える 3

13

OK、それで私はついに私の問題の解決策を見つけました。

行を置き換えるだけで

   scrollPane.setOnScroll(new ZoomHandler(innerGroup));

    scrollPane.addEventFilter(ScrollEvent.ANY, new ZoomHandler(innerGroup));

これで、期待どおりに機能します。神秘的な長方形やその他のハックは必要ありません。

では、次の質問はなぜですか?イベントの処理に関するこの優れた記事によると、

イベントフィルターは、イベントキャプチャフェーズ中に実行されます。

その間

イベントハンドラーは、イベントバブリングフェーズ中に実行されます。

これが違いを生むものだと思います。

于 2012-11-12T11:28:10.203 に答える
2

次の回避策により、より良い結果が得られるようです。

  1. 外側のグループに onscroll イベントを設定して、スクロール ペインからイベントを取得します。
  2. 画面全体を覆う不透明な四角形を追加して、スクロール イベントを見逃さないようにします。どうやら、形状に当たらない場合、スクロールイベントを見逃す可能性があります。

    Rectangle opaque = new Rectangle(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    opaque.setOpacity( 0 );
    outerGroup.getChildren().add( opaque );
    outerGroup.setOnScroll(new ZoomHandler(innerGroup));
    
于 2012-11-08T23:45:05.797 に答える