javagraphicsawtpiccolo

How to make holes in a shape in Piccolo2D?


In main Java it supports "winding rule" which may help to make holes in the shapes.

Unfortunately, this concept is ignored in Piccolo2D:

public class Try_Holes_01 {

    @SuppressWarnings("serial")
    public static void main(String[] args) {

        final Path2D path = new Path2D.Double(Path2D.WIND_EVEN_ODD);
        //final Path2D path = new Path2D.Double(Path2D.WIND_NON_ZERO);

        path.append(new Ellipse2D.Double(100,100,200,200), false);
        path.append(new Ellipse2D.Double(120,120,100,100), false);

        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;

                g2.fill(path);
            }
        };

        final PPath path_p = new PPath(path);
        path_p.setPaint(Color.BLACK);

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        new PFrame() {
            @Override
            public void initialize() {
                getCanvas().getLayer().addChild(path_p);
            }
        };


    }

}

enter image description here

So how to make holes inside Piccolo2D paths?


Solution

  • PPath maintains a private GeneralPath member for its operations. It is initialized with WIND_NON_ZERO. Luckily it can be accessed with PPath.getPathReference(). So this should do the trick:

    path_p.getPathReference().setWindingRule(Path2D.WIND_EVEN_ODD);
    

    Here is a result:

    enter image description here