Results 1 to 5 of 5
  1. #1
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default timer not working

    im trying to use timer to make an animation, for some reason it only plays once...or not at all.
    here is code, before u ask fram is my window adapter.

    import java.awt.*;
    import java.util.*;
    public class CFrame extends Fram
    {
    Image image1,image2;
    Button b = new Button("this");
    Timer timer = new Timer();
    int xloc=500;
    CFrame()
    {
    super("CFRAME");
    setSize(1000,1000);
    setLayout(null);
    add(b);
    b.setBounds(50,50,100,100);
    setVisible(true);
    repaint();
    for(int x=0;x<25;++x)
    {
    start();
    }
    }
    public void paint(Graphics g)
    {
    Toolkit tool = Toolkit.getDefaultToolkit();
    image1 = tool.getImage("E:\\cover.png");
    g.drawImage(image1,30,85,this);
    g.fillOval(xloc,100,50,50);
    }
    public void start() {
    timer.schedule(new TimerTask()
    {
    public void run()
    {
    xloc-=5;
    //System.out.println("hey");
    //timer.cancel();
    }
    },500);

    repaint();
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: timer not working

    A moderator added the code tags for you in your last thread. Now take some initiative and learn how to do that yourself.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: timer not working

    You might need to check the javadoc for what this method timer.schedule(TimerTask task, long delay) is created for. So you'll know why it was executed only once. You can check for the other schedule method to find what you need in your code.

  4. #4
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: timer not working

    i tried adding an additional 500 to the scheduel method, it still does nothing.

    here is revised code:

    import java.awt.*;
    import java.util.*;
    public class CFrame extends Fram
    {
    Image image1,image2;
    Button b = new Button("this");
    Timer timer = new Timer();
    int xloc=500;
    CFrame()
    {
    super("CFRAME");
    setSize(1000,1000);
    setLayout(null);
    add(b);
    b.setBounds(50,50,100,100);
    setVisible(true);
    repaint();
    start();
    }
    public void paint(Graphics g)
    {
    Toolkit tool = Toolkit.getDefaultToolkit();
    image1 = tool.getImage("E:\\cover.png");
    g.drawImage(image1,30,85,this);
    g.fillOval(xloc,100,50,50);
    }
    public void start() {
    timer.schedule(new TimerTask()
    {
    public void run()
    {
    xloc-=5;
    //System.out.println("hey");
    //timer.cancel();
    }
    },500,500);

    repaint();
    }
    }

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: timer not working

    If you look carefully to this method you'll see why it doesn't move.

    Java Code:
    public void start() {
            timer.schedule(new TimerTask() {
                public void run() {
                    xloc -= 5;
                }
            }, 500, 500);
    
            repaint();
        }
    The repaint() method should be inside the TimerTask.run() so that it will be executed repeatedly.

Similar Threads

  1. Timer And Timer Task
    By Ashish_jain in forum New To Java
    Replies: 3
    Last Post: 06-22-2011, 09:41 AM
  2. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  3. Timer stops working :P
    By Addez in forum New To Java
    Replies: 13
    Last Post: 09-21-2010, 08:36 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •