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 6 of 6

Thread: Button Won't Stop Animation.

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

    Question Button Won't Stop Animation.

    package four.work.course;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    // import java.util.Random;
     
    public class MovingShapes {
     
    	protected JFrame frameCW4 = new JFrame("CourseWork Four");
    	private FirstDrawPanel firstDrawPanel = new FirstDrawPanel();
    	private SecondDrawPanel secondDrawPanel = new SecondDrawPanel();
    	private int xCoord = 0, yCoord = 0;
    	private int counter;
    	final private byte LOOPBACK  = 0;
    	protected boolean status = true;
    	// private Random generator = new Random();
     
     
    	protected void setUpComponents()
    	{
    		frameCW4.setVisible(true);
    		frameCW4.setSize(500,500);
    		frameCW4.getContentPane().add(BorderLayout.CENTER, firstDrawPanel);
    		frameCW4.getContentPane().add(BorderLayout.NORTH, secondDrawPanel);
    		frameCW4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
    	protected void animationManager(String direction)
    	{
    		String buffer;
    		buffer = direction.toString().trim();
     
    		if (buffer.equalsIgnoreCase("PartA"))
    		{
    			for (  counter = 0; counter < frameCW4.getWidth(); counter++ )
    			{
    				for ( counter = 0; counter < frameCW4.getHeight(); 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();
    						}		
    					}
    				}
    			}
    		}
    	}
     
    	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();
    					}
    				}
    			}
    		}
    	}
     
    	class FirstDrawPanel extends JPanel
    	{	
    		public void paintComponent(Graphics g)
    		{
    			g.setColor(Color.WHITE); 
    			g.fillRect(0, 0, this.getWidth(), this.getHeight());
    			g.setColor(Color.BLUE);
    			g.fillOval(xCoord, yCoord, 50, 50);
    		}
    	} 
    	class SecondDrawPanel extends JPanel
    	{
    		public void paintComponent(Graphics g)
    		{
    			g.setColor(Color.WHITE); 
    			g.fillRect(0, 0, this.getWidth(), this.getHeight());
    			g.setColor(Color.ORANGE);
    			g.fillRect(xCoord, 0, 50, 50);
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		MovingShapes ms = new MovingShapes();
    		ms.setUpComponents();
    		ms.animationManager("PartA");	
    	}
    }

    package four.work.course;
     
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
     
    public class MovingShapesButton extends MovingShapes implements ActionListener
    {	
    	private MovingShapes ms = new MovingShapes();
    	private JButton btnStopAnimation = new JButton("Stop Animation");
     
    	MovingShapesButton()
    	{	
    		ms.frameCW4.getContentPane().add(BorderLayout.SOUTH, btnStopAnimation);
    		btnStopAnimation.addActionListener(this);
    		ms.setUpComponents();
    		ms.retrace(true);
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    	}
     
    	public static void main(String[] args)
    	{
    		MovingShapesButton msb = new MovingShapesButton();
    	}
     
    }

    Help me out guys?
    Last edited by SyntheticD; March 23rd, 2011 at 09:41 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Button Won't Stop Animation.

    Well, you're ActionListener appears to be blank.

    You could have
    public void actionPerformed(ActionEvent e)
    	{
     
    if (e.getActionCommand().equals("Stop Animation"))
    {
    // get it to stop the animation somehow
    }
    	}

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

    Default Re: Button Won't Stop Animation.

    Quote Originally Posted by javapenguin View Post
    Well, you're ActionListener appears to be blank.

    You could have
    public void actionPerformed(ActionEvent e)
    	{
     
    if (e.getActionCommand().equals("Stop Animation"))
    {
    // get it to stop the animation somehow
    }
    	}
    Any theories on the "somehow" part.. I had some code in there, but when I clicked the button, it freezed. That's why I removed it.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Button Won't Stop Animation.

    No such word as freezed. It froze!

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Post Re: Button Won't Stop Animation.

    That's Java for you.

    What was your code in there. Perhaps we, if not me since I'm bad at animation, others could figure out why it "Freezed!"

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Button Won't Stop Animation.

    For animation you should really use a SwingTimer (see How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features) ). Going into a while loop to accomplish animation is not the best way to go about it, especially given the single threaded model of Swing

  7. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (March 24th, 2011)

Similar Threads

  1. Stopping Graphics Animation.
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 25th, 2011, 11:59 AM
  2. Interaction between two label in animation
    By odiejodie in forum Java Theory & Questions
    Replies: 1
    Last Post: February 16th, 2011, 08:51 AM
  3. Stop a while loop with an awt button, problem
    By frx08 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 20th, 2011, 08:24 AM
  4. Basic Animation
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 10:07 AM
  5. Problem with my animation applet
    By cBlue in forum Java Applets
    Replies: 1
    Last Post: December 9th, 2009, 07:49 PM