I am trying to draw a chessboard with setColor(c) and fillRect(x, y, dx, dy) methods of Graphics. I want it to be resizable according to the modifications of the window, but keeping the shape of a square. Additionally, I want it to be placed in the middle of the encompassing panel. 
As it may be seen below, the last one is not the case. Apart from that everything resizes well.

JFrameis split into two JPanels usingGridBagLayoutkeeping the proportion 3:1. The upper cell usesfill = GridBagConstraints.BOTH. The correspondingupperPanelis thus stretched as red border shows.- Then this 
upperPanelcontains anotherJPanel boardPanelused solely for the chessboard itself. This part is definitely wrong here but I don't know how to fix it. 
a. upperPanel defines the following layout in its constructor:
setLayout(new BorderLayout());
setBorder(BorderFactory.createLineBorder(Color.RED));
add(boardPanel, BorderLayout.CENTER);
I wanted to stretch the frame as far as possible within the grid cell.
b. boardPanel has then such layout:
setLayout(new BorderLayout());
setBorder(BorderFactory.createLineBorder(Color.BLUE));
Frankly speaking nothing else worked. The panel contains only graphics being drawn in paintComponent(Graphics g).
setColor(c)andfillRect(x, y, dx, dy)is done in a loop withinpaintComponent(Graphics g)method ofboardPanel, starting fromx=0, y=0. The length of a chessboard cell edge is determined every time this method is called through calculations ongetWidth()andgetHeight()of anupperPanel(asboardPanelis a nested class inupperPanel).
Is there a way to make boardPanel adjust to what was drawn within it? So that the boardPanel is the size of an actual image of the board. I guess then layout of upperPanel would place it in the center of itself.