Custom Stroke

Tamer KARAKAN tamerkarakan at yahoo.com
Mon Feb 27 11:02:46 PST 2012


I think JavaFX must support custom stroke style. There is an example do it with a lot of hacking. I know some method may change over time. 
Technique quite simple. AnimationHelper created for support PathAnimation class. It's basicly calculate coordinates and rotation from any shape's path over time. We use it in a loop that add a shape every next location on shape's path. That shape can be any node. So every custom node can be a stroke any shape.



import com.sun.javafx.animation.transition.AnimationPathHelper;
import com.sun.javafx.animation.transition.Position2D;
import com.sun.javafx.geom.Path2D;
import com.sun.javafx.scene.layout.region.ShapeChangeListener;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.shape.Shape;

/**
 *
 * @author Tamer KARAKAN
 */
public class StrokeStyle {

    private AnimationPathHelper pathHelper;
    private Shape path;
    private double frac;
    private Position2D positionResult;
    private Group root;
    private Group strokeShape;

    public Group getGroup() {
        return root;
    }

    public void setGroup(Group root) {
        this.root = root;
    }

    public double getFrac() {
        return frac;
    }

    public void setFrac(double frac) {
        this.frac = frac;
    }

    public Shape getPath() {
        return path;
    }

    public void setPath(Shape path) {
        this.path = path;
    }

    public StrokeStyle(Shape path, Group group) {
        this(path, group, 1.0);


    }

    public StrokeStyle(Shape path, Group root, double frac) {
        this.path = path;
        this.frac = frac;
        this.root = root;
        
        applyStroke();
        path.impl_setShapeChangeListener(new ShapeChangeListener() {

            @Override
            public void changed() {
                applyStroke();
            }
        });
    }

    private void applyStroke() {
        if (strokeShape != null) {
            root.getChildren().remove(strokeShape);
        }
        if (strokeShape == null) {
            strokeShape = new Group();
        }
        if (!strokeShape.getChildren().isEmpty()) {
            strokeShape.getChildren().clear();
        }


        pathHelper = new AnimationPathHelper(new Path2D(path.impl_configShape()), null, .00001);

        for (double i = 0.0; i < 1.0; i += frac) {
            positionResult = new Position2D();
            Position2D position2D = pathHelper.getPosition2D(i, true, positionResult);
           Shape iterationShape= new Rectangle(10, 5, Color.DIMGREY);
   


            iterationShape.setRotate(positionResult.rotateAngle);
            iterationShape.setTranslateX(positionResult.x);
            iterationShape.setTranslateY(positionResult.y);

            iterationShape.setMouseTransparent(true);
            strokeShape.getChildren().add(iterationShape);

        }

        root.getChildren().add(strokeShape);
    }
}


More information about the openjfx-dev mailing list