I have a ScreenController class that allows easy switching of the root Panes in a JavaFX program. It does this with a HashMap that stores Pane values with String keys.
I tried to set it up so that whenever new Panes are added to a SceneController object, it creates a full-screen version of the Pane given, and adds that to a second HashMap with the same String key as the first.
However, whenever I switch screens now, the full-screen version of the Pane is always used.
It seems like the goFullscreen()
method I am using to create the full-screen Pane versions is modifying both even though Java is pass-by-value.
How can I get the HashMap attribute in my ScreenController class, windowedRootMap, return the original Panes without full-screen scaling?
/**
* Creates an appropriately scaled fullscreen version of the argument Pane object.
*
* @param contentPane The Pane to create a fullscreen version of.
* @return Pane
*/
private static Pane goFullscreen(Pane contentPane) {
// get original dimensions and their ratio.
final double windowedWidth = 1280.0;
final double windowedHeight = 800.0;
final double windowedRatio = windowedWidth / windowedHeight;
// get fullscreen width and height from monitor dimensions
final double fullscreenWidth = Screen.getPrimary().getBounds().getWidth();
final double fullscreenHeight = Screen.getPrimary().getBounds().getHeight();
// find how much to scale by
double scaleFactor;
if (fullscreenWidth / fullscreenHeight > windowedRatio)
scaleFactor = fullscreenHeight / windowedHeight;
else
scaleFactor = fullscreenWidth / windowedWidth;
// scale the contents of the Pane appropriately
Scale scale = new Scale(scaleFactor, scaleFactor);
contentPane.getTransforms().setAll(scale);
contentPane.setPrefWidth(fullscreenWidth / scaleFactor);
contentPane.setPrefWidth(fullscreenHeight / scaleFactor);
return contentPane;
}
/**
* Allows switching root nodes easily.
*/
private class ScreenController {
private HashMap<String, Pane> windowedRootMap = new HashMap<>();
private HashMap<String, Pane> fullscreenRootMap = new HashMap<>();
private Scene currentScene;
private ScreenController(Scene currentScene) {
this.currentScene = currentScene;
}
private void addScreen(String name, Pane pane) {
this.windowedRootMap.put(name, pane);
this.fullscreenRootMap.put(name, goFullscreen(pane));
}
private void activate(String name) {
this.currentScene.setRoot(this.windowedRootMap.get(name));
}
}