有几种方法可以调整用户界面的大小。
按字体大小缩放
您可以通过设置缩放所有控件-fx-font-size
in the .root
场景的样式表。
例如,如果将以下样式表应用到场景中,则所有控件的大小都会加倍(因为默认字体大小为 13px)。
。根 {
-fx-字体大小:26px;
}
上面的方法将适用于缩放控件,这对于完全基于控件的事物来说很好,但对于基于图形和形状的事物来说不太好。
通过变换缩放
Apply a Scale http://docs.oracle.com/javafx/2/api/javafx/scene/transform/Scale.html以 (0,0) 为中心变换到场景的根节点。
Scale scale = new Scale(scaleFactor, scaleFactor);
scale.setPivotX(0);
scale.setPivotY(0);
scene.getRoot().getTransforms().setAll(scale);
为了缩放我开发的包含图形和各种形状的游戏,我使用了信箱技术,将游戏窗口的大小调整为恒定的纵横比(类似于您在 16 英寸电视上观看 4:3 电视节目时看到的信箱) :9 屏幕)。
下面代码中的 SceneSizeChangeListener 侦听场景大小的更改,并根据可用场景大小缩放场景内容。
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import org.jewelsea.games.supersnake.layout.LayoutController;
import java.io.IOException;
import java.util.ResourceBundle;
/* Main JavaFX application class */
public class SuperSnake extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(final Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(
getClass().getResource("layout/layout.fxml"),
ResourceBundle.getBundle("org.jewelsea.games.supersnake.layout.text")
);
Pane root = (Pane) loader.load();
GameManager.instance().setLayoutController(loader.<LayoutController>getController());
Scene scene = new Scene(new Group(root));
stage.setScene(scene);
stage.show();
GameManager.instance().showMenu();
letterbox(scene, root);
stage.setFullScreen(true);
}
private void letterbox(final Scene scene, final Pane contentPane) {
final double initWidth = scene.getWidth();
final double initHeight = scene.getHeight();
final double ratio = initWidth / initHeight;
SceneSizeChangeListener sizeListener = new SceneSizeChangeListener(scene, ratio, initHeight, initWidth, contentPane);
scene.widthProperty().addListener(sizeListener);
scene.heightProperty().addListener(sizeListener);
}
private static class SceneSizeChangeListener implements ChangeListener<Number> {
private final Scene scene;
private final double ratio;
private final double initHeight;
private final double initWidth;
private final Pane contentPane;
public SceneSizeChangeListener(Scene scene, double ratio, double initHeight, double initWidth, Pane contentPane) {
this.scene = scene;
this.ratio = ratio;
this.initHeight = initHeight;
this.initWidth = initWidth;
this.contentPane = contentPane;
}
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
final double newWidth = scene.getWidth();
final double newHeight = scene.getHeight();
double scaleFactor =
newWidth / newHeight > ratio
? newHeight / initHeight
: newWidth / initWidth;
if (scaleFactor >= 1) {
Scale scale = new Scale(scaleFactor, scaleFactor);
scale.setPivotX(0);
scale.setPivotY(0);
scene.getRoot().getTransforms().setAll(scale);
contentPane.setPrefWidth (newWidth / scaleFactor);
contentPane.setPrefHeight(newHeight / scaleFactor);
} else {
contentPane.setPrefWidth (Math.max(initWidth, newWidth));
contentPane.setPrefHeight(Math.max(initHeight, newHeight));
}
}
}
}
这是一个屏幕截图,您可以在其中看到信箱和缩放的效果。中间的绿草是主要的游戏内容屏幕,可以放大和缩小以适应可用的屏幕区域。外部周围的木质纹理提供了一个灵活大小的边框,如果您以与屏幕不同的宽高比观看电视节目,该边框将填充黑色信箱条通常所在的区域。请注意,下面的屏幕截图中的背景在标题页上是模糊的,因为我这样做,当游戏开始时,模糊效果将被删除,并且无论大小如何,视图都很清晰。
窗口版本:
缩放全屏版本:
您可能认为上面的缩放方法可能会使所有内容变得块状和像素化,但事实并非如此。所有字体和控件均可平滑缩放。所有标准绘图和图形命令以及基于 CSS 的样式都可以平滑缩放,因为它们都是基于矢量的。即使位图图像也能很好地缩放,因为 JavaFX 在缩放图像时使用相当高质量的过滤器。
获得良好图像缩放效果的一个技巧是提供高分辨率图像,这样当屏幕放大时,JavaFX 系统就有更多原始数据可供使用。例如,如果应用程序的首选窗口大小是屏幕大小的四分之一并且包含 64x64 图标,请改用 128x128 图标,以便当应用程序全屏显示且所有元素都缩放时,缩放器具有更多原始资源用于插值的像素数据样本。
由于硬件加速,缩放速度也很快。
如何删除单击“esc”键退出全屏模式时的消息和效果?
在 JavaFX 2.2 中无法删除全屏退出消息,但在 JavaFX 8 中可以:
RT-15314 允许受信任的应用程序禁用全屏覆盖警告并禁用“ESC 退出”行为 https://javafx-jira.kenai.com/browse/RT-15314
完成后会很好,因为这样我的游戏就不会产生“看着我 - 我看起来像测试版”的感觉。