Results 1 to 2 of 2

Thread: Timer Problem

  1. #1
    ellias2007 is offline Member
    Join Date
    Feb 2010
    Posts
    25
    Rep Power
    0

    Default Timer Problem

    Hi every one,
    in order to make a method to make a nice transition to any swing item like JPanel or JButton or others, i made the following method :




    public static void MoveTo(final Container Cnt,final int fX,final int fY,final int Slow) {

    MyActionListener = new ActionListener()
    {
    int initialX=Cnt.getX();
    int initialY=Cnt.getY();
    int DeltaX=fX-initialX;
    int DeltaY=fY-initialY;
    public void actionPerformed(ActionEvent actionEvent) {
    Cnt.setLocation(initialX + (int)(DeltaX*Math.sin(Counter*Math.PI/(2*Slow))) , initialY + (int)(DeltaY*Math.sin(Counter*Math.PI/(2*Slow))) );
    Counter++; if (Counter>=Slow) { //Cnt.setLocation(fX,fY);
    Counter=0;
    MyTimer.stop();
    }
    }
    };
    MyTimer = new javax.swing.Timer(0, MyActionListener); //0 milliSeconds as Delay ....
    MyTimer.start();
    }



    But when i use it i face a Crazy problem :
    if i call it and wait it to complete its motion : No Problem...
    BUT if the user calls it and BEFORE ending its motion the user again calls it, a CRAZY motion occurs....
    i have an idea of the problem reason : i call it again then the Conter and may be Mytimer and others are double-used....
    But i don't have an idea about any SOLUTION ....
    Someone has a solution or other idea ...
    Many thanks
    Last edited by ellias2007; 10-20-2011 at 11:25 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Timer Problem

    You've posted 15 times -- please use code tags so we can read your code.

    I suggest that you create and post a small compilable and runnable program that shows your problem, again with code tags. Also, don't use a timer delay of 0 as that's unrealistic. Experiment with different values, but not 0.

Similar Threads

  1. Replies: 10
    Last Post: 04-03-2011, 12:06 AM
  2. Replies: 15
    Last Post: 11-15-2010, 09:29 PM
  3. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  4. Problem with timer in java
    By paul in forum Advanced Java
    Replies: 3
    Last Post: 07-26-2007, 11:18 AM
  5. problem with timer
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 06:13 AM

Posting Permissions

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