Results 1 to 1 of 1
- 12-02-2010, 04:54 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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
Thanks again 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(); } }Last edited by blue50; 12-02-2010 at 05:03 AM. Reason: left something out...and comments messed up the tabs
Similar Threads
-
pass a variable from one object to another
By kev670 in forum New To JavaReplies: 3Last Post: 11-25-2010, 01:20 AM -
How can I Pass Variable from JApplet to PHP?
By fantasyme in forum JDBCReplies: 2Last Post: 04-18-2010, 04:48 AM -
Jframe pass variable to Applet
By wayn in forum AWT / SwingReplies: 0Last Post: 03-10-2010, 09:54 AM -
How to pass a variable to another frame.
By DJCali in forum New To JavaReplies: 10Last Post: 10-14-2009, 03:57 AM -
How pass variable from jsp to servelet
By shiva in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-22-2009, 01:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks