Animation thread not using repaint help

hi
I have made a simple applet moving a circle across the screen.
But the animation is jerky,I use buffering but the circle still
gets sudden bigger moves though it should only move 1 pixel at each repaint.
Could this be fixed with making a new thread that updates the position of the circle instead of updating it in paint() ?
And why is it normal to use sleep() in the animation thread, isnt it possible to just paint as fast as possible? instead of using sleep(25),why not just tell the thread to wait for the "calculation" thread to be finished,and then continue? isn't that what the sleep() is for?waiting for the other stuff to be done so the rendering doesnt take all the processor time? So it would be better to just tell the animation thread to wait until the other thread is finished. This would make the animation be as fast as possible..and faster on a fast machine.
Any ideas?
Here is the source code to my applet test. If you want to comment then please do.
import java.awt.*;
import java.applet.Applet;
public class Class1 extends Applet implements Runnable
private Thread anim;
private Thread calc;
private boolean running = false;
private boolean running2 = false;
int x = 0;
int y = 40;
Dimension minBufferDimension;
Image minBufferImage;
Graphics minBufferGraphics;
public void init()
minBufferDimension = getSize();
minBufferImage = createImage( 300, 300 );
minBufferGraphics = minBufferImage.getGraphics();
public void run()
Thread currentThread = Thread.currentThread();
while(currentThread == anim)
repaint();
try
anim.sleep(15);
catch (InterruptedException e){}
while(currentThread == calc)
x++;
try
calc.sleep(15);
catch (InterruptedException e) {}
public synchronized void start()
if(anim == null || !anim.isAlive())
anim = new Thread(this);
anim.setPriority(Thread.MIN_PRIORITY + 1);
if(calc == null || !calc.isAlive())
calc = new Thread(this);
calc.setPriority(Thread.MIN_PRIORITY + 1);
calc.start();
anim.start();
public synchronized void stop()
running = false;
running2 = false;
public void update( Graphics g )
paint(g);
public void paint(Graphics g)
minBufferGraphics.setColor( getBackground() );
minBufferGraphics.fillRect(     0 , 0 , 300, 300 );
minBufferGraphics.setColor( Color.black );
minBufferGraphics.drawOval(x,y,40,40);
g.drawImage( minBufferImage, 0, 0, this );
}

