pritesh soni
pritesh soni

Reputation: 11

How do I stop timer task after executed one-time

I have 2 problems with this code 1)I see the error "local variables referenced from an inner class must be final or effectively final" in the codes inside the while statement. 2)although I have used timer.cancel() I can not stop the timer. it keeps working. in clear words, whenever I create another object(oval), it goes to the previous end points.

private void JButton3ActionPerformed(java.awt.event.ActionEvent evt) 
    {                                         
        Timer timer = new Timer(); 

        Graphics g = JPanel1.getGraphics();
        int x1 = P.objectXCoordinate + P.OvalWidth/2;
        int y1 = P.objectYCoordinate + P.OvalHeight/2;
        int x2 = P.endPointX;
        int y2 = P.endPointY;
        int dx = Math.abs(x2 - x1);
        int dy = Math.abs(y2 - y1);
        int xn = x1;
        int yn = y1;
        //Special step variables
        int xs;
        int ys;
        int pn;
        // Dicreasing xn/yn solution
        if (x2 < x1){
            xs = -1;
        }
        else{
            xs = 1;
        }
        if (y2 < y1){
            ys = -1;
        }
        else{
            ys = 1;
        }
        //Bresenham algorithm (main)
        //dx > dy
        DrawObject();
        if (dx >= dy)
        {
            pn = 2*dy - dx;
             TimerTask task = new TimerTask()
    {
        public void run()
        {
            while(xn != x2)
            {
                xn = xn + xs;
                if (pn>0)
                {
                    yn = yn + ys;
                    pn = pn + 2*dy-2*dx;
                }
                else
                {
                    pn = pn + 2*dy;
                }
                g.setColor(Color.black);
                P.objectXCoordinate = xn;
                P.objectYCoordinate = yn;
                DrawPanel();
                DrawObject();
//            g.drawLine(xn, yn, xn, yn);
            }
        }
    };
             timer.scheduleAtFixedRate(task, 0, 5000);
             task.cancel();
             task=null;

        }    
        //dy > dx
        else if (dy >=dx)
        {
            pn = 2*dx - dy;
            TimerTask task = new TimerTask()
         {
                   public void run()
           {
            while(yn != y2)
                {
                yn = yn + ys;
                if (pn>0)
                {
                    xn = xn + xs;
                    pn = pn + 2*dx-2*dy;
                }
                else
                {
                    pn = pn + 2*dx;
                }
                g.setColor(Color.black);
                P.objectXCoordinate = xn;
                P.objectYCoordinate = yn;
                DrawPanel();
                DrawObject();
//            g.drawLine(xn, yn, xn, yn);

        }

                }
         };
            timer.scheduleAtFixedRate(task, 0, 5000);
           task.cancel();
           task=null;
        }


            }

Upvotes: 1

Views: 499

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347214

Swing Timer is NOT a TimerTask, instead, you need to simply stop the Timer ... having said that, there are so many things about your code which just terrify me it's not funny.

First, take a look at How to Use Swing Timers and the JavaDocs for Swing Timer. The method you are looking for is stop. To call it, you will need a reference to the Timer itself.

One way to do this, is to extract the Timer reference from the ActionEvent of the ActionListener registered to the Timer

Timer timer = (Timer)evt.getSource();

Since your in Swing, you need to stop using TimerTask, Swing is NOT thread safe and you should not try and update the UI from out side the context of the Event Dispatching Thread - Take a look at Concurrency in Swing for more details.

Graphics g = JPanel1.getGraphics();

This is both dangerous and ill advised. getGraphics can return null and at best is just a snapshot of the last paint cycle. Anything your attempt to paint to it will be erased on the next paint cycle.

Stop, go and read Performing Custom Painting and Painting in AWT and Swing to get a better understanding of how painting in Swing works AND how you should work with it.

You seem to simply plugging as much code into your problem in some vain attempt to solve it, rather the understanding the APIs available to you and how you should actually use them. You might need to break your problem down into small chunks which you can play around with and attempt to solve in isolation, and once you understand how they work, bring them together into a larger solution

Upvotes: 1

Related Questions