0

I'm making an applet which has two buttons: step back and step forward. I have the same timer in every button to execute an animation. If I push step forward it works fine, and the animation runs, but if i push step back, the animation doesn't run or is bad runned (in a wrong position and velocity). I guess the problem is the timer doesn't stop correctly and is running when start the timer again, but I don´t know how to solve it.

This is the code of the step forward button:

//Code of the button "Paso a Paso"
        this.botonPasoAPaso = new JButton("Paso a paso");
            this.botonPasoAPaso.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

            //Avoid timer to accelerate 
            if (timer != null && timer.isRunning()) {
                timer.stop();
            }                           
            //Code of the timer, makes an animation 
            timer = new Timer(35, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (pasoAPaso <= listaPaquetes.size()) {
                        Paquete p = listaPaquetes.get(pasoAPaso);
                        p.animar();
                        panelGrafo.removeAll();
                        panelGrafo.updateUI();
                        panelGrafo.setPaquete(p);
                        panelGrafo.setAnimar(true);
                        panelGrafo.repaint();                           
                    }                       
                }
            });

            timer.start();  

            pasoAPaso++;                
        }           
    });

This is the code of the step back button:

    this.botonAtras.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent arg0) {
            if (timer != null && timer.isRunning()) {
                timer.stop();
            }


            timer = new Timer(35, new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    if (pasoAPaso < listaPaquetes.size() && pasoAPaso>=0) {

                        Paquete p = listaPaquetes.get(pasoAPaso);
                        p.animar();
                        panelGrafo.removeAll();
                        panelGrafo.updateUI();
                        panelGrafo.setPaquete(p);
                        panelGrafo.setPasoAPaso(pasoAPaso);
                        panelGrafo.setAnimar(true);
                        panelGrafo.repaint();
                        //Actualizar matriz del render para pintar celdas
                        RenderTabla.matrizTotal = new Cuadrado[pasos.get(pasoAPaso).length][pasos.get(pasoAPaso)[0].length];
                        RenderTabla.matrizTotal = pasos.get(pasoAPaso);
                        tabla.introducirDatos(pasos.get(pasoAPaso),false);                          
                    }                       
                }
            });

            timer.start();                  

            pasoAPaso--;            

        }                   
    });

This is a video of the applet to see what's happening If you see the video, when I click the "Paso a Paso" (Step forward) button, a truck is animated, but when I click the "rewind" (Step back) button, it doesn't work (appears a truck but is not correctly animated), and if i click again nothing happens.

Thanks.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
carlbron
  • 3
  • 2
  • 1
    There's too many questions within your code snippets to be able to formulate any kind of idea of what might be going wrong. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Apr 06 '16 at 12:14
  • *"I'm making an applet.."* 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). 3) Does it use AWT or Swing components? – Andrew Thompson Apr 08 '16 at 02:17
  • `panelGrafo.removeAll(); ..` Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Apr 08 '16 at 02:19

0 Answers0