Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Last active July 12, 2021 19:11
Show Gist options
  • Select an option

  • Save jewelsea/5032398 to your computer and use it in GitHub Desktop.

Select an option

Save jewelsea/5032398 to your computer and use it in GitHub Desktop.

Revisions

  1. jewelsea revised this gist Jul 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PannableView.java
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ public class PannableView extends Application {
    private Image backgroundImage;

    @Override public void init() {
    backgroundImage = new Image("http://www.woodge.com/books/maps/map_Narnia.jpg");
    backgroundImage = new Image("http://www.narniaweb.com/wp-content/uploads/2009/08/NarniaMap.jpg");
    }

    @Override public void start(Stage stage) {
  2. jewelsea created this gist Feb 25, 2013.
    76 changes: 76 additions & 0 deletions PannableView.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    import javafx.application.Application;
    import static javafx.application.Application.launch;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;

    /** Constructs a scene with a pannable Map background. */
    public class PannableView extends Application {
    private Image backgroundImage;

    @Override public void init() {
    backgroundImage = new Image("http://www.woodge.com/books/maps/map_Narnia.jpg");
    }

    @Override public void start(Stage stage) {
    stage.setTitle("Drag the mouse to pan the map");

    // construct the scene contents over a stacked background.
    StackPane layout = new StackPane();
    layout.getChildren().setAll(
    new ImageView(backgroundImage),
    createKillButton()
    );

    // wrap the scene contents in a pannable scroll pane.
    ScrollPane scroll = createScrollPane(layout);

    // show the scene.
    Scene scene = new Scene(scroll);
    stage.setScene(scene);
    stage.show();

    // bind the preferred size of the scroll area to the size of the scene.
    scroll.prefWidthProperty().bind(scene.widthProperty());
    scroll.prefHeightProperty().bind(scene.widthProperty());

    // center the scroll contents.
    scroll.setHvalue(scroll.getHmin() + (scroll.getHmax() - scroll.getHmin()) / 2);
    scroll.setVvalue(scroll.getVmin() + (scroll.getVmax() - scroll.getVmin()) / 2);
    }

    /** @return a control to place on the scene. */
    private Button createKillButton() {
    final Button killButton = new Button("Kill the evil witch");
    killButton.setStyle("-fx-base: firebrick;");
    killButton.setTranslateX(65);
    killButton.setTranslateY(-130);
    killButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent t) {
    killButton.setStyle("-fx-base: forestgreen;");
    killButton.setText("Ding-Dong! The Witch is Dead");
    }
    });
    return killButton;
    }

    /** @return a ScrollPane which scrolls the layout. */
    private ScrollPane createScrollPane(Pane layout) {
    ScrollPane scroll = new ScrollPane();
    scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
    scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
    scroll.setPannable(true);
    scroll.setPrefSize(800, 600);
    scroll.setContent(layout);
    return scroll;
    }

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