Last active
July 12, 2021 19:11
-
-
Save jewelsea/5032398 to your computer and use it in GitHub Desktop.
Create a pannable map background in JavaFX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.narniaweb.com/wp-content/uploads/2009/08/NarniaMap.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); } | |
} |
This Code only produces an empty scene for me.
The ScrollPane seems to be present, but the image and button aren't.
Can you check on that?
I use JavaFX8 3.7.0, apparently.
Update: Changing the URL to use https instead of http fixes it ^^
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks, I learned a lot from this!