Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Stopping Graphics Animation.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Stopping Graphics Animation.

    protected void retrace(boolean check)
    	{
    		while (status == true)
    		{
    			for (counter = LOOPBACK; counter <= frameCW4.getWidth(); counter++ )
    			{
    				for (counter = LOOPBACK; counter <= frameCW4.getHeight(); counter++)
    				{
    						{
    							// System.out.println(counter);
    							// xCoord = generator.nextInt(frameCW4.getWidth());
    							// yCoord = generator.nextInt(frameCW4.getHeight());
     
    							xCoord++;
    							yCoord++;
     
    							try 
    							{
    								Thread.sleep(5);
    								firstDrawPanel.repaint();
    								secondDrawPanel.repaint();
    							}
    							catch (InterruptedException e)
    							{
    								e.printStackTrace();
    							}	
    						}
    				}
    			}
     
    			for ( counter = frameCW4.getWidth(); counter >= LOOPBACK; counter--)
    			{
    				for (counter = frameCW4.getHeight(); counter >= LOOPBACK; counter --)
    				{
    					xCoord--;
    					yCoord--;
     
    					try 
    					{
    						Thread.sleep(5);
    						firstDrawPanel.repaint();
    						secondDrawPanel.repaint();
    					}
    					catch (InterruptedException e)
    					{
    						e.printStackTrace();
    					}
    				}
    			}
    		}
    	}

    This code makes the graphics object move through the JFrame, how do I do it such that, when a button is clicked, the graphics objects stop moving.
    Last edited by SyntheticD; February 23rd, 2011 at 08:02 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stopping Graphics Animation.

    What Thread is this one. I'm hoping it's not the EDT, as it's pretty horrible to sleep on the EDT. But if it's not, then you're modifying variables you presumably use on the EDT, which might get tricky.

    But the basic answer is- why don't you just have a boolean that indicates that the objects should be moving, check that in your above loop, and set it to false when you click the button? When it's false, exit the loop.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stopping Graphics Animation.

    I tried that, but when the button is clicked to stop the animation, program "freezes"

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stopping Graphics Animation.

    If you want help, you'll have to provide an SSCCE that demonstrates the problem. Otherwise we're just guessing at what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Starting and Stopping a loop with JButtons
    By Endevor in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 8th, 2010, 03:37 PM
  2. Stopping All Current Thread
    By newbie in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 08:27 PM
  3. While loop stopping?
    By Echo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 16th, 2010, 05:40 PM
  4. Code stopping, need help fast.
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2010, 09:00 AM
  5. Basic Animation
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 10:07 AM