如何在Piccolo2D中打造一个形状的洞?

时间:2013-10-27 12:30:21

标签: java graphics awt piccolo

在主Java中它支持“缠绕规则”,这可能有助于在形状上打洞。

不幸的是,在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

那么如何在Piccolo2D路径中打洞?

1 个答案:

答案 0 :(得分:2)

PPath为其操作维护了一个私有GeneralPath成员。它用WIND_NON_ZERO初始化。幸运的是,可以使用PPath.getPathReference()访问它。所以这应该可以解决问题:

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

结果如下:

enter image description here