简体   繁体   中英

Timer doesn't work but giving me no errors

Here is the class I'm using to run the "game" :

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();

    }
}

So everything is working perfectly inside my play() method, but the timer doesn't seem to work, it doesn't give me an error so I know it's not a compilation error but still it won't do what it's supposed to do here:

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

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

The class Ocean is as follow if it's important for you to see it:

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);
    }

}

I think you need to change a call

timer.schedule(task, 3000);

to

timer.schedule(task, 3000, 3000);

The first one only calls TimerTask once, The second call TimerTask at specified duration.

Also I think you should change your timer to deamon with call

new Timer(true)

According to Javadoc deamon is

daemon thread is called for if the timer will be used to schedule repeating "maintenance activities", which must be performed as long as the application is running

PS Also it'd be useful to set name to your timer - it can help with debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM