Search code examples
javaswingjframerounded-corners

Java - Only Two Rounded Corners on a JFrame


I would like to round the top two corners on a JFrame for a project I am currently working on. I am currently rounding all four corners using setShape(new RoundRectangle2D.Double(0, 0, 200, 252, 30, 30)); but I do not want the bottom two rounded I want the to be a normal corner.


Solution

  • you can combine shapes to get this.By combining roundered rectangle with a normal rectangle you can make a rectangle without bottom two rounded corners.

    for example

    public class example extends JFrame{
    
        public example() {
            this.setUndecorated(true);
            this.getContentPane().setBackground(Color.red);
    
            Area shape1 = new Area(new RoundRectangle2D.Double(0, 0, 200, 252, 30, 30));
            Area shape2 = new Area(new Rectangle(0, 252-30, 200, 100));
            shape1.add(shape2);
            this.setShape(shape1);
            this.setSize(300, 400);
        }
        public static void main(String[] args) {
            new example().setVisible(true);
        }
    
    }
    

    enter image description here enter image description here

    alternatively you can give smaller height to the frame than RoundRectangle rectangle .so you can't see bottom of the RoundRectangle .and then you can get desired output