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: Animation

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

    Default Animation

    Hi guys,

    I would like to run a simple animation by pressing the run button. but It does not work and I don't know what is wrong with my code. I appreciate your help.

    Iman

     
    import javax.swing.*;
    import java.awt.*; 
    import java.awt.event.*; 
     
     
    public class SimpleAnimation {
    	JFrame frame; 
    	JButton Animate;
    	MyDrawPanel md;
    	int x=70; 
    	int y=70; 
     
    	public static void main(String [] args){ 
     
    		SimpleAnimation gui=new SimpleAnimation(); 
     
    		gui.go(); 
     
    	}
     
    	public void go(){ 
     
    		frame=new JFrame(); 
    		JButton Animate=new JButton("Run"); 
     
     
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     
    		md=new MyDrawPanel(); 
     
    		frame.getContentPane().add(BorderLayout.SOUTH,Animate); 
     
    		frame.getContentPane().add(md); 
     
    		Animate.addActionListener(new ButtonListener()); 
     
     
    		frame.setSize(500,500);
    		frame.setVisible(true); 
     
     
     
    	}
     
    	class ButtonListener implements ActionListener{ 
    			public void actionPerformed(ActionEvent event){ 
    				for(int i=0;i<130;++i){ 
     
    					x++; 
    					y++; 
     
    					md.repaint(); 
     
    					try{ 
    						Thread.sleep(50); 
     
    					}
    					catch(Exception ex){}
     
    				}
    			}
    	}
     
    	class MyDrawPanel extends JPanel{ 
     
    		public void paintComponent(Graphics g){ 
     
    			Graphics2D g2d=(Graphics2D) g; 
     
    			g2d.setColor(Color.white);
    			g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); 
     
    			g2d.setColor(Color.green); 
     
    			g2d.fillOval(x, y, 100, 100); 
     
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Animation

    It does not work
    There are many ways for a program to "not work". Can you explain what the program does?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Animation

    I would like to press the run button for running the animation ( for loop inside the buttonlistener). If you copy that for loop inside the go method, its work perfectly but inside the buttonlistener it s not working.





    Quote Originally Posted by Norm View Post
    There are many ways for a program to "not work". Can you explain what the program does?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Animation

    Can you explain what the program does when you execute it?


    One problem I see is a long running loop in the actionListener method. This holds up any updates of the GUI until the method exits. It runs on the EDT which does the GUI updates.
    For long running jobs, you need to start a new thread that will do the long running job and exit the listener method as some as possible.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Animation

    If I add that for loop to gui.go(). it works. it shows a circle that move from x=0,y=0 to x=130,y=130. but I would like to do this when I press the run button.

    I don't know how I can fix this. I appreciate if you can help me.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Animation

    you need to start a new thread that will do the long running job and exit the listener method as some as possible.
    If you don't understand my answer, don't ignore it, ask a question.

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

    iman1300 (February 17th, 2013)

Similar Threads

  1. lagging animation
    By chri in forum What's Wrong With My Code?
    Replies: 35
    Last Post: December 29th, 2012, 06:48 AM
  2. Animation in JApplet
    By Aurin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 14th, 2012, 10:18 AM
  3. Use of threads in an animation
    By 2by4 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 15th, 2011, 09:47 AM
  4. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  5. Basic Animation
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 10:07 AM