I'm creating a program that lets users draw on the screen, much like using the pencil tool in MS paint, and then allow the user to replay the process of creating the drawing, as if someone was painting it in front of you.
The way I've done this using Path2D, and through the moveTo and lineTo methods, draw a line using the path.
I now can't seem to figure out how to animate the redrawing of the Path2D object. My current strategy is to create a new a Path2D, and using a PathIterator, iteratively add the line segments from the old path to the new path.
This is what I'm thinking so far:
public void redrawPath() {
Path2D oldPath = path;
path = new Path2D.Double();
double[] coords = new double[100];
PathIterator pi = oldPath.getPathIterator(new AffineTransform());
while (!pi.isDone()) {
pi.next();
pi.currentSegment(coords);
//Add segment to new path
repaint();
}
}
The main issue is that I don't know the size of the line segments, so I don't know how to size the coords array. I also haven't quite figured out how I'm going to add the segments to the new path. It would seem that the append method in Path2D could be used, though it would seem to add the entire path to itself.
I realise that Path2D is a Shape, but I can't seem to find any alternative ways of doing this.