如何在JavaFX中切换图像

时间:2017-11-24 02:10:42

标签: java image javafx imageview scene

我遇到的问题是,我目前正在使用Java来解决JavaFX和图形问题,我仍然认为自己是业余爱好者,我正在制作一个小像素游戏,以提高我的知识和经验图形。我想在几张图像之间交替,只是为了模拟我所创建的像素字符的“反弹”,只是为了给程序添加一些活力,但我在弄清楚如何做到这一点时遇到了很多麻烦,我花了很长时间试图找到解决我的具体问题无济于事。我将非常感谢你能给予的任何帮助,如果你有这样的帮助,请提示我改进我的代码以及我正在犯的错误。感谢您的时间。对不起,如果我错误地粘贴了代码,那么第一个计时器。

package view;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

// import JavaFX classes: Application, Stage, Scene, HBox, and Label.

public class JavaFXTesting extends Application {

    Stage window;
    Scene sceneIntro, sceneFBM;

    public static void main(String[] args) {

        // Launch the application.
        launch(args);
    }

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

        window = primaryStage;

        int loop = 0;

        primaryStage.getIcons().add(new Image("file:PixelChar.png"));
        Image pixelScene1 = new Image("file:PixelScene.png");
        Image pixelScene2 = new Image("file:PixelScene2.png");
        ImageView pixScene1 = new ImageView(pixelScene1);
        ImageView pixScene2 = new ImageView(pixelScene2);
        pixScene1.setPreserveRatio(true);
        pixScene2.setPreserveRatio(true);

        pixScene1.setFitWidth(400);
        pixScene1.setFitHeight(350);
        pixScene2.setFitWidth(400);
        pixScene2.setFitHeight(350);

        Label promptWelcome = new Label(":Welcome To Meme Adventure!:");
        Label promptIntro =
                new Label("You are currently playing the pre pre pre pre Alpha version\n"
                        +
                        " of Meme Adventrue, which involves you memeing on various enemies.\nEnjoy!");
        Label promptAction =
                new Label("Oh no! a wild FeelsBadMan as appeared!\n Quickly! Attack it.");

        promptWelcome.setFont(new Font("Impact", 20));
        promptIntro.setTextAlignment(TextAlignment.CENTER);
        promptAction.setTextAlignment(TextAlignment.CENTER);
        promptIntro.setFont(new Font("Franklin Gothic Demi", 13));
        promptAction.setFont(new Font("Franklin Gothic Demi", 13));
        promptIntro.setWrapText(true);
        promptAction.setWrapText(true);

        Button switchButton = new Button("Meme!");
        switchButton.setOnAction(e -> window.setScene(sceneFBM));
        Button testButton = new Button("Attack!");
        testButton.setOnAction(e -> window.setScene(sceneIntro));
        Button exitOption1 = new Button("Exit Program");
        exitOption1.setOnAction(e -> window.close());
        Button exitOption2 = new Button("Exit Program");
        exitOption2.setOnAction(e -> window.close());

        HBox buttonLayout2 = new HBox(10, exitOption1, switchButton);
        VBox intro = new VBox(10, pixScene1, promptWelcome, promptIntro, buttonLayout2);

        HBox buttonLayout1 = new HBox(10, exitOption2, testButton);
        VBox intro2 = new VBox(10, pixScene2, promptAction, buttonLayout1);

        intro.setAlignment(Pos.CENTER);
        intro.setPadding(new Insets(10));
        intro2.setAlignment(Pos.CENTER);
        buttonLayout1.setAlignment(Pos.CENTER);
        buttonLayout2.setAlignment(Pos.CENTER);
        intro2.setPadding(new Insets(10));

        sceneIntro = new Scene(intro);
        sceneFBM = new Scene(intro2);

        window.setScene(sceneIntro);
        window.setTitle("Meme Adventures");
        window.show();
    }
}

1 个答案:

答案 0 :(得分:0)

可能的错误可能是,在构建图像时,将file:放在文件名之前。这可以通过在创建图像后添加这行代码来证明。

System.out.println(pixelScene1.isError());

即使文件存在,也会返回true。尝试删除第37,39和39行所见的file