so many threads for so little work.
import java.awt.*;
import java.applet.Applet;
public class Class1 extends Applet implements Runnable{
     private Thread thread;
     private boolean running = true;
     int x = 1;
     int y = 40;
     int direction=1;
     int changeDirection=-1;
     Image minBufferImage;
     Graphics minBufferGraphics;
     public void init(){
     public void run(){
          while(running){
               repaint();
               move();
               try{
               thread.sleep(15);
               }catch (InterruptedException e){}
     public synchronized void start(){
          thread = new Thread(this);
          thread.start();
     public synchronized void stop(){
          running = false;
     public void move(){
          if(x>getSize().width-40 || x<0){
               direction=direction*changeDirection;
          x=x+direction;
     public void paint(Graphics g){
          g.setColor(Color.black);
          g.drawOval(x,y,40,40);
     public void update(Graphics g) {
          if(minBufferImage==null){
               minBufferImage=createImage(getSize().width,getSize().height);
          minBufferGraphics=(minBufferImage.getGraphics());
          minBufferGraphics.setColor(getBackground());
          minBufferGraphics.fillRect(0,0,getSize().width,getSize().height);
          paint(minBufferGraphics);
          g.drawImage(minBufferImage,0,0,this);
}the sleep method is used to slow down the animation, and to allow time for garbage collection. it doesn't need to be as high as it is, but a least a few millisecs is recommended.

Similar Messages

  • Firefox crashes every time I open it and try to go between pages, I can not use firefox, help

    I converted my new computer from an old one about a week ago.
    Every time I open Firefox, it either crashes immediately or once I try to go between pages it crashes. I get a window that says Firefox quite Unexpectedly. Apple Care worked on this with me.
    We opened Firefox in Test Mode, the crashes did not happen.
    I would like to use Firefox, it is much better than Safari, but not being able to get help for Firefox; I have no choice.
    I don't know how to submit information to Mozilla, therefore I have no crash ID.

    With Test mode, do you mean starting Firefox in [[Safe mode]] ?
    If it does work in Safe-mode then disable all extensions and then try to find which is causing it by enabling one at a time until the problem reappears.
    * Use "Disable all add-ons" on the [[Safe mode]] start window to disable all extensions.
    * Close and restart Firefox after each change via "File > Exit" (Mac: "Firefox > Quit"; Linux: "File > Quit")

  • When I type localhost in my urlbar firefox appends /xampp to it and gives a 404 I do not use xampp help please

    I use Apache2.2 with PHP not xampp, however firefox appends the /xampp to the urlbar when I type localhost, I can type the exact address in the urlbar and it works but this really bothers me.
    I used xampp at one time but found I did not like it, so I uninstalled it and reinstalled Apache and php from their respective zip files.
    This just started happening

    Hmmm, I still think it's most likely a redirect from the server. Is there an index.php that could contain a Location header?
    Can you use the Firebug add-on (Net tab), or an external proxy like Fiddler2, to view the request and response?
    * [https://addons.mozilla.org/en-US/firefox/addon/firebug/ Firebug]
    * [http://www.fiddler2.com/fiddler2/ Fiddler Web Debugger]

  • My iPhone 5s was bought with full price in the USA from Sprint, and I was told that it is a global phone. And I can use it in china. But I can not use it in the USA, so could you mind help me to solve that problem.  I think someone of you must help me to

    My iPhone 5s was bought with full price in the USA from Sprint, and I was told that it is a global phone. And I can use it in china.
    But I can not use it in the USA, so could you mind help me to solve that problem.
    I think someone of you must help me to solve this problem !
    Details:
    SN:F2*****FDR
    <Edited by Host>

    See the response in your other thread:
    https://discussions.apple.com/message/24641427#24641427

  • In Siri, I can call by my voice, but why I can not use Siri voice call in my country (Laos), just can call only us phone number; my country we use like 3 number for option call, 8 numbers for call friend but Siri cannot use this please help us, thanks

    In Siri, I can call by my voice, but why I can not use Siri voice call in my country (Laos), just can call only us phone number; my country we use like 3 number for option call, 8 numbers for call friend but Siri cannot use this please help us, thanks
    And please help me can type Laos font in it like andrio phone.

    Hi Cozumel,
    Thanks for posting. I'm sorry you're having problems with your bills. I can take a look at this for you. Drop me an email with your account details and a link to this thread for reference. You'll find the address in my profile.
    Cheers
    David
    BTCare Community Mod
    If we have asked you to email us with your details, please make sure you are logged in to the forum, otherwise you will not be able to see our ‘Contact Us’ link within our profiles.
    We are sorry but we are unable to deal with service/account queries via the private message(PM) function so please don't PM your account info, we need to deal with this via our email account :-)

  • TS1398 I have an iPad 2 that is updated and using 6.0.1. It is connecting to my wifi just fine. I just purchased a 4th generation iPad. It shows it is connected to the same wifi, same strong signal, but it will not work. HELP!!!

    I have an iPad 2 that is updated and using 6.0.1. It is connecting to my wifi just fine. I just purchased a 4th generation iPad. It shows it is connected to the same wifi, same strong signal, but it will not work. HELP!!!

    Some things to try first:
    1. Turn Off your iPad. Then turn Off (disconnect power cord for 30 seconds or longer) the wireless router & then back On. Now boot your iPad. Hopefully it will see the WiFi.
    2. Go to Settings>Wi-Fi and turn Off. Then while at Settings>Wi-Fi, turn back On and chose a Network.
    3. Change the channel on your wireless router (Auto or Channel 6 is best). Instructions at http://macintoshhowto.com/advanced/how-to-get-a-good-range-on-your-wireless-netw ork.html
    4. Go into your router security settings and change from WEP to WPA with AES.
    5.  Renew IP Address: (especially if you are droping internet connection)
        •    Launch Settings app
        •    Tap on Wi-Fi
        •    Tap on the blue arrow of the Wi-Fi network that you connect to from the list
        •    In the window that opens, tap on the Renew Lease button
    ~~~~~~~~~~~~~~~~~~~~~~~~~
    iOS 6 Wifi Problems/Fixes
    Fix For iOS 6 WiFi Problems?
    http://tabletcrunch.com/2012/09/27/fix-ios-6-wifi-problems/
    Did iOS 6 Screw Your Wi-Fi? Here’s How to Fix It
    http://gizmodo.com/5944761/does-ios-6-have-a-wi+fi-bug
    How To Fix Wi-Fi Connectivity Issue After Upgrading To iOS 6
    http://www.iphonehacks.com/2012/09/fix-wi-fi-connectivity-issue-after-upgrading- to-ios-6.html
    iOS 6 iPad 3 wi-fi "connection fix" for netgear router
    http://www.youtube.com/watch?v=XsWS4ha-dn0
    Apple's iOS 6 Wi-Fi problems
    http://www.zdnet.com/apples-ios-6-wi-fi-problems-linger-on-7000004799/
    ~~~~~~~~~~~~~~~~~~~~~~~
    How to Fix a Poor Wi-Fi Signal on Your iPad
    http://ipad.about.com/od/iPad_Troubleshooting/a/How-To-Fix-A-Poor-Wi-Fi-Signal-O n-Your-iPad.htm
    iOS Troubleshooting Wi-Fi networks and connections  http://support.apple.com/kb/TS1398
    iPad: Issues connecting to Wi-Fi networks  http://support.apple.com/kb/ts3304
    WiFi Connecting/Troubleshooting http://www.apple.com/support/ipad/wifi/
    How to Fix: My iPad Won't Connect to WiFi
    http://ipad.about.com/od/iPad_Troubleshooting/ss/How-To-Fix-My-Ipad-Wont-Connect -To-Wi-Fi.htm
    iOS: Connecting to the Internet http://support.apple.com/kb/HT1695
    iOS: Recommended settings for Wi-Fi routers and access points  http://support.apple.com/kb/HT4199
    How to Quickly Fix iPad 3 Wi-Fi Reception Problems
    http://osxdaily.com/2012/03/21/fix-new-ipad-3-wi-fi-reception-problems/
    iPad Wi-Fi Problems: Comprehensive List of Fixes
    http://appletoolbox.com/2010/04/ipad-wi-fi-problems-comprehensive-list-of-fixes/
    Fix iPad Wifi Connection and Signal Issues  http://www.youtube.com/watch?v=uwWtIG5jUxE
    Fix Slow WiFi Issue https://discussions.apple.com/thread/2398063?start=60&tstart=0
    How To Fix iPhone, iPad, iPod Touch Wi-Fi Connectivity Issue http://tinyurl.com/7nvxbmz
    Unable to Connect After iOS Update - saw this solution on another post.
    https://discussions.apple.com/thread/4010130
    Note - When troubleshooting wifi connection problems, don't hold your iPad by hand. There have been a few reports that holding the iPad by hand, seems to attenuate the wifi signal.
    ~~~~~~~~~~~~~~~
    If any of the above solutions work, please post back what solved your problem. It will help others with the same problem.
     Cheers, Tom

  • Time constrain in Core Java...not using Threads

    Hi All,
    I m designing a very small practice application in Java. I am running the program thru command prompt. Once the program starts, I want user t respond in certain amount of time, lets say 3 seconds.
    I tried looking for some "TIMER" classes in java api docs.
    most classes are used with Thread or MBean. I dont want to implement it this way.
    Could anyone pls help me identify how can this be implemented in core java without any fency classes like Thread being used.
    Looking forward for response.

    shilpadahiya wrote:
    Hi All,
    I m designing a very small practice application in Java. I am running the program thru command prompt. Once the program starts, I want user t respond in certain amount of time, lets say 3 seconds. You need to be clearer. When the three seconds are over do you want the program to output "times up!" and proceed, or do you want the program to wait for as long as it takes the user to answer and only after they answer the program responds, "you took 7.9 seconds!"
    I tried looking for some "TIMER" classes in java api docs.
    most classes are used with Thread or MBean. I dont want to implement it this way.Why not?

  • Like Norton Utilities for PCs, is it helpful to use a software on a MBP to clean up junk that is not used programs and documents?

    Like Norton Utilities for PCs, is it helpful to use a software on a MBP to clean up junk, that is not used programs or documents? If so what's the best software for this?

    No. See this recent thread and always look on right side bar for "More like this"
    https://discussions.apple.com/thread/5098172?tstart=0
    Norton for one thing does more harm than good and tries to modify the system's behavior.
    Your post is not in the MacBook Pro area, would also be more along t he lines of something for OS X Mavericks Community.
    MacBook Series Forums
    https://discussions.apple.com/community/notebooks?view=discussio 
    http://www.apple.com/support/macbookpro
    Mac OS X Forum
    https://discussions.apple.com/community/mac_os?view=discussions

  • Help Thread Not Spam

    After getting notified my linking of a help thread to all the threads suffering a common issue and the help thread being deleted I will try one more time to assist people who have broken phones,  Mine was stuck in a boot loop or would only work if connected to the wall, I suspected the battery and have replaced it and now the phone works 100% again, However when I replaced the battery and booted the phone it got hot with Android 5.02 installed, also simply while charging as well, This prompted me to roll back to 4.4.4 and all the heat issue has gone and the phone is working 100% again, This leads me to conclude 5.0.2 is flawed and is directly destroying batteries simply during the charge cycle until the battery even though reporting good and appearing charged 100% is in fact dead and holding about 5% charge which is not enough to boot properly, I actuall suspect the little connector on the battery is the victim of 5.0.2 but that's another day once I can test my theory,  So for now I would NOT update my phone to Lollypop until 5.1.1 is available for the device, if you want proof Google any Android phone running Lollypop and look at the similarities to the ones us Sony users have had, Some small remedies to help you repair yours if it's too late and it simply won't boot,  If you have the rear off of it, unclip the battery connector for a 30 seconds, hold power button and release, then reconnect the connector, Now hold Vol+ and Power until you get 3 vibes, Now plug it into the wall charger and leave it for a few hours, You can now attempt to boot it, if it boots and stays live long enough you may be able to flash back to 4.4.4 through Flashtool, not the Sony one, you need the one with the lightning bolt option, If you can complete the flash you may find it needs the back placed back on to read the NFC chip to fully install, All this is best done with a known good battery, one that can complete the flash or at least 25% charged 50% is better,  Final advise, DO NOT UPGRADE TO LOLLYPOP YET! I have the 5.1.1 on my Nexus 7 and it's great, but until it's an option on your phone avoid the updates, 4.4.4 removes all heat issues and it's heat that shuts it down in the end as it happened to me twice. I trust this is suitable this time.

    Besides some major issues with your design (who's going to call Ticker's stop() to stop the thread? Why doesn't your servlet have a useful doGet()/doPost()? Why not use your servlet as a web-based admin tool for your auto-emailer?), the probable reason for your thread not running is that you don't start it anywhere!
    An instance of your Runnable implementation (Ticker, in your case) needs to be passed to the Thread constructor (ExternalMailJavaBean in your case). Then the thread instance's start() method needs to be called.
    In your code, I see that Ticker is just used for timing. Why not do that in the ExternalMailJavaBean class's run() method instead? You need to work through the Thread tutorial again.

  • Photoshop Elements 8. "Could not use Clone Stamp Tool because of a program error."  Please help.

    Photoshop Elements 8.  "Could not use Clone Stamp Tool because of a program error."  Please help.

    Try this:
    Open your picture file
    Access the clone stamp tool
    Hold down the ALT key on the keyboard and left click on the area from which you wish to clone, then release the ALT key, and click to place the pixels at the destination
    TIPS:
    It is a good idea to open a blank layer at the top in the layers palette, and do the cloning on this layer. Be sure that "sample all layers" at the top is checked. You can change the layer opacity if necessary
    Use the bracket keys next to the letter p on the keyboard to increase & decrease the size of the cursor
    Let us know  how you make out with the error message now.

  • I have unabled 5 fingure gesture now not able to perform any task,also my power button is not working,please help me in removing this gesture,using I phone 4

    I have unabled 5 fingure gesture now not able to perform any task,also my power button is not working,please help me in removing this gesture,using I phone 4

    I have unabled 5 fingure gesture now not able to perform any task,also my power button is not working,please help me in removing this gesture,using I phone 4

  • TS3988 My icloud account is not verifying it says check my mail witch I have done and I get a message say your iPhone for has been used to set up and Icloud account but still can not use the icloud can anybody help me.

    My icloud account is not verifying it says check my mail witch I have done and I get a message say your iPhone for has been used to set up and Icloud account but still can not use the icloud can anybody help me.

    Be sure you are checking the email account that you used to set up the Apple ID you used to create the iCloud account.  Also, be sure to check your spam/junk folder as well as your inbox.  The email you should receive looks like the image below.  When you get it, click the Verify Now link.
    If it still isn't there, try going to https://appleid.apple.com, click Manage your Apple ID, sign in, click on Name, ID and Email addresses on the left, then to the right look for a link to resend the verification email.

  • HT1386 Hi All, I plugged my iPhone into my laptop and I was asked to upgrade my iTunes software. It took more than seven hours and just to be told that my SIM card does not appear to be supported. Since then I can not use my iPhone. I am lost plse help. J

    Hi All, I plugged my iPhone into my laptop and I was asked to upgrade my iTunes software. It took more than seven hours and just to be told that my SIM card does not appear to be supported. Since then I can not use my iPhone. I am lost plse help. Jam53
    Every time I switch on the iPhone, the iTunes icon is displayed together with a USB icon and Emergency call message appears on the screen in many languages. When I touch the i symbol I get the following information IMEI: **** AND ICCID: ****
    However, the iphone is visible in the Devices directory in iTunes. I changed my SIM card but still can not be recognized by the iPhone.
    <Edited By Host>

    Do you mean when you say its locked to another carrier other then the one im using, that maybe my insurance sent me an iphone thats for a different network for example orange and not mine which is o2?

  • Good evening, my name is Ludmila I have a problem. im buy second hand iphone 5 but I can not use the screen shows asking me apple id but I do not know this is my first iphone I do not know what the police thought it was stolen or lost but I you can help m

    Good evening, my name is Ludmila I have a problem. im buy second hand iphone 5 but I can not use the screen shows asking me apple id but I do not know this is my first iphone I do not know what the police thought it was stolen or lost but I you can help me. Someone told me we should stop function Find my iphone but how?? email does not even know the first lord. It cost me 400 euros but may not use iphone. someone laugh at me because I deceived. Please help me tell me his email talk to you should stop off iCloud or ID

    Scuzati de engleza google translate

  • Hi i have an ipad mini and i have not used it for 2 month or more. Today i have tried to use it i came across with a problem. my Ipad is blocked and it asks me to wait 23,401,418 :) what should i do need your help. thanks

    hi i have an ipad mini and i have not used it for 2 month or more. Today i have tried to use it i came across with a problem. my Ipad is blocked and it asks me to wait 23,401,418 what should i do need your help. thanks 

    Have you charged ipad Try a Reboot press & hold power button & menu button hold both down until you see Apple Logo You may need to do this more than once. Bsydd uk

Maybe you are looking for

  • Why is my publishing page blank?

    I have a Macbook Pro (Leopard) and have just created a website and cannot publish it, cause the publishing page is white - empty - blank? Why is that?

  • /var/adm/messages file not updatiing

    Hi All! Can you pls help, I´m new into solaris, so I´ve got a problem, ever since I didi "> messages" inside the /var/adm/ direcotory the messages file does not update anymore.~ I´ve done ps -ef ! grep syslogd, and the deamon is running. So pls can y

  • Safari crashing after Mavericks installed

    Process:         Safari [2285] Path:            /Applications/Safari.app/Contents/MacOS/Safari Identifier:      com.apple.Safari Version:         7.0 (9537.71) Build Info:      WebBrowser-7537071000000000~3 Code Type:       X86-64 (Native) Parent Pro

  • Hp dv 2000 shuts down automatically... plz help!!

    it runs perfectly for hours ...but after i restart it at any time it shuts off after every few mints....i have to wait at least 30 mints.. then it runs normal....plz help me out guise ..

  • Slowly loosing it

    i use windows live mail and over the last 3/4 weeks i think i have had to change my password about 15/20 times i  get emails on two days maybe, then it asks me for my password when i enter it it says it wrong, i have to go to bt.com and try and sign