Solved

Timer function prob

Posted on 2003-11-01
8
259 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
 
LVL 15

Expert Comment

by:jimmack
ID: 9662436
Working the weekend Tim?

;-)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven java path setting 5 50
Problem to start Neon 20 54
eclipse formatting 6 43
JList custom Cell Renderer refresh 15 43
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This video teaches viewers about errors in exception handling.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now