timer manipulation in JavaFX

Sometimes I am halfway through the Hangman game and the “time is up” alert interrupts me not on its perfect time. The remaining time is 40 secs as an example and it tells me my time is up, but the problem is that this happens sometimes and NOT in every single game.It might be due to the JavaFX timer features but I’m no pro. Maybe there is sthg I need to know. I posted asking for help after trying to figure it out myself and not being able to, I don’t need someone to come tell me this is a code dump. I wouldn’t have posted if I figured it out myself in the 1st place!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Controller class:
```
//libraries import
public class Controller {
//other FXML elements
@FXML
private Text time;
@FXML
private Text time1;
//variables used defined;
public void initialize() {
language1.setText("Please select a language 1.English 2.French ");
english.setVisible(true);
french.setVisible(true);
//all others elements are invisible
}
public void onClick(ActionEvent event){
String letter = ((Button)event.getSource()).getText().toLowerCase();
((Button) event.getSource()).setDisable(true);
time1.setText("");
String str1 = timer.remain();
time.setText("The remining time is "+str1+" seconds");}
if(myLetters.contains(letter)){
int letterIndex = myLetters.indexOf(letter);
while (letterIndex != -1) {
correct++;
myLetters.set(letterIndex, "*");
answer.set(letterIndex*2, letter);
letterIndex = myLetters.indexOf(letter);
String res = String.join("", answer);
text.setText(res);
}
if(correct==word.length()){
winStatus.setText("You Win!");}
buttons.setDisable(true);
timer.stop();
}
}
else{
mistakes++;
if(timer.isFinished()){
timer.stop();
timer.showAlertEnglish();
buttons.setDisable(true);
}
if(mistakes ==1) base1.setVisible(true);
// show other parts of hangman
else if (mistakes ==8){
man.setVisible(true);
winStatus.setText("You Lose!");
realWord.setText("The actual word was " + word);
buttons.setDisable(true);
timer.stop();
}
}
}
public void select(ActionEvent event) {
//chnages the game due to the selected language
time1.setText("Vous avez 1 minute. Allez !");
timer.start();
}
public void newGame(){
time.setText("");
for(int i=0; i<35; i++){
buttons.getChildren().get(i).setDisable(false);
}
newGameButton.setText("");
initialize();
}
}```
Timer class:
``` mport java.util.Timer;
import java.util.TimerTask;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
public class timer {
private static long startTime;
private static long remaining;
private static long remainingTimeSeconds;
private static long remainingTimeMilliSeconds;
private static boolean timeUp;
private static Timer timer;
public static void start() {
startTime = System.currentTimeMillis();
timeUp = false;
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
timeUp = true;
}
}, 60 * 1000);
}
public static String remain() {
remaining=(60 * 1000) - (System.currentTimeMillis() - startTime);
remainingTimeSeconds = (remaining)/ 1000;
remainingTimeMilliSeconds= (remaining)%1000;
if (remainingTimeMilliSeconds<0) {remainingTimeMilliSeconds=0;}
String ch=String.valueOf(remainingTimeSeconds)+"."+String.valueOf(remainingTimeMilliSeconds);
return ch;
}
public static boolean isFinished() {
return timeUp;
}
public static void stop() {
timeUp=true;
}
public static void showAlertEnglish() {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Time's Up!");
alert.setHeaderText(null);
alert.setContentText("You ran out of time. You lose!");
alert.showAndWait();
}
public static void showAlertFrench() {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Temps écoulé !");
alert.setHeaderText(null);
alert.setContentText("Vous avez dépassé le temps imparti. Vous avez perdu !");
alert.showAndWait();
}
}
```
</code>
<code>Controller class: ``` //libraries import public class Controller { //other FXML elements @FXML private Text time; @FXML private Text time1; //variables used defined; public void initialize() { language1.setText("Please select a language 1.English 2.French "); english.setVisible(true); french.setVisible(true); //all others elements are invisible } public void onClick(ActionEvent event){ String letter = ((Button)event.getSource()).getText().toLowerCase(); ((Button) event.getSource()).setDisable(true); time1.setText(""); String str1 = timer.remain(); time.setText("The remining time is "+str1+" seconds");} if(myLetters.contains(letter)){ int letterIndex = myLetters.indexOf(letter); while (letterIndex != -1) { correct++; myLetters.set(letterIndex, "*"); answer.set(letterIndex*2, letter); letterIndex = myLetters.indexOf(letter); String res = String.join("", answer); text.setText(res); } if(correct==word.length()){ winStatus.setText("You Win!");} buttons.setDisable(true); timer.stop(); } } else{ mistakes++; if(timer.isFinished()){ timer.stop(); timer.showAlertEnglish(); buttons.setDisable(true); } if(mistakes ==1) base1.setVisible(true); // show other parts of hangman else if (mistakes ==8){ man.setVisible(true); winStatus.setText("You Lose!"); realWord.setText("The actual word was " + word); buttons.setDisable(true); timer.stop(); } } } public void select(ActionEvent event) { //chnages the game due to the selected language time1.setText("Vous avez 1 minute. Allez !"); timer.start(); } public void newGame(){ time.setText(""); for(int i=0; i<35; i++){ buttons.getChildren().get(i).setDisable(false); } newGameButton.setText(""); initialize(); } }``` Timer class: ``` mport java.util.Timer; import java.util.TimerTask; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; public class timer { private static long startTime; private static long remaining; private static long remainingTimeSeconds; private static long remainingTimeMilliSeconds; private static boolean timeUp; private static Timer timer; public static void start() { startTime = System.currentTimeMillis(); timeUp = false; timer = new Timer(); timer.schedule(new TimerTask() { public void run() { timeUp = true; } }, 60 * 1000); } public static String remain() { remaining=(60 * 1000) - (System.currentTimeMillis() - startTime); remainingTimeSeconds = (remaining)/ 1000; remainingTimeMilliSeconds= (remaining)%1000; if (remainingTimeMilliSeconds<0) {remainingTimeMilliSeconds=0;} String ch=String.valueOf(remainingTimeSeconds)+"."+String.valueOf(remainingTimeMilliSeconds); return ch; } public static boolean isFinished() { return timeUp; } public static void stop() { timeUp=true; } public static void showAlertEnglish() { Alert alert = new Alert(AlertType.WARNING); alert.setTitle("Time's Up!"); alert.setHeaderText(null); alert.setContentText("You ran out of time. You lose!"); alert.showAndWait(); } public static void showAlertFrench() { Alert alert = new Alert(AlertType.WARNING); alert.setTitle("Temps écoulé !"); alert.setHeaderText(null); alert.setContentText("Vous avez dépassé le temps imparti. Vous avez perdu !"); alert.showAndWait(); } } ``` </code>
Controller class:
```
//libraries import

public class Controller {

    //other FXML elements
    @FXML
    private Text time;
    @FXML
    private Text time1;

    //variables used defined;
    
    public void initialize() {
        language1.setText("Please select a language 1.English 2.French ");
        english.setVisible(true);
        french.setVisible(true);

        //all others elements are invisible
        
    }

    public void onClick(ActionEvent event){    
        String letter = ((Button)event.getSource()).getText().toLowerCase();        
        ((Button) event.getSource()).setDisable(true);
        
        time1.setText("");
        String str1 = timer.remain();
        time.setText("The remining time is "+str1+" seconds");}
      
        if(myLetters.contains(letter)){
            int letterIndex = myLetters.indexOf(letter);
            while (letterIndex != -1) {
                correct++;
                myLetters.set(letterIndex, "*");
                answer.set(letterIndex*2, letter);
                letterIndex = myLetters.indexOf(letter);
                String res = String.join("", answer);
                text.setText(res);
            }
            
            if(correct==word.length()){
                winStatus.setText("You Win!");}
                buttons.setDisable(true);
                timer.stop();
            }
        }
        else{
            mistakes++;
            if(timer.isFinished()){
                timer.stop();
                timer.showAlertEnglish();
                buttons.setDisable(true);
            }
         
            if(mistakes ==1)       base1.setVisible(true);
            // show other parts of hangman 
            else if (mistakes ==8){
                man.setVisible(true);
                winStatus.setText("You Lose!");
                realWord.setText("The actual word was " + word);
                buttons.setDisable(true);
                timer.stop();
            }
        }
    }
    
    public void select(ActionEvent event) {
        //chnages the game due to the selected language
   
   
        time1.setText("Vous avez 1 minute. Allez !");
        timer.start();
    }
    
    
    public void newGame(){
        time.setText("");
        for(int i=0; i<35; i++){
            buttons.getChildren().get(i).setDisable(false);
        }
        newGameButton.setText("");
        initialize();
    }
    
}```

Timer class:
``` mport java.util.Timer;
import java.util.TimerTask;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;

public class timer {
    
    private static long startTime;
    private static long remaining;
    private static long remainingTimeSeconds;
    private static long remainingTimeMilliSeconds;
    private static boolean timeUp;
    private static Timer timer;
    
    public static void start() {
        startTime = System.currentTimeMillis();
        timeUp = false;
        timer = new Timer();
        timer.schedule(new TimerTask() {
         public void run() {
                      timeUp = true;
                  }
         }, 60 * 1000);
        }
    
    public static String remain() {
        remaining=(60 * 1000) - (System.currentTimeMillis() - startTime);
        remainingTimeSeconds = (remaining)/ 1000;
        remainingTimeMilliSeconds= (remaining)%1000;
        if (remainingTimeMilliSeconds<0) {remainingTimeMilliSeconds=0;}
        String ch=String.valueOf(remainingTimeSeconds)+"."+String.valueOf(remainingTimeMilliSeconds);
        return ch;
    }    
        

    public static boolean isFinished() {
         return timeUp;
    }
    
    public static void stop() {
        timeUp=true;
    }
    

    public static void showAlertEnglish() {
        Alert alert = new Alert(AlertType.WARNING);
        alert.setTitle("Time's Up!");
        alert.setHeaderText(null);
        alert.setContentText("You ran out of time. You lose!");
        alert.showAndWait();
    }
    
    public static void showAlertFrench() {
        Alert alert = new Alert(AlertType.WARNING);
        alert.setTitle("Temps écoulé !");
        alert.setHeaderText(null);
        alert.setContentText("Vous avez dépassé le temps imparti. Vous avez perdu !");
        alert.showAndWait();
    }
}
 ```

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật