How to create shape with a hole?

2019-08-13 00:45发布

How to create shape with a hole in Java?

I want to create circle with circular hole inside.

If I just add Ellipse2D to a Path I am getting no holes.

UPDATE

I found, that winding rule controls that:

public class Try_Holes_01 {

    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);

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

                g2.fill(path);
            }
        };

        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);


    }

}

i.e. it draws hole with WIND_EVEN_ODD but draws filled shape with WIND_NON_ZERO.

But now I wonder, if it is possible to draw hole with WIND_NON_ZERO?

According to docs, this rule behaves regarding the parity. So, apparently, if I would able to change inner circle direction, then it would draw hole with WIND_NON_ZERO.

Is it possible to control circle direction? My be it is possible to invert direction after creation?

标签: java awt shape
0条回答
登录 后发表回答