Solved

Timer function prob

Posted on 2003-11-01
8
260 Views
Last Modified: 2010-03-31
hi..
I am using Timer in my program to continuosly draw some circles after a small delay....but even if i give delay parameter as 1 millisecond..i draws circle after 10 ms...and bcoz of that whole proc works slow..is there any better way to do this or what is prob with Timer??

0
Comment
Question by:sushpb4u
8 Comments
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9662362
The Timer is not accurate enough to distinguish between resolutions of 1ms (I think it has more to do with the OS than the java Timer function though).  Anyways, you cannot rely on a Timer or Sleep function to accurately distinguish resolutions that small.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9662408
and if you could, the Timer would lock the system :-(

Try storing the System.currentTimeMillis() value at the end of the timer function, then drawing that number of circles next time the Timer fires...  bit of a hack, but you will get one circle per ms ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9662410
ie:

draw ( System.currentTimeMillis() - oldTime ) circles
then set oldTime = System.currentTimeMillis() ;
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 15

Expert Comment

by:jimmack
ID: 9662436
Working the weekend Tim?

;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9662442
hehehe, no...just got up and thought I'd have a look before I go and buy a dvd player and some fireworks :-)

what a combination ;-)
0
 

Author Comment

by:sushpb4u
ID: 9669401
sorry for late reply..but TimYates i didnt get ur solution...
see tihs is my code in Timer funciotn

speed=1;

myTimer=new Timer(speed,this) ;
      myTimer.start();

///in actionPerformed()

if (e.getSource() == myTimer)
   {
drawCircle(0);
if(i%2==0 )
 drawCircle(1);
if(i%4==0 )
drawCircle(2);
if(i%7==0 )
drawCircle(3);
i++;
  }
}

by tihs i want to draw 4 circles pixel by pixel with different speeds...and i want to draw first circle with speed 1ms...like when 1st circle completes 2 circles...2nd circle circle shud complete one round..like this...
but all this shud happen with speed given by 'speed' variable..so it is possible with System.currentTimeMillis()???
plz reply..
0
 
LVL 35

Accepted Solution

by:
TimYates earned 40 total points
ID: 9669705
long oldTime = -1 ;

speed = 1 ;

myTimer=new Timer(speed,this) ;
      myTimer.start();

///in actionPerformed()

if (e.getSource() == myTimer)
{
  if( oldTime != -1 )
  {
    for( int cnt = oldTime ; cnt < System.currentTimeMillis() ; cnt++ )
    {
      drawCircle(0);
      if(i%2==0 )
        drawCircle(1);
      if(i%4==0 )
        drawCircle(2);
      if(i%7==0 )
        drawCircle(3);
      i++;
    }
  }
  oldTime = System.currentTimeMillis() ;
}

Would draw the correct number of circles...

If it took ages to get back to the function, it would draw lots...

You may want to rethink your idea though....one circle per ms is just a bit too quick... :-(
0
 

Author Comment

by:sushpb4u
ID: 9699496
hi tim Yates....yes..its really tooo fast ...but i will do sometihing for that..and thanx a lotttttttt:)

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question