I have a class extending a JComponent. I override the
paintComponent(Graphics g) method to perform custom painting. Lets say
I want to stepwise paint some arbitrary shape, e.g. a square one side
at a time, side 1, 2, 3 and 4.
If I paint the square in one step the paintComponent method could look
like this - pseudo;
paintComponent(Graphics g){
super.paintComponent(g);
paint side 1, 2, 3 and 4;
}
Is there a way to perform a stepwise painting of this square, without
having to paint the already painted sides again;
paintComponent(Graphics g){
super.paintComponent(g);
paint side 1;
}
paintComponent(Graphics g){
super.paintComponent(g);
paint side 1, 2;
}
paintComponent(Graphics g){
super.paintComponent(g);
paint side 1, 2, 3;
}
paintComponent(Graphics g){
super.paintComponent(g);
paint side 1, 2, 3 and 4;
}
Knute Johnson - 17 Jun 2007 18:28 GMT
> I have a class extending a JComponent. I override the
> paintComponent(Graphics g) method to perform custom painting. Lets say
[quoted text clipped - 28 lines]
> paint side 1, 2, 3 and 4;
> }
You want your painting method to repaint all of what there is at that
time to paint. That way if you damage the window it will repaint
itself. Use some sort of state variable to control what is painted at
any given time.
Some minor additions to the XComponent class that I showed you before.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class XComponent extends JComponent implements Runnable {
private volatile int sides = 0;
public XComponent() {
setPreferredSize(new Dimension(40,40));
}
public void run() {
while (true) {
if (++sides > 4)
sides = 0;
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException ie) { }
}
}
public void paintComponent(Graphics g) {
if (sides > 0)
g.drawLine(10,10,30,10);
if (sides > 1)
g.drawLine(30,10,30,30);
if (sides > 2)
g.drawLine(30,30,10,30);
if (sides > 3)
g.drawLine(10,30,10,10);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
XComponent x = new XComponent();
f.add(x);
f.pack();
f.setVisible(true);
new Thread(x).start();
}
};
EventQueue.invokeLater(r);
}
}

Signature
Knute Johnson
email s/nospam/knute/
Roedy Green - 23 Jun 2007 12:19 GMT
>Is there a way to perform a stepwise painting of this square, without
>having to paint the already painted sides again;
I take it you want half a second or so between stages.
So you want some sort of timer than triggers your paint ever half
second. You ran use the
repaint( int x, int y, int width, int height )
feature to just ask your paint method to paint a piece (containing the
next segment). If it paints more, that part outside the clipregion is
ignored. Then you just do the math, not all the pixel shuffling.
You will need SwingUtilities.invokeLater since your timer will not be
the Swing thread.
See http://mindprod.com/jgloss/swing.html
http://mindprod.com/jgloss/timer.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com