Results 1 to 11 of 11
Thread: repaint every
- 03-20-2010, 12:41 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
repaint every
Hi all
I want a frame to repaint a JFrame every x msecs. I put a thread.sleep(x) but this makes the whole thread sleep for this time. This causes a mouseMotionListener not to take input while the thread is sleeping. I need the coordinates of the listener to be in the calculation of the next cycle so the method above is not appropriate here. Is there a way to say to the repaint method to wait x time without making the whole thread to sleep?
Thank you for your help
Java Code:class MyDrawPanel extends JPanel { public static Plegma plegmaObject = new Plegma(); public void paintComponent(Graphics g) { for (int i=1; i<MainClass.columns; i++){ for (int j=1; j<MainClass.rows; j++){ if (plegmaObject.plegma[i][j].isOn == 1){ g.setColor(Color.orange); g.fillRect(plegmaObject.plegma[i][j].x,plegmaObject.plegma[i][j].y, Automaton.size, Automaton.size); } else{ g.setColor(Color.white); g.fillRect(plegmaObject.plegma[i][j].x,plegmaObject.plegma[i][j].y, Automaton.size, Automaton.size); } } } plegmaObject.calcNextStep(); repaint(); try{ Thread.sleep(500); } catch(InterruptedException ie){ } } }
-
If called in the Event Dispatch Thread, or EDT, Thread.sleep(...) will put the entire Swing app to sleep since the EDT is the only thread that Swing uses to do painting and user interaction. So use a Swing Timer instead as this won't put the EDT to sleep.
Also, your use of a static variable above is a little suspect. I'd avoid using static anything unless you have a very good reason to do so.Last edited by Fubarable; 03-20-2010 at 01:02 PM.
-
Moderator comment: Since this is a Swing topic, I'm moving this thread to the Swing forum.
- 03-20-2010, 02:54 PM #4
And never ever call repaint() from within a painting method override.
db
- 03-20-2010, 03:43 PM #5
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Java Code:new Thread( new Runnable() { public void run() { while (true) { repaint(); try { Thread.sleep(framerate); } catch (InterruptedException ie) { System.err.println("Error: "+ie); ie.printStackTrace(); } } } } ).start();I die a little on the inside...
Every time I get shot.
- 03-20-2010, 03:56 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
mmmm. I haven't read threads yet (I started learning 1 month ago) and seems essential for this. I will read threads and i will get back to this application.
Thank you again for your help
-
I respectfully disagree with this solution as I believe that directly using a separate thread here is a bit of over-kill. Instead, and again, use a Swing Timer. While this also uses a separate thread, it does so behind the scenes, and it is built specifically so that timed events will be called on the EDT, thus eliminating many worries. It's also a lot easier to start, stop, and to detect if it's running.
For e.g.:
Java Code:new Timer(500, new ActionListener() { public void actionPerformed(ActionEvent e) { plegmaObject.calcNextStep(); repaint(); } }).start(); // code not compiled and not tested
Note: ignore what I just stated if plegmaObject.calcNextStep(); is very CPU and time-intensive. If so, then this should be called from within a SwingWorker background thread.Last edited by Fubarable; 03-20-2010 at 05:28 PM.
- 03-23-2010, 08:09 AM #8
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
Ok. i got it working now. I just have two things unclear. I now have put the repaint method in the class i create the frame.
Is it the right place to put repaint()? And isn´t a bit strange that this doesnt interupt the MouseMotionListener methods.Java Code:public class AutomataGUI implements MouseMotionListener { public void show(){ JFrame frame = new JFrame("Cellular Automata"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyDrawPanel panel = new MyDrawPanel(); frame.setSize(754, 754); frame.setResizable(false); frame.addMouseMotionListener(this); frame.getContentPane().add(panel); frame.setVisible(true); while (true) { panel.repaint(); try{ Thread.sleep(100); } catch(InterruptedException ie){ } } }
The second and most important:
How can i access the variables of an instance of a class (an object) from another class without declaring them static.Also, your use of a static variable above is a little suspect. I'd avoid using static anything unless you have a very good reason to do so.
Thank you again
-
Again, use a Swing timer as what you're currently doing is at great risk of breaking.
Sorry, but this doesn't make sense to me. I've had no problem using variables in other classes without using a static modifier. Please show me a simple case where use of an instance variable fails.The second and most important:
How can i access the variables of an instance of a class (an object) from another class without declaring them static.
- 03-23-2010, 01:33 PM #10
Member
- Join Date
- Mar 2010
- Posts
- 10
- Rep Power
- 0
Java Code:public class One { //plegma public int x = 2; }Java Code:public class Two { public void changex(){ One.x = 5; } }This wont compile: Two.java:4: non-static variable x cannot be referenced from a static contextJava Code:public class MainClass { public static void main(String[] args) { Two two = new Two(); two.changex(); } }
One.x = 5;
-
The solution is not to use static but to use OOP techniques. For instance you should give the one class a way for two to change its state:
Java Code:public class MainClass { public static void main(String[] args) { Two two = new Two(); two.changex(); } } class Two { public void changex() { One one = new One(); one.setValue(5); System.out.println("one's value is: " + one.getValue()); } } class One { //plegma public int x = 2; public void setValue(int i) { x = i; } public int getValue() { return x; } }
If you don't have a good book on object oriented programming in Java, now's the time to get one. Much luck.
Similar Threads
-
Repaint problem
By citizenXL in forum New To JavaReplies: 4Last Post: 10-28-2009, 03:02 PM -
Trying to do a simple repaint
By IYIaster in forum New To JavaReplies: 9Last Post: 10-14-2009, 10:30 PM -
Repaint() not working
By Catkill in forum AWT / SwingReplies: 3Last Post: 09-09-2009, 10:51 PM -
repaint() problems
By Emily1100125 in forum AWT / SwingReplies: 5Last Post: 02-03-2009, 04:11 PM -
Help with repaint() command
By GeoffTK in forum New To JavaReplies: 2Last Post: 11-26-2008, 04:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks