Skip to content
Snippets Groups Projects
Select Git revision
  • b9a3bc7b7ba351a5a54c45d945be169ee8367ce1
  • master default
  • v1.0.0
3 results

CountdownTimer.java

Blame
  • CountdownTimer.java 1.20 KiB
    package de.bricked.utils;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    import de.bricked.ui.LevelController;
    import javafx.application.Platform;
    import javafx.scene.control.Label;
    import javafx.scene.layout.HBox;
    
    public class CountdownTimer
    {
    	private int count;	
    	private HBox hbox;	
    	private Timer timer;
    
    	public CountdownTimer(int seconds, HBox hbox, LevelController levelController)
    	{
    		this.count = seconds;
    		this.hbox = hbox;
    		
    		CountdownTimer self = this;
    		
    		timer = new Timer();
    		TimerTask task = new TimerTask()
    		{
    			@Override
    			public void run()
    			{
    				Platform.runLater(()->{
    					try
    					{
    						Label labelSeconds = (Label)hbox.getChildren().get(1);
    						labelSeconds.setText(String.valueOf(count));
    					}
    					catch(Exception e)
    					{
    						
    					}
    				});			
    				if(count > 0)
    				{
    					count--;
    				}
    
    				if(count == 0)
    				{
    					Platform.runLater(()->{
    						levelController.deactivatePowerUp(self, hbox);
    					});
    					timer.cancel();
    				}				
    			}
    		};
    		timer.schedule(task, 0, 1000);
    	}
    	
    	public void addSecondsToTimer(int seconds)
    	{
    		this.count += seconds;
    	}	
    	
    	public HBox getHBox()
    	{
    		return hbox;
    	}	
    	
    	public void stop()
    	{
    		timer.cancel();
    		timer.purge();
    	}
    }