首页 文章

如何在JavaFX中切换图像

提问于
浏览
2

我遇到的问题是,我目前正在使用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

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

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

    即使文件存在,也会返回true . 尝试删除第37,39和39行中 file 的位置 .

相关问题