如何让舞台在计时器

时间:2015-06-26 01:11:15

标签: timer javafx

单击开始按钮后,我的程序将不显示第二个场景和线程,直到线程完成。并且线程将无法正常运行(草没有增加)。

public class test111 extends Application {
    private static int grass_i=130;
    private static int grass_j=200;
    private static int[][] map = new int[grass_i][grass_j];//草地資料grassdata
    private static int totalgrass; //grass total number
    private static int totalgrassrest; //grass rest number
    private static int months=1; //月
    private Timer timer;//時間
    //GUI
    Scene menusc ;
    //START後的視窗
    BorderPane bpAll = new BorderPane();  
    Pane playView = new Pane();
    Scene mainsc = new Scene(bpAll);
    //草
    Image littlegrassImg = new Image(getClass().getResourceAsStream("map_littlegrass.png"));
    Image midiumgrassImg = new Image(getClass().getResourceAsStream("map_midiumgrass.png"));
    Image muchgrassImg = new Image(getClass().getResourceAsStream("map_muchgrass.png"));
    Rectangle2D[][] viewportRect = new Rectangle2D[130][200];
    private static ImageView[][] mapView = new ImageView[130][200];
    //時間
    @Override
    public void start(Stage primaryStage) throws InterruptedException {     
        //MENU
        Pane menu = new Pane();
        menusc = new Scene(menu);
        menu.setPrefSize(1000, 800);
        Image MenuImg = new Image(getClass().getResourceAsStream("Menu_title.jpg"));        
        Image StartImg = new Image(getClass().getResourceAsStream("Start.jpg"));    
        menu.getChildren().add(new ImageView(MenuImg)); 
        Button StartBt = new Button();
        StartBt.setPrefSize(450, 150);
        StartBt.setGraphic(new ImageView(StartImg));
        StartBt.setMaxSize(450, 150);
        StartBt.setLayoutX(300);
        StartBt.setLayoutY(600);
        menu.getChildren().addAll(new ImageView(MenuImg),StartBt);
        primaryStage.setMinHeight(800);//固定視窗大小fix the size of Stage
        primaryStage.setMinWidth(1000);//固定視窗大小fix the size of Stage
        primaryStage.setMaxHeight(800);//固定視窗大小fix the size of Stage
        primaryStage.setMaxWidth(1000);//固定視窗大小fix the size of Stage
        primaryStage.setScene(menusc);
        primaryStage.setTitle("Hypnos' yard");

        //START
        StartBt.setOnAction(e->{
            BorderPane bp2 = bp2(); 
            menusc = new Scene(bp2);
            primaryStage.setScene(menusc);
        });
    primaryStage.show();
}
public BorderPane bp2(){
    playView = playView();
    bpAll = new BorderPane();
    bpAll.setPrefSize(1000, 800);
    playView.setPrefSize(1000, 650);  //庭院
    bpAll.setTop(playView);  
    Random ran = new Random();
    totalgrass = (ran.nextInt(50)*1000)+48000;
    totalgrassrest = totalgrass;
    grasscreate();
    //設定初始草地construct the initial grass
    grassGraphicsLoading(); //loading grass pictures

    return bpAll;
}

public Pane playView(){
    playView = new Pane();

    while(months<12){

        timer = new Timer();
        TimerTask task = new TimerTask(){
            public void run(){
            month();
            }
        };//after program executing 0.2s, a month past
        timer.schedule(task, 200);
        try {
            Thread.sleep(200);
        }
        catch(InterruptedException e6) {
        }                   
        timer.cancel();                 
    }       
    return playView;
}
//月month
protected void month(){
    if(months<13){
        grassgrow(totalgrass*2);//grass growth
        Platform.runLater(new Runnable(){
            @Override
                public void run(){
                grassGraphicsLoading();             
            }
        });
    months++;
    }
}
//把草地資料帶入圖形loading grass pictures
private void grassGraphicsLoading(){

    for (int i = 0; i < grass_i; i++) { // 設定imageView的圖形和位置
        for (int j = 0; j < grass_j; j++) {
            viewportRect[i][j] = new Rectangle2D(j * 5, i * 5, 5, 5);
            if (170 <= j && j <= grass_j - 1) {
                mapView[i][j] = new ImageView(muchgrassImg);
            } else if (140 <= j && j < 170) {
                mapView[i][j] = new ImageView(muchgrassImg);
            } else {
                if (map[i][j] == 0) {
                    mapView[i][j] = new ImageView(littlegrassImg);
                } else if (map[i][j] == 1 || map[i][j] == 2) {
                    mapView[i][j] = new ImageView(midiumgrassImg);
                } else {
                    mapView[i][j] = new ImageView(muchgrassImg);
                }
            }
            playView.getChildren().add(mapView[i][j]);
            mapView[i][j].setViewport(viewportRect[i][j]);
            mapView[i][j].setX(j * 5);
            mapView[i][j].setY(i * 5);

        }
    }
}
public static void main(String[] args) {
    launch(args);   
}
// 每經過30天(一個月)草地+1grassgrowth
protected void grassgrow(int sum) {

    for (int i = 0; i < grass_i; i++) {
        for (int j = grass_j - 1; j >= 0; j--) {
            if (map[i][j] < 4 && totalgrass <sum) {
                map[i][j] += 1;
                totalgrass++;
            }
            if (totalgrass == 104000||totalgrass ==sum) {
                break;
            }
        }
    }
}
//建構草地construct grass
protected void grasscreate() {
    for (int j = grass_j - 1; j >= 0; j--) {
        for (int i = grass_i - 1; i >= 0; i--) {
            if (170 <= j && j <= grass_j - 1) {
                map[i][j] = 0;
            } else if (140 <= j && j < 170) {
                map[i][j] = 4;
                totalgrassrest -= 4;
            } else if (totalgrassrest <= 0) {
                map[i][j] = 0;
            } else if (4 * (j + 1) * grass_i <= totalgrassrest) {
                map[i][j] = 4;
                totalgrassrest -= 4;
            } else if (totalgrassrest < 4) {
                map[i][j] = totalgrassrest;
                totalgrassrest = 0;
            } else {
                map[i][j] = rancreate();
                totalgrassrest -= map[i][j];
            }
        }
    }
    totalgrass -= totalgrassrest;
    totalgrassrest = 0;
}

// 一格(5X5)內草地數量隨機1-4平米z there is 1-4 grass inside a rectangle
private int rancreate() {
    Random ran = new Random();
    int grass = ran.nextInt(5);
    return grass;
}

}`

0 个答案:

没有答案