在javafx窗格中设置来自阵列的图像动画,以便在剪辑窗口中无缝滚动

在javafx窗格中设置来自阵列的图像动画,以便在剪辑窗口中无缝滚动,java,animation,javafx,observablelist,Java,Animation,Javafx,Observablelist,我正在用javafx创建老虎机应用程序。期望的行为:在动画(樱桃、柠檬、数字服务器等的图片对用户来说应该是一个整体的和平)中,分离的图片必须在窗格中显示为真实的老虎机鼓,就像在图片上显示一样 我的问题是,我不能把单独的图像放在一起,以便在老虎机窗口中无缝滚动。 我对这个问题做了很多研究,但没有找到任何有效的解决办法。我尝试在ArrayList中添加所有图像,然后在动画过程中将它们设置为TranslateTransition引用的节点。但是初始图像堆栈在windows中 import javaf

我正在用javafx创建老虎机应用程序。期望的行为:在动画(樱桃、柠檬、数字服务器等的图片对用户来说应该是一个整体的和平)中,分离的图片必须在窗格中显示为真实的老虎机鼓,就像在图片上显示一样

我的问题是,我不能把单独的图像放在一起,以便在老虎机窗口中无缝滚动。 我对这个问题做了很多研究,但没有找到任何有效的解决办法。我尝试在ArrayList中添加所有图像,然后在动画过程中将它们设置为TranslateTransition引用的节点。但是初始图像堆栈在windows中

import javafx.animation.Interpolator;
import javafx.animation.ParallelTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TestClass extends Application {

private BorderPane borderPane = new BorderPane();
private GridPane gridPane = new GridPane();

@Override
public void start(Stage primaryStage) throws Exception {

    ImageView image1 = new ImageView(createImage(Color.RED));
    ImageView image2 = new ImageView(createImage(Color.GREEN));
    ImageView image3 = new ImageView(createImage(Color.BLUE));

    gridPane.setLayoutX(50);
    gridPane.setLayoutY(50);
    gridPane.setPadding(new Insets(5, 5, 5, 5));
    gridPane.setAlignment(Pos.CENTER);
    gridPane.setVgap(5);

    gridPane.add(image1, 0, 0);
    gridPane.add(image2, 1, 0);
    gridPane.add(image3, 2, 0);
    gridPane.setMaxWidth(image1.getFitWidth() * 3);
    gridPane.setMaxHeight(image1.getFitHeight());

    Rectangle clip = new Rectangle(732, 230);
    clip.setLayoutX(30);
    clip.setLayoutY(10);
    clip.setStroke(Color.BLACK);

    // clip.setFill(null);

    gridPane.setClip(clip);

    borderPane.setCenter(gridPane);
    Scene scene = new Scene(borderPane, 900, 500);
    primaryStage.setScene(scene);
    primaryStage.setTitle("SlotMachine");
    primaryStage.show();

    ImageView[] images = { image1, image2, image3 };

    TranslateTransition t1 = new TranslateTransition();
    for (ImageView i : images) {

        t1.setDuration(Duration.millis(2000));
        t1.setNode(i);
        t1.setFromX(image1.getX());
        t1.setFromY(image1.getY() - gridPane.getHeight());
        t1.setToX(image1.getX());
        t1.setToY(image1.getY() - image1.getFitHeight() / 2 + gridPane.getHeight());
        t1.setCycleCount(2);

        t1.setAutoReverse(false);
        t1.setInterpolator(Interpolator.LINEAR);
    }

    TranslateTransition t2 = new TranslateTransition();
    for (ImageView i : images) {

        t2.setDuration(Duration.millis(2000));
        t2.setNode(i);
        t2.setFromX(image2.getX());
        t2.setFromY(image2.getY() - gridPane.getHeight());
        t2.setToX(image2.getX());
        t2.setToY(image2.getY() - image2.getFitHeight() / 2 + gridPane.getHeight());
        t2.setCycleCount(2);

        t2.setAutoReverse(false);
        t2.setInterpolator(Interpolator.LINEAR);
    }

    TranslateTransition t3 = new TranslateTransition();
    for (ImageView i : images) {

        t3.setDuration(Duration.millis(2000));
        t3.setNode(i);
        t3.setFromX(image3.getX());
        t3.setFromY(image3.getY() - gridPane.getHeight());
        t3.setToX(image3.getX());
        t3.setToY(image3.getY() - image3.getFitHeight() / 2 + gridPane.getHeight());
        t3.setCycleCount(2);

        t3.setAutoReverse(false);
        t3.setInterpolator(Interpolator.LINEAR);
    }

    ParallelTransition pt = new ParallelTransition(t2, t3, t1);
    pt.play();
}

private final Image createImage(Color color) {
    Rectangle rect = new Rectangle(32, 32);
    rect.setFill(color);
    return rect.snapshot(null, null);

}

public static void main(String[] args) {
    launch(args);
}
}
请帮忙。提前感谢您

从我所知道的情况来看,您似乎没有使用模型/视图/控制器体系结构;在我看来,使用这种架构是实现这样一个gui的最简单的方法。在您的情况下,您可以根据以下psuedo代码对逻辑进行建模:

在控制器中:

//changes x or y coordinate (depending on direction of movement) of top left corner
//the controller should change the values of x or y that are stored in your model    
moveImageMethod(image); 
//Number of ms between timer events, play with this number to make it smooth
private static final int TIMER_INTERVAL = 1000/30;

//constructor
public View(){
    <your other stuff, event handlers, etc.>
    this.timer = new Timer(TIMER_INTERVAL, new ActionListener() { 
            @Override 
            public void actionPerformed(ActionEvent e) { 
                   handleTimerTick(); 
            } 
     }); 
}

// Start the animation timer. 
public void startTimer() { 
       timer.start(); 
} 

protected void handleTimerTick(){
    controller.moveImageMethod(image);
    <other stuff you might need/want>
    repaint();
}

<your graphics methods>
视图中:

//changes x or y coordinate (depending on direction of movement) of top left corner
//the controller should change the values of x or y that are stored in your model    
moveImageMethod(image); 
//Number of ms between timer events, play with this number to make it smooth
private static final int TIMER_INTERVAL = 1000/30;

//constructor
public View(){
    <your other stuff, event handlers, etc.>
    this.timer = new Timer(TIMER_INTERVAL, new ActionListener() { 
            @Override 
            public void actionPerformed(ActionEvent e) { 
                   handleTimerTick(); 
            } 
     }); 
}

// Start the animation timer. 
public void startTimer() { 
       timer.start(); 
} 

protected void handleTimerTick(){
    controller.moveImageMethod(image);
    <other stuff you might need/want>
    repaint();
}

<your graphics methods>
//计时器事件之间的毫秒数,使用此数字使其平滑
专用静态最终整数计时器间隔=1000/30;
//建造师
公众观点(){
this.timer=new timer(timer\u INTERVAL,new ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
handleTimerTick();
} 
}); 
}
//启动动画计时器。
public void startTimer(){
timer.start();
} 
受保护的void handleTimerTick(){
控制器。移动图像方法(图像);
重新油漆();
}

请参阅:如何创建。James\D我已编辑了我的问题。现在是更清楚了,还是我必须提供更多细节?我是这个社区的新手,不熟悉如何提问的规则。请阅读我之前评论中的链接。我已经仔细阅读了这些文章,并根据它们修改了我的问题。请帮我找到解决问题的办法。谢谢。请创建一个链接,并将您的问题包括在内。