[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Timer function prob

Posted on 2003-11-01
8
Medium Priority
?
269 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 160 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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