如何在JavaFX应用程序中通过动画为角色创建老虎机?
我正在制作一个JavaFX应用程序。我在主窗口中有一个标签。我有一个从字母表生成随机字符的方法,但我想给它添加一些动画 我的意思是在2秒内旋转标签中的字符,然后出现一些随机字符。动画就像真正的老虎机一样 我没有找到像那样的图书馆 我该怎么做?如何在JavaFX应用程序中通过动画为角色创建老虎机?,java,javafx,scenebuilder,Java,Javafx,Scenebuilder,我正在制作一个JavaFX应用程序。我在主窗口中有一个标签。我有一个从字母表生成随机字符的方法,但我想给它添加一些动画 我的意思是在2秒内旋转标签中的字符,然后出现一些随机字符。动画就像真正的老虎机一样 我没有找到像那样的图书馆 我该怎么做? @FXML private Label answerID; //generate random character and apply it to the label private void generateChar() { Random r = ne
@FXML private Label answerID;
//generate random character and apply it to the label
private void generateChar() {
Random r = new Random();
String alphabet = "ABCDEFGHIKLMNOPQRSTUXYZ";
for (int i = 0; i < 25; i++) {
String text = "" + alphabet.charAt(r.nextInt(alphabet.length()));
answerID.setText(text);
}
}
@FXML私有标签应答器id;
//生成随机字符并将其应用于标签
私有void generateChar(){
随机r=新随机();
字符串字母表=“ABCDEFGHIKLMNOPQRSTUXYZ”;
对于(int i=0;i<25;i++){
字符串text=”“+alphabet.charAt(r.nextInt(alphabet.length());
answerID.setText(文本);
}
}
基本设置可以包括表示插槽的插槽,该插槽由文本
节点组成,该节点由堆栈窗格
扭曲
插槽
可以将动画应用于文本
,并为其设置随机字母
TilePane
保存所有插槽,因此所有插槽的大小都相同:
import java.util.Random;
import javafx.animation.Interpolator;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FxTest extends Application {
public static final String ALFA_BET = "ABCDEFGHIKLMNOPQRSTUXYZ";
private final Random rnd = new Random();
private TilePane main;
@Override
public void start(Stage primaryStage)throws Exception{
main = new TilePane(); //use tile pane: all tiles have same size
main.setPrefColumns(ALFA_BET.length());
main.setOrientation(Orientation.HORIZONTAL);
main.setHgap(1); main.setVgap(10); //vertical and horizontal space
makeSlots();
Button add = new Button("Spin");
add.setOnAction(e -> spin());
BorderPane root = new BorderPane(main);
root.setBottom(add);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
//primaryStage.sizeToScene();
primaryStage.show();
}
private void spin() {
for(Node node : main.getChildren()){
((Slot)node).spin();
}
}
private void makeSlots() {
for (int i=0; i< ALFA_BET.length(); i++){
main.getChildren().add(new Slot());
}
}
public static void main(String[] args) {
launch(null);
}
class Slot extends StackPane{
private final Text text;
private static final String format = "%1S";
private static final double WIDTH = 30, TRANS_SIZE = 30;
private SequentialTransition animation;
Slot(){
text = new Text();
setRandomText();
getChildren().add(text);
setPrefWidth(WIDTH);
//better apply using css
setStyle("-fx-padding: 5;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: blue;"+
//all letters have the same width
"-fx-font-family: 'monospaced';");
initializeAnimation();
}
//define animation to be applied to text
private void initializeAnimation() {
TranslateTransition t1 = new TranslateTransition();
t1.setDuration(Duration.millis(1));
t1.setNode(text);
t1.setFromX(0);
t1.setFromY(0);
t1.setToX(0);
t1.setToY(TRANS_SIZE);
t1.setInterpolator(Interpolator.LINEAR);
TranslateTransition t2 = new TranslateTransition();
t2.setDuration(Duration.millis(300));
t2.setNode(text);
t2.setFromX(0);
t2.setFromY(-TRANS_SIZE);
t2.setToX(0);
t2.setToY(TRANS_SIZE);
t2.setInterpolator(Interpolator.LINEAR);
TranslateTransition t3 = new TranslateTransition();
t3.setDuration(Duration.millis(1));
t3.setNode(text);
t3.setFromX(0);
t3.setFromY(TRANS_SIZE);
t3.setToX(0);
t3.setToY(0);
t3.setInterpolator(Interpolator.LINEAR);
//to play animations one by one
animation = new SequentialTransition(t1, t2, t3);
}
void spin() {
animation.play(); //animate
animation.setOnFinished(e-> setRandomText()); //change letter whaen animation ends
}
void setRandomText(){
char letter = ALFA_BET.charAt(rnd.nextInt(ALFA_BET.length()));
setText(letter );
}
void setText(char c){
text.setText(String.format(format, c) );
}
}
}
import java.util.Random;
导入javafx.animation.Interpolator;
导入javafx.animation.SequentialTransition;
导入javafx.animation.TranslateTransition;
导入javafx.application.application;
导入javafx.geometry.Orientation;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.StackPane;
导入javafx.scene.layout.TilePane;
导入javafx.scene.text.text;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类FxTest扩展了应用程序{
公共静态最终字符串ALFA_BET=“ABCDEFGHIKLMNOPQRSTUXYZ”;
私有最终随机rnd=新随机();
私人提利班大街;
@凌驾
public void start(Stage primaryStage)引发异常{
main=new TilePane();//使用平铺窗格:所有平铺大小相同
main.setPrefColumns(ALFA_BET.length());
main.setOrientation(方向.水平);
main.setHgap(1);main.setVgap(10);//垂直和水平空间
makeSlots();
按钮添加=新按钮(“旋转”);
add.setOnAction(e->spin());
BorderPane根=新的BorderPane(主);
根.立根(添加);
场景=新场景(根);
初级阶段。场景(场景);
//primaryStage.sizeToScene();
primaryStage.show();
}
私人空间旋转(){
对于(节点:main.getChildren()){
((插槽)节点);
}
}
私有void makeSlots(){
对于(int i=0;isetRandomText());//在动画结束时更改字母
}
void setRandomText(){
字符字母=ALFA_BET.charAt(rnd.nextInt(ALFA_BET.length());
setText(信函);
}
void setText(字符c){
text.setText(String.format(format,c));
}
}
}
我认为不需要图书馆。引用的TimelineEvents
变体说明了基本思想。不是你的下一个选民,但这样的情况往往很糟糕;请在您的问题中加入一个显示您修改过的方法的字符。@cOder谢谢您的回答,可以将所有字符放在一个插槽中吗?我只需要一个插槽,所有字符将在2秒内移动,然后出现一个随机字符是的,这是可能的。这个答案是为了告诉你大致的方向。试着实现你所需要的,如果你有问题,发布一个新的问题。尽量在问题中包含所有相关信息,包括mcve。如果你这样做了,请在这里给我留言。