Results 1 to 20 of 30
Thread: 'sleep' not working well
- 01-17-2012, 02:23 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
'sleep' not working well
I have modified Points.java from this link: Basic drawing
It draws small lines at random points. But I want these lines to draw after an interval of 100 msec each time. So I tried to use Thread.sleep() function, but the program just stops. Please help.
Thanks in advance.Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import javax.swing.JPanel; import javax.swing.JFrame; import java.util.Random; public class Points extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.blue); Dimension size = getSize(); Insets insets = getInsets(); int w = size.width - insets.left - insets.right; int h = size.height - insets.top - insets.bottom; Random r = new Random(); for (int i=0; i<1000; i++) { int x = Math.abs(r.nextInt()) % w; int y = Math.abs(r.nextInt()) % h; g2d.drawLine(x, y, x+5, y+5); try{Thread.sleep(100); } catch(Exception e) {System.out.println("Exception in sleep function..");}; } } public static void main(String[] args) { JFrame frame = new JFrame("Points"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Points()); frame.setSize(750, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
- 01-17-2012, 03:30 PM #2
Re: 'sleep' not working well
Never call Thread.sleep() from the EDT. All you're doing is causing the call to paintComponent() to take longer, which must finish before displaying anything. You're just blocking the EDT, which will cause your GUI to become unresponsive and buggy.
Instead, create a Swing Timer that adds to some data structure that holds everything to be drawn. From paintComponent, simply draw everything in that data structure.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-17-2012, 04:10 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: 'sleep' not working well
Thanks for the info. Could you please explain with some code.
-
Re: 'sleep' not working well
You can find great explanation about use of Swing Timers and code at the tutorials: How to use Swing Timers
- 01-17-2012, 05:52 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: 'sleep' not working well
I am trying to add following Timer:
But I am getting this error:Java Code:javax.swing.Timer tm = new javax.swing.Timer(1500, new ActionListener() { public void actionPerformed(ActionEvent e) { g2d.repaint();} } ).start() ;
Java Code:ActionListener cannot be resolved to a type
- 01-17-2012, 06:11 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: 'sleep' not working well
Have you imported it?
- 01-18-2012, 01:52 AM #7
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: 'sleep' not working well
Yes. Following is the code I am trying:
It gives following error:Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.Timer; import java.util.Random; public class Points extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.blue); Dimension size = getSize(); Insets insets = getInsets(); int w = size.width - insets.left - insets.right; int h = size.height - insets.top - insets.bottom; Random r = new Random(); int i=0; Timer tm = new Timer(500, new ActionListener() { public void actionPerformed(ActionEvent e) { if (i>1000) return; int x = Math.abs(r.nextInt()) % w; int y = Math.abs(r.nextInt()) % h; g2d.drawLine(x, y, x+5, y+5); i++; }; //end action performed; } //end actionlistener; ); //end timer tm; tm.start(); } public static void main(String[] args) { JFrame frame = new JFrame("Points"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Points()); frame.setSize(750, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
Java Code:Points.java:32: error: ActionListener cannot be resolved to a type Timer tm = new Timer(500, new ActionListener() { public void actionPerformed(ActionEvent e) { ^^^^^^^^^^^^^^Last edited by rng; 01-18-2012 at 01:56 AM.
-
Re: 'sleep' not working well
I see no import for the ActionListener type. Toll's question is still quite valid.
- 01-18-2012, 02:59 AM #9
Re: 'sleep' not working well
On top of the advice you're already receiving, I didn't mean for you to create a new Timer every time paintComponent() is called. Create the Timer separate from the paintComponent() method, and call repaint() from it instead. In paintComponent(), simply draw whatever is in the data structure, which the Timer will add to.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-18-2012, 03:45 AM #10
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
Re: 'sleep' not working well
ActionListener is an interface so doesn't it have to be implemented to be used?
Oh and I think you need:Java Code:public class Points extends JPanel implements ActionListener{ //// //// Timer tm = new Timer(500, this); //// public void actionPerformed(ActionEvent e) { //// } //// }
Java Code:import java.awt.event.*;
Last edited by Bestsanchez; 01-18-2012 at 03:48 AM.
- 01-18-2012, 04:54 AM #11
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: 'sleep' not working well
I want a delay of 500 milliseconds between drawing of two lines. Will following work:
I think I need to write 'tm.start();' somewhere, but I do not know where. Thanks for your help.Java Code:import java.awt.event.*; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import javax.swing.JPanel; import javax.swing.JFrame; import java.util.Random; public class Points extends JPanel implements ActionListener { Timer tm = new Timer(500, this); public void actionPerformed(ActionEvent e) { repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.blue); Dimension size = getSize(); Insets insets = getInsets(); int w = size.width - insets.left - insets.right; int h = size.height - insets.top - insets.bottom; Random r = new Random(); for (int i=0; i<1000; i++) { int x = Math.abs(r.nextInt()) % w; int y = Math.abs(r.nextInt()) % h; g2d.drawLine(x, y, x+5, y+5); } } public static void main(String[] args) { JFrame frame = new JFrame("Points"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Points()); frame.setSize(750, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
'sleep(500);' used to be so simple in C!Last edited by rng; 01-18-2012 at 04:56 AM.
- 01-18-2012, 05:15 AM #12
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
Re: 'sleep' not working well
A few things I notice:
Put 'draw new line' code in 'actionPerformed'
Put tm.start(); in the constructor...
Oh and you'll probably need a constructor...
Oh and you should probably construct the object itself in main.
- 01-18-2012, 05:46 AM #13
Re: 'sleep' not working well
Why do they call it rush hour when nothing moves? - Robin Williams
- 01-18-2012, 05:48 AM #14
- 01-18-2012, 06:18 AM #15
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
- 01-18-2012, 09:28 AM #16
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: 'sleep' not working well
I am now using following code. A line is being drawn at intervals of 500msec (some movement on screen after all!) but each time the previous line is being erased. At any time there is only one line on screen. I want lines to come one after another, without previous one being erased. Also system is exiting after 10 lines only while I have coded it to exit after drawing 20 lines.
Thanks in advance.Java Code:import java.awt.event.*; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.Timer; import java.util.Random; public class Points extends JPanel implements ActionListener { public void actionPerformed(ActionEvent e) { repaint(); } int counter=0; static Timer tm; public void paintComponent(Graphics g) { tm = new Timer(500, this); tm.start(); super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.blue); Dimension size = getSize(); Insets insets = getInsets(); int w = size.width - insets.left - insets.right; int h = size.height - insets.top - insets.bottom; Random r = new Random(); int x = Math.abs(r.nextInt()) % w; int y = Math.abs(r.nextInt()) % h; g2d.drawLine(x, y, x+5, y+5); counter++; if (counter>20) System.exit(0); } public static void main(String[] args) { JFrame frame = new JFrame("Points"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Points()); frame.setSize(750, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
- 01-18-2012, 02:22 PM #17
Re: 'sleep' not working well
Like I already said- you need a data structure that holds everything to be drawn. You need a Timer (which is NOT initialized inside paintComponent) that adds to that data structure and calls repaint. Inside paintComponent, you simply draw everything in that data structure.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
Re: 'sleep' not working well
Agrees with all that Kevin has said and to amplify, the paintComponent method is for painting alone and nothing else, no program logic, no object creation, nothing. It needs to be small and fast as possible.
- 01-18-2012, 04:24 PM #19
Member
- Join Date
- Jan 2012
- Posts
- 23
- Rep Power
- 0
Re: 'sleep' not working well
I can't get what is meant by timer 'adds to' that data structure. Do I create Timer tm in the data structure class or in main()?You need a Timer that adds to that data structure and calls repaint.
- 01-18-2012, 04:43 PM #20
Re: 'sleep' not working well
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Thread.sleep
By Gog in forum New To JavaReplies: 3Last Post: 01-13-2011, 08:14 AM -
Do I have to put Thread to sleep?
By atch in forum New To JavaReplies: 0Last Post: 03-08-2010, 10:21 AM -
Sleep in thread
By jithan in forum New To JavaReplies: 1Last Post: 08-27-2008, 02:27 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
How to use the sleep and thread?
By jiuhu in forum Java AppletsReplies: 4Last Post: 08-07-2007, 02:56 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks