Search code examples
javagraphicsdrawpaintcomponent

Java Graphics clear arc


Is there a method like clearRect(), but clearArc() ? I want to fill all the screen except one circle, something like that: picture (without those rounded corners) ( I'm doing that in paintComponent() )

P.S. Background is transparent


Solution

  • Try it out:

    import java.awt.Graphics2D;
    import java.awt.geom.Area;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.RoundRectangle2D;
    // and other stuffes you should have already imported
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Area area = new Area(
                new RoundRectangle2D.Double(0, 0, 200, 200, 50, 50));
        area.subtract(new Area(new Ellipse2D.Double(75, 50, 50, 50)));
        g.setColor(Color.RED);
        ((Graphics2D) g).fill(area);
    }
    

    Link to Java Tutorial: http://docs.oracle.com/javase/tutorial/2d/ (I'm somewhat lazy now...)

    enter image description here