How to use the TIMER class in a method with Math.max and Math.min

Asked

Viewed 522 times

2

I’m on a kind of ambitious project to create my little math game (which I found better than a simple calculator) in just one JFrame, methods only, without using any other class in the package...

With the help of Swing and the GUI builder netbeans

One day, I heard about the class Timer, however, I find nothing about using it within a method, only within another class without the main

I tried to use it in a method in a question I asked a while ago, unfortunately I ended up losing the code (because it didn’t work)

Could someone give me an example of what would be the use of this class in a method? (preferably on a decreasing timer)

  • An example of the Timer class application? It would be interesting to edit, citing a more specific situation for the use, so that the question is not wide, since there may be countless examples using this class.

  • 1

    OK @Diegof, edited with a few words

1 answer

1


You can use TimerTask and Timerof java.util Follow an example:

import java.util.TimerTask;

public class ExemploTimer  extends TimerTask {

    private int numeroExecucao = 0;

    @Override
    public void run() {
        numeroExecucao++;
        System.out.println("Exemplo Timer executando: " + numeroExecucao);
        //Aqui você executa as suas ações da thead.
    }

}

RUNNING HIS CLASS

public class ExemploMain {
    public static void main(String args[]){
        //Em qualquer lugar você pode utilizar a sua classe.

        ExemploTimer exemploTimer = new ExemploTimer();


        Timer timer = new Timer(true);
        //data atual + 10 minutos.
        Date dataPrimeiraExecucao = new Date();
        //Tempo para execução, executar a cada 10 segundos
        long tempoParaProximaExecucao = 1000 * 10; 
        // Shedule, acredito que isto é o que você precisa
        timer.schedule(exemploTimer, dataPrimeiraExecucao, tempoParaProximaExecucao);

        //Este código serve apenas para esperar a linha acima executar.
        //Deve ser eliminado no seu código.
        try {
            Thread.sleep(1000 * 100);
        } catch (InterruptedException ex) {
            Logger.getLogger(ExemploMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

To complement follow some examples and tutorials:

Timer

Timer Task

  • And that’s not the problem with swing, since this one has its own thread? Perhaps it would be interesting to use the Timer from the swing api to GUI.

  • @Diegof has the Swingworker which can be used to avoid locking the components Swing, but this example I put up creates a task that must be executed in a period of time, I use with Swing also and does not lock the components.

  • @Elyelrubensdarosa I’ll try, but it would be better if I could do something on Java without having to create a billion classes within trillions of packages, why use only the class that contains the method main gets a lot easier ;-;

  • @Fabriciocardoso This may be your vision at the moment, but believe me, you can organize your project very well with packages and classes so that it is very easy for you to work, the ideal is that the main method is as lean as possible, calling the main functions of your game. When you get halfway through it may be too late to consider these things. Anyway, if you want to do everything in one place, I recommend using the example I passed and write all your logic inside the run method.

  • @Elyelrubensdarosa Ok... I give up, I came from C pro JAVA, dai, still I prefer to do everything in 1 place, but I will try to separate my methods in other classes to my point of view, it’s still much better, with time I learn hehehe

  • @Fabriciocardoso It is complicated to start a change of habits, but when accustomed you will see many advantages. If you have questions about how to structure the beginning, put your problem there that we can help you.

  • @Elyelrubensdarosa Really... but, it’s not so difficult to do something only with a class that contains the main, Well, so far, I’ve needed almost nothing inside the main, methods only, since I am using the GUI Builder from Netbeans to swing, so it makes it easier to try something cool. But I’ll try to get used to this new style of having a zillion packets with different classes for just one application. Any questions beyond, I put the code here and ask for help again ^^

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.