Solved

Timer function prob

Posted on 2003-11-01
8
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Suggested Courses

738 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