?
Solved

Timer function prob

Posted on 2003-11-01
8
Medium Priority
?
265 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 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