gpt4 book ai didi

java - 如何在 Piccolo2D 中的形状上打洞?

转载 作者:行者123 更新时间:2023-11-30 04:10:56 24 4
gpt4 key购买 nike

在主 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 路径中打洞呢?

最佳答案

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

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

这是结果:

enter image description here

关于java - 如何在 Piccolo2D 中的形状上打洞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19618041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com