2

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.

Noupoi
  • 93
  • 7
  • I just found [this page](http://www.java2s.com/Code/JavaAPI/java.awt.geom/PathIteratorcurrentSegmentdoublecoords.htm) that contained a very useful example. I just realised that I was being dumb and reading the api wrong. The the coordinates array can only be a max size of coords[6]. Calling repaint now does not do anything, so I figure I will have to use a helper thread to update the path in the background. – Noupoi Apr 01 '14 at 11:58
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Apr 02 '14 at 06:59

2 Answers2

2

You can use FlatteningPathIterator pasing your Shape and processing segments.

See as example moving point here http://java-sl.com/tip_flatteningpathiterator_moving_shape.html

StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

I just found this page that contained a very helpful example.

Turns out I was reading the api wrong. The coordinates array can only be a max size of 7.

To get this to work, I also had to use SwingWorker to update the Path in the background. redrawPath() just starts the thread.

This is what the code in doInBackGround of the SwingWorker looks like:

PathIterator pi = oldPath.getPathIterator(null);
while (!pi.isDone()) {
            double[] coordinates = new double[6];
            int type = pi.currentSegment(coordinates);

            switch (type) { //Decide what do based on type of segment
                case PathIterator.SEG_MOVETO:
                    tempPath.moveTo(coordinates[0], coordinates[1]);
                    break;
                case PathIterator.SEG_LINETO:
                    tempPath.lineTo(coordinates[0], coordinates[1]);
                    break;
                default:
                    break;
            }

            publish(tempPath.clone());

            pi.next();
        }

The process method updates the path on the canvas and calls repaint();

Noupoi
  • 93
  • 7