Java FX MVC多个视图

我正在使用Java FX(类项目是必需的),并且正在努力格式化MVC,以便可以将多个视图加载到舞台(各种场景)上。我有一个Controller,如下所示,其中有2个View1实例。

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.animation.AnimationTimer;

public class Controller extends Application {

public int page = 0;

private View1 view2;
private View1 view1;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage theStage) {
    view1 = new View1(theStage);
    view2 = new View1(theStage);
    new AnimationTimer() {
        public void handle(long currentNanoTime)
        {
            switch (page){
                case 0:
                    view1.update();
                    break;
                case 1:
                    view2.update();
                    break;
                default:
                    page = 0;
            }
            try {
                Thread.sleep(33);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();
    theStage.show();
  }

}

问题发生在第view2 = new View1(theStage);行。没有此行,输出是带有背景图像的大画布。但是,没有那条线,它只是空白画布。下面是我的视图,我尽可能地简化了它,只是一张背景图像,以确定它是否正确加载。 (为了简化起见,我省略了导入,如果需要,我可以将其重新添加)

public class View1 {
Stage theStage;
Group root;
Scene theScene;
Canvas canvas;

// value of the height and width of screen
int canvasWidth = 1000;
int canvasHeight = 800; 

GraphicsContext gc;

Image background;

//View1 constructor initialize the starting position for the image
//Called in controller
public View1(Stage theStage) {


    this.theStage = theStage;
    this.theStage.setTitle("Estuary Game");

    root = new Group();
    theScene = new Scene(root);
    this.theStage.setScene(theScene);

    canvas = new Canvas(canvasWidth,canvasHeight);
    root.getchildren().add(canvas);
    gc = canvas.getGraphicsContext2D();

    background = createImage("assets/mini-game-1.png");
}

//Read image from file and return
private Image createImage(String image_file) {
    Image img = new Image(image_file);
    return img;
}

//method used to repaint on the image and called in controller
public void update() {
    // draw background and sharkMove such like current
    gc.drawImage(background,0);

}

}

我不确定我是否正确处理了多个视图,我的意图是只包含多个场景,但不确定如何构造它。任何帮助将不胜感激。

wangjiajunwf 回答:Java FX MVC多个视图

尝试以下结构(以下代码是一个文件,将其复制粘贴到SwitchScene.java中并运行):

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class SwitchScene extends Application {

    public int page = 0;

    @Override
    public void start(Stage theStage) {

        new Controller(theStage);
        theStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

class Controller {

    private final View1 view1,view2;
    private final Stage stage;

    private static final String[] images = {
            "https://findicons.com/files/icons/345/summer/128/cake.png","http://icons.iconarchive.com/icons/atyourservice/service-categories/128/Sweets-icon.png"
    };

    Controller(Stage stage) {
        this.stage = stage;
        view1 = new View1(images[0]);
        view2 = new View1(images[1]);
        swapScenes();
    }

    void swapScenes(){

        new AnimationTimer() {

            int page = 2;
            @Override
            public void handle(long currentNanoTime){
                switch (page){
                    case 2:
                        page = 1;
                        stage.setScene(new Scene(new Group(view1)));
                        break;
                    case 1:
                        page = 2;
                        stage.setScene(new Scene(new Group(view2)));
                        break;
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

class View1 extends ImageView {

    public View1(String imagePath) {
        super(imagePath);
    }
}

编辑:使用画布的View1版本:

class View1 extends Group {

    public View1(String imagePath) {

        Image img = new Image(imagePath);
        Canvas canvas = new Canvas(img.getWidth(),img.getHeight());
        GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.drawImage(img,canvas.getWidth(),canvas.getHeight());
        getChildren().add(canvas);
    }
}
本文链接:https://www.f2er.com/3141860.html

大家都在问