Painting w/ delay

Hi everyone,

I'm currently trying to write a program that fills a space with a Hilbert/Peano curve. I've managed to paint all the lines, so now I'd like to make it look a little more interesting. That is, I want to put a delay between every painting of a new line. I tried a simple try - catch -block with a Thread.sleep( delay) in it, but that just leaves me waiting in the beginning (for several seconds depending on how many lines there are to paint) and then gives me all lines at once again.

The source is several pages long and I wouldn't quite know what part to cut out, so let me describe how it works:

I'm using MVC with my Model generating all the coordinates for the lines and then giving it to the View (as a Collection) to paint it. The Collection is then iterated with a delay between every step in the paint( Graphics g) method.

However, as I said before, this doesn't work like I thought it would.

My idea of the paint Method was, that it paints every line step by step, but it seems to 'collect' all information and then put it on the screen, thus leading to a delay in the beginning but not in between.

Is that last thought correct, and if so what can I do about it?

[1235 byte] By [Mahatma1204a] at [2007-11-27 7:27:24]
# 1

Demo:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class DrawingExample extends JPanel implements ActionListener {

private Timer timer = new Timer(200, this);

private Rectangle[] rects = new Rectangle[10];

private int currentUB;

private boolean desc;

public DrawingExample() {

setPreferredSize(new Dimension(new Dimension(300,300)));

for(int i=0; i<rects.length; ++i)

rects[i] = new Rectangle(i*15, i*15, 40, 40);

timer.start();

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

for(int i=0; i >< currentUB; ++i)

g2.draw(rects[i]);

}

public void actionPerformed(ActionEvent evt) {

currentUB += desc ? -1 : +1;

if (currentUB == -1) {

desc = false;

currentUB = 1;

} else if (currentUB == rects.length+1) {

desc = true;

currentUB = rects.length-1;

}

repaint();

}

public static void main(String[] args) {

EventQueue.invokeLater(buildApp);

}

static Runnable buildApp = new Runnable() {

public void run() {

JComponent comp = new DrawingExample();

JFrame f = new JFrame();

f.getContentPane().add(comp);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

};

}

Hippolytea at 2007-7-12 19:07:44 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi Hippolyte,

thanks for the lightspeed answer. I'm not sure, however, if this solves my problem. Just before you wrote this I tried putting the delay in the update-method of my View (being the Observer of the Model). So whenever a new coordinate is generated and the observer notified, there should be a delay and then the picture (as far as available) should be painted. That's how I see it, at least. Yet what actually happens is the same as before.

So, why doesn't it work like that?

Mahatma1204a at 2007-7-12 19:07:44 > top of Java-index,Java Essentials,Java Programming...
# 3

> Hi Hippolyte,

>

> thanks for the lightspeed answer. I'm not sure,

> however, if this solves my problem. Just before you

> wrote this I tried putting the delay in the

> update-method of my View (being the Observer of the

> Model). So whenever a new coordinate is generated and

> the observer notified, there should be a delay and

> then the picture (as far as available) should be

> painted. That's how I see it, at least. Yet what

> actually happens is the same as before.

> So, why doesn't it work like that?

Take a look where the delay is:

//place this wherever you have your "delay"

System.out.println(Thread.currentThread().getName());

If that is printing out "AWT-EventQueue-X", then you are in trouble

any delays in the event queue will only cause painting to malfunction.

You either need to drive your animation using a javax.swing.Timer,

as in my demo, or using a Thread you create. Since you activity

is GUI-related, I would try to use a timer.

Hippolytea at 2007-7-12 19:07:44 > top of Java-index,Java Essentials,Java Programming...