I made A gui that is (640 200)x640 pixels big. On the left side is a chess board (it is at position 0,0 and has a size of 640x640.The 200 pixels on additional width are ment for some buttons to configure the game etc.
The problem:
The chess board's dimensions always have to be multiples of 8 to fit the frame perfectly. If i resize the frame a little bit so that its height increases by 7 pixels, there is a little gap between the chess board and the frame because the squares can't ne drawn in euqal size.
What is a clever way to fix this?
CodePudding user response:
I assume you use Swing or AWT, using Frame
or JFrame
or Window
or JWindow
as the window you're working in.
Step 1) Put your chessboard into its own JPanel
. Let's call that class ChessBoardPanel
.
Step 2) Set the ChessBoardPanel
's minimum, preferred and maximum sizes to the sizes you wish for in its constructor (or put it in a method if you wanna check the resize event of the parent and react to that, adjusting in 8px steps for bigger/smaller parent window.)
Step 3) Install a LayoutManager in the Window (window.setLayout(...)
), like BoderLayout
Step 4) Create a new instance of the ChessBoardPanel
: ChessBoardPanel myChessBoardPanel = new ChessBoardPanel(parentWindow)
;
Step 5) Add it to the parent window, maybe indicating layout behavior. For example, if you went with the BorderLayout
, add it like this in the parent window: this.add(myChessBoardPanel, BorderLayout.WEST);
so it will always be displayed on the left.