aerion
aerion

Reputation: 732

How to center JPanel with dynamic size

I need to display chessboard. I have a BoardPanel class which extends JPanel and a GamePanel (also extending JPanel) class containing BoardPanel. GamePanel fills all the application frame.

I want BoardPanel to always be a square with size equal to the minimum of GamePanel's width and height (if GamePanel's width is greater than height there should be empty space on the left and right, if it's smaller there should be empty space on top and bottom). It's also important that BoardPanel should be displayed in the center of parent panel.

I wrote sth like this:

public GamePanel() {
    setLayout(new BorderLayout(0, 0));
    boardPanel = new BoardPanel(...);
    this.add(boardPanel, BorderLayout.CENTER);
    ...
}

and in BoardPanel:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int size = Math.min(this.getParent().getHeight(), this.getParent().getWidth());
    this.setSize(size, size);
    ...
}

It resizes well, but chessboard is always displayed in top left corner of GamePanel (all the empty space is displayed on bot or right) and I don't know how to fix it.

Any help? Thanks in advance!

Upvotes: 1

Views: 2211

Answers (2)

David Kroukamp
David Kroukamp

Reputation: 36423

  • No need for new BorderLayout(0,0) simply use default constructor for BorderLayout

  • Dont call setSize() rather override getPreferredSize() of JPanel like so:

    @Override
    public void getPreferredSize() {
    
            int size = Math.min(this.getParent().getHeight(), this.getParent().getWidth());
    
           return new Dimension(size,size);
    }
    
  • also its never good to do work in your paintComponent as this should be used exclusively for painting only.

If the above does not work I'd suggest a SSCCE to illustrate specific problems you might have

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168845

Center it using a GridBagLayout.

Centered, square panel

import java.awt.*;
import javax.swing.*;

public class CenteredPanel {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JPanel gui = new JPanel(new GridBagLayout());
                JPanel square = new SquarePanel();
                square.setBackground(Color.RED);
                gui.add(square);

                JFrame f = new JFrame("SquareBoard");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);
                f.add(gui);
                f.setMinimumSize(new Dimension(400,100));
                f.pack();
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class SquarePanel extends JPanel {
    @Override
    public Dimension getPreferredSize() {
        Container c = this.getParent();
        int size = Math.min(c.getHeight(), c.getWidth());
        Dimension d = new Dimension(size,size);
        return d;
    }
}

Upvotes: 4

Related Questions