Results 1 to 1 of 1
  1. #1
    blue50 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default How to pass variable back from thread for animation

    Hello how do I pass a variable (ScrollX) back from a thread to animate a ball bouncing around on a Jpanel... The following code has everything working except I cannot figure out how to pass the position variable back to paintcomponent so when i repaint it does not move...

    Also the only way I could get the paintcomponent to work on the jpanel was to call it from the jpanel constructor is there some other way to do this more efficiently?

    Thanks for the Help
    Java Code:
    import java.awt.*;
    import javax.swing.*;	
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent; 
    
    class UserInterfaceSetup
    {	 int ScrollX;
    	 JFrame frame = new JFrame("UserInterface");	
             JPanel Panel1 = new JPanel(){  
                                                      public void paintComponent(Graphics g) 
                                                       {
                                                       super.paintComponent(g);
                                                       g.setColor(Color.orange);  
                                                       g.fillOval(ScrollX,200,20,20);
                                                       }  
                                                    };		                     
    	public void ui() 		
    	{
    		Panel1.setLayout(null);		
    		frame.setVisible(true);										
    		frame.setSize(640,480);										
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(Panel1);										
    		UpdatePlot UpdatePlot1 = new UpdatePlot();
    		new Thread(UpdatePlot1).start();	
    	}
    
    class UpdatePlot extends JPanel implements Runnable  
    	{
    		public UpdatePlot(){}	
    		
    		public void run()  
      		{ 
    			while(true)
    			{
    			 System.out.println(ScrollX); 
    			 ScrollX = (int) (Math.random() * 300);
    			 repaint();  
        		try {  
                  Thread.sleep(1000);  
            	    } catch (Exception ex) {}
    			}   
    		}
    	}
    
    		
    }
    
    public class UserInterface extends UserInterfaceSetup
    {
    	public static void main(String[] args)
    	{
    		UserInterfaceSetup n = new UserInterfaceSetup();
    		n.ui();
    
    	}
    
    }
    Thanks again for the help!
    Last edited by blue50; 12-02-2010 at 05:03 AM. Reason: left something out...and comments messed up the tabs

Similar Threads

  1. pass a variable from one object to another
    By kev670 in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 01:20 AM
  2. Replies: 2
    Last Post: 04-18-2010, 04:48 AM
  3. Jframe pass variable to Applet
    By wayn in forum AWT / Swing
    Replies: 0
    Last Post: 03-10-2010, 09:54 AM
  4. How to pass a variable to another frame.
    By DJCali in forum New To Java
    Replies: 10
    Last Post: 10-14-2009, 03:57 AM
  5. How pass variable from jsp to servelet
    By shiva in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-22-2009, 01:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •