计时器不起作用但没有错误

时间:2016-02-04 18:18:07

标签: java timer

这是我用来运行“游戏”的课程:

package glaces;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class Game {

    public Game(){

    }

    public static void play(){

    Scanner sc = new Scanner(System.in);

        int continueLoop = 1;
    final Ocean ocean = new Ocean();
    int[][] tab = ocean.getColors();
    int[][] tab1;
    Pingouin pingouin = new Pingouin(ocean,tab);
    ArcticImage img = new ArcticImage(ocean.getWidth(),ocean.getHeight());
    img.setColors(tab);

    TimerTask task = new TimerTask() {
        public void run() {
            ocean.meltIcebergs(1.);
        }
        };

    Timer timer = new Timer();
    timer.schedule(task, 3000);

    while(continueLoop == 1){
        System.out.println("Veuillez saisir une lettre : \n \t->X : Avancer le pingouin en bas.\n \t->Z : Avancer le pingouin en haut.\n \t->Q : Avancer le pingouin a gauche.\n \t->D : Avancer le pingouin a droite.");

        String choice = sc.next();

        if(choice.equals("x") || choice.equals("X")){
        if(pingouin.getHeight() < ocean.getHeight()){
            tab1 = pingouin.moveX();
            img.setColors(tab1);
        }
        } else if(choice.equals("z") || choice.equals("Z")){
        if(pingouin.getHeight() < ocean.getHeight()){
            tab1 = pingouin.moveZ();
            img.setColors(tab1);
        }
        } else if(choice.equals("q") || choice.equals("Q")){
        if(pingouin.getWidth() < ocean.getWidth()){
            tab1 = pingouin.moveQ();
            img.setColors(tab1);
        }
        } else if(choice.equals("d") || choice.equals("D")){
        if(pingouin.getWidth() < ocean.getWidth()){
            tab1 = pingouin.moveD();
            img.setColors(tab1);
        }
        } else{
        System.out.println("Merci, au revoir.");
        continueLoop=0;
        }

    }

    } 




    public static void main(String[] args) {


    play();

    }
}

所以一切都在我的play()方法中完美运行,但是计时器似乎不起作用,它不会给我一个错误,所以我知道它不是编译错误,但它仍然不会做它是什么应该在这里做:

TimerTask task = new TimerTask() {
        public void run() {
            ocean.meltIcebergs(1.);
        }
        };

    Timer timer = new Timer();
    timer.schedule(task, 3000);

如果对你来说很重要,那么海洋课程如下:

package glaces;
import geometrie.Point ;
import java.util.Random;

public class Ocean {

    private Iceberg2D[] tab;
    private int heightOfTable;
    private int widthOfTable;

    public Ocean(){
    heightOfTable = 300;
    widthOfTable = 300;
        Random rand = new Random();
    int randNumb = rand.nextInt(6); // number of iceberg
    tab = new Iceberg2D[randNumb + 1];

        for(int i=0; i<tab.length; i++){
        int randX1 = rand.nextInt(200);
        int randY1 = rand.nextInt(200);
        int randX2 = rand.nextInt(21)+200;
        int randY2 = rand.nextInt(21)+200;
        tab[i] = new Iceberg2D(new Point(randX1,randY1),new Point(randX2,randY2));
    }
    }

    public Ocean(int nb, int height, int width){
    tab = new Iceberg2D[nb];
    heightOfTable = height;
    widthOfTable = width;

    Random rand = new Random();
    for(int i=0; i<tab.length; i++){
        int randX1 = rand.nextInt(heightOfTable-1);
        int randY1 = rand.nextInt(heightOfTable-1);
        int randX2 = rand.nextInt(widthOfTable-randX1)+(randX1);
        int randY2 = rand.nextInt(widthOfTable-randY1)+(randY1);
        tab[i] = new Iceberg2D(new Point(randX1,randY1),new Point(randX2,randY2));
    }

    }

    public int getWidth(){
    return heightOfTable;
    }

    public int getHeight(){
    return widthOfTable;
    }

    public int getCount(){
    return tab.length;
    }

    public void meltIcebergs(double fr){
    for(int i = 0; i<tab.length; i++){
        tab[i].meltIcebergs(fr);
    }
    }

    public int[][] getColors(){
    int i,j,k;
    int[][] table = new int[heightOfTable][widthOfTable];

    for(i=0; i<table.length; i++){
        for(j=0; j<table.length; j++){
        table[i][j] = 0;
        }
    }
    for(i=0; i<tab.length; i++){
        int w1 = (int) this.tab[i].cornerBottomLeft().getAbscisse();
        int w2 = (int) this.tab[i].cornerTopRight().getAbscisse();
        int h1 = (int) this.tab[i].cornerBottomLeft().getOrdonnee();
        int h2 = (int) this.tab[i].coinEnHautADroite().getOrdonnee();
        for(k=0; k<table.length; k++){
        for(j=0; j<table.length; j++){
            if(k > w1 && 
               k < w2 &&
               j > h1 &&
               j < h2
               ){
            table[k][j] = 1;
            }
        }
        }

    }
    return table;
    }

    public String toString(){
    return "Il y a "+getCount()+" iceberg | L'hauteur de l'ocean est "+getHeight()+" et la largeur de l'ocean est " + getWidth();
    }

    public static void main(String[] args) {
    Ocean ocean = new Ocean(2,700,700);
    int[][] i = ocean.getColors();
        ArcticImage img = new ArcticImage(700,700);
    img.setColors(i);
    }

}

1 个答案:

答案 0 :(得分:0)

我认为你需要改变一个电话

timer.schedule(task, 3000);

timer.schedule(task, 3000, 3000);

第一个只调用TimerTask一次,第二个调用TimerTask指定的持续时间。

此外,我认为您应该通过电话

将计时器更改为deamon
new Timer(true)

根据Javadoc deamon的说法

  如果定时器将用于安排重复的“维护活动”,则会调用

守护程序线程,只要应用程序正在运行,就必须执行该活动

P.S。将名称设置为计时器也很有用 - 它可以帮助调试。