-2

I'm some new in Java and I don't know how I can subtract a value every second in a class, in this case NivelCronometrado, I tried to use a Timer of swing and java.util and I failed.

What I need is in a JPanel show a countdown timer, NivelCronometrado has the time to start and a JPanel contains this class.

I create in the JPanel a JLabel to show this time.

Here is the code of NivelCronometrado

import clases.logicas.elementos.Puntaje;
import java.util.ArrayList;

public class NivelCronometrado extends Nivel
{
    private int tiempo;

    public NivelCronometrado(int argTiempo, int argId, int[][] argObstaculos,
                         int[] argPuntosEstrella, Long argRandomSeed, ArrayList<Puntaje> argPuntajes)
    {
        super(argId, argObstaculos, argPuntosEstrella, argRandomSeed, argPuntajes);
        this.tiempo = argTiempo;
    }

    public void disminuirTiempo()
    {
        this.tiempo--;
    }

    public int getTiempo()
    {
    return this.tiempo;
    }

}

And here an extract of the code of the JPanel where I try to modify a JLabel to show the time:

public void establecerNivel()
{
    this.setTextTitulo("Nivel " + this.nivel.getId());

    this.setTextPuntosValor("0");

    if (this.nivel instanceof NivelRestringido) {
        this.setTextEspecialTexto("Movimientos:");
        NivelRestringido nivelRestringido = (NivelRestringido)this.nivel;
        this.setTextEspecialValor(Integer.toString(nivelRestringido.getIntentos()));
    }
    else if (this.nivel instanceof NivelCronometrado) {
        this.setTextEspecialTexto("Tiempo:");
        NivelCronometrado nivelCronometrado = (NivelCronometrado)this.nivel;

        /*Here subtract time to nivelCronometrado every second*/

        this.setTextEspecialValor(Integer.toString(nivelCronometrado.getTiempo()));     
    }

    this.setTextEstrellaValor1(Integer.toString(this.nivel.getPuntosEstrella()[0]));
    this.setTextEstrellaValor2(Integer.toString(this.nivel.getPuntosEstrella()[1]));
    this.setTextEstrellaValor3(Integer.toString(this.nivel.getPuntosEstrella()[2]));

    this.repaint();
}

Edit: Here is the code of one of my attemps

else if (this.nivel instanceof NivelCronometrado) {
        this.setTextEspecialTexto("Tiempo:");
        NivelCronometrado nivelCronometrado = (NivelCronometrado)this.nivel;

        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                nivelCronometrado.disminuirTiempo();
            }
        };

        new Timer(1000, taskPerformer).start();

        this.setTextEspecialValor(Integer.toString(nivelCronometrado.getTiempo()));     
    }
Daniel
  • 286
  • 1
  • 2
  • 9
  • *"I tried to use a Timer of swing and java.util and I failed."* Where is your attempt(1) to implement it, and how exactly did you fail? 1) And by 'attempt' I mean an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example) as opposed to uncompilable, out of context code snippets.. – Andrew Thompson Jul 19 '15 at 16:52
  • I did a edit and now it is added to the post. – Daniel Jul 19 '15 at 18:17
  • You apparently either did not read, or did not understand, what an MCVE is.. – Andrew Thompson Jul 19 '15 at 18:40

1 Answers1

-1

Possible Duplicate

Use javax.swing.Timer to make countdown timer in Java

Below is the code snippet which you can invoke in your JPanel.

Timer timer = new Timer(1000, new TimerListener());

class TimerListener implements ActionListener {
   int elapsedSeconds = 30;

   public void actionPerformed(ActionEvent evt){
       elapsedSeconds--;
       timerLabel.setText(elapsedSeconds)
       if(elapsedSeconds <= 0){
           timer.stop();
           wrong()
           // fill'er up here...
       }
   }
}
Community
  • 1
  • 1
Chavakri
  • 32
  • 2
  • 2
    An answer is not the place to announce a possible duplicate. New users cannot post comments for a reason. Wait until you get some more rep. so you *can* make comments. – Andrew Thompson Jul 19 '15 at 16:57