public MyLayout (){
        frame.setLayout(new BorderLayout());
        panel.setLayout(new BorderLayout());
        panel.add(new GraphicsSurface(),BorderLayout.CENTER);
        frame.add(panel,BorderLayout.NORTH);
        frame.add(btn1,BorderLayout.SOUTH);
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true); 
    }
I'm creating a graphic placing it inside a panel
panel.add(new GraphicsSurface(),BorderLayout.CENTER);
and placing this panel inside a JFrame
frame.add(panel,BorderLayout.NORTH);
Except the graphic is displaying outside the JFrame

                        
In
GraphicsSurfaceoverridegetPreferredSizeand give it the size you want (I guess the size of the clockFor
panel, don't set the layout. The defaultFlowLayoutwill work perfectly (it also centers by default -yeeee!). What will happen is that theGraphicsSurfacewill maintain its preferred size, asFlowLayoutwill respect it. Then thepanelwill be stretched to the width of the frame (the height will remain theGraphicsSurface's height), and theGraphicsSurfacewill be centered in thepanelWith this you should be fine, adding the
panelto the frame'sNORTH. The center will be left for everything else.