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.
Re: 'sleep' not working well
Thanks for the info. Could you please explain with some code.
Re: 'sleep' not working well
Quote:
Originally Posted by
rng
Thanks for the info. Could you please explain with some code.
You can find great explanation about use of Swing Timers and code at the tutorials: How to use Swing Timers
Re: 'sleep' not working well
I am trying to add following Timer:
Code:
javax.swing.Timer tm = new javax.swing.Timer(1500, new ActionListener() { public void actionPerformed(ActionEvent e) { g2d.repaint();} } ).start() ;
But I am getting this error:
Code:
ActionListener cannot be resolved to a type
Re: 'sleep' not working well
Re: 'sleep' not working well
Yes. Following is the code I am trying:
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);
}
}
It gives following error:
Code:
Points.java:32: error: ActionListener cannot be resolved to a type
Timer tm = new Timer(500, new ActionListener() { public void actionPerformed(ActionEvent e) {
^^^^^^^^^^^^^^
Re: 'sleep' not working well
I see no import for the ActionListener type. Toll's question is still quite valid.
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.
Re: 'sleep' not working well
ActionListener is an interface so doesn't it have to be implemented to be used?
Code:
public class Points extends JPanel implements ActionListener{
////
////
Timer tm = new Timer(500, this);
////
public void actionPerformed(ActionEvent e) {
////
}
////
}
Oh and I think you need:
Code:
import java.awt.event.*;
Re: 'sleep' not working well
I want a delay of 500 milliseconds between drawing of two lines. Will following work:
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);
}
}
I think I need to write 'tm.start();' somewhere, but I do not know where. Thanks for your help.
'sleep(500);' used to be so simple in C!
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.
Re: 'sleep' not working well
Quote:
Originally Posted by
Bestsanchez
ActionListener is an interface so doesn't it have to be implemented to be used?
Yes, but it's not necessary or even desirable to implement it in a public class. The approach already shown in this thread, which involves an anonymous inner class, is far more common and in most cases improves readability and maintainability.
db
Re: 'sleep' not working well
Quote:
Originally Posted by
Bestsanchez
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.
It's nice that you want to help, but I suspect you need more experience in Java before you can make any meaningful suggestions in this topic.
db
Re: 'sleep' not working well
Quote:
Originally Posted by
DarrylBurke
It's nice that you want to help, but I suspect you need more experience in Java before you can make any meaningful suggestions in this topic.
db
I'll go back to asking questions instead of answering them because you're right, i'm only 4 months into java experience haha.
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.
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);
}
}
Thanks in advance.
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.
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.
Re: 'sleep' not working well
Quote:
You need a Timer that adds to that data structure and calls repaint.
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()?
Re: 'sleep' not working well
Quote:
Originally Posted by
rng
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()?
Huh? What would it mean to create a Timer in the data structure? And what happened when you just tried it out?