This code draws two lines but waits a second..
I am looking how to do that in a separate thread so it wont freeze the application..
To draw one line and display it to the user and then the second..
Sorry but i am confused.. found too many solutions
public class Askisi2_3 extends JFrame {
private class LineJPanel extends JPanel {
public LineJPanel() {
setSize(500,500);
}
private void drawRandomLines(Graphics g) {
g.drawLine(5, 4, 50, 100);
try{
Thread.sleep(1000);
} catch(InterruptedException ex) {
}
g.drawLine(5, 4, 50, 200);
}
@Override
public void paint(Graphics g) {
super.paint(g);
drawRandomLines(g);
}
}
public Askisi2_3() {
initialiseComponents();
}
private void initialiseComponents() {
JPanel panel = new LineJPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
setSize(500, 500);
setVisible(true);
}
}
EDIT
Thank you for your responses!!!
A requirement for this is to use
try{
Thread.sleep(1000);
}
Is this possible ?
Here is my updated code
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < lines.length; i++) {
try{
Thread.sleep(1000);
}catch(InterruptedException e) {
}
g.drawLine(lines[i].getX1(),lines[i].getY1(), lines[i].getX2(), lines[i].getY2());
}
}
Before opening this thread googled it and found about Timer.. But i am forced to use Thread.Sleep().. so is there a solution or not?
So pst you are suggesting to put the sleep outside somehow?
Short answer
You can use
Thread.sleepbut not from the paint method. Use it from outside and just reapaint your panel.Long answer
As it is now, your code paints the panel, and until the pause is finish it returns. Visually it would look like the paint took too much time to finish.
What you need, is to have a “model” to paint. Your component would just paint that model and finish.
Then you add more “things” to your model every second, and that’s it.
For instance. Let’s say your model is an array of lines:
What you need to do in your paint method is to draw those lines:
And that’s it. That way you won’t freeze the GUI.
To add the effect of having more and more lines, you’ll create a separate thread and add lines to it.
To keep things simple, you can add that thread in the constructor:
That should as simple as adding more lines into the “model” ( the array ) and let the component re-paint them.
So to complete the code we could add a
addRandomLinemethod that creates a line, set some random values, and put it in the array:So, wrapping up your new thread would look like:
Be aware that, this would invoke
repaintin other thread different to the EDT. To fix this we would use:SwingUtilities.invokeLaterwhich let us define a method to be invoked “eventually” in the EDT:So, the final code ( with some formatting enhancements from my part ) would be:
The result is a very nice “line animated” panel: