Grid drawn on A JPanel is not visible completely

KunLun :

I have a panel which extends JPanel and overrides method paintComponent(Graphics g) which draw some squares.

public class Painter extends JPanel{

    private static final Color orange = new Color(225, 95, 0);

    private Block[][] blocks;
    private int sizeBlock;

    public Painter() {
        this.setBackground(Color.WHITE);
    }

    public void setBlocks(Block[][] blocks) {   
        this.blocks = blocks;   
    }

    public void setSizeBlock(int size) {    
        this.sizeBlock = size;  
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        for(Block[] cols : blocks) {
            for(Block block : cols) {

                switch(block.getType()) {

                    case Block.wall: {

                        paintBlockLineBorder(g, block.getX()*(sizeBlock-1),
                                                block.getY()*(sizeBlock-1), orange);
                        break;

                    }

                    default: {break;}
                }           
            }       
        }       
    }

    private void paintBlockLineBorder(Graphics g, int x, int y, Color color) {

        //background
        g.setColor(color);
        g.fillRect(x, y, sizeBlock, sizeBlock);

        //border
        g.setColor(Color.BLACK);
        g.drawRect(x, y, sizeBlock-1, sizeBlock-1);

    }
}

I add this JPanel (Painter) into other JPanel (painterPanel) which is added to a JFrame with layout GridBagLayout.

The result is not how I want:

This is what I want:

Here is a little use-case which reproduces the problem:

public static void main(String[] args) {

    JFrame mainFrame = new JFrame();
    mainFrame.setLayout(new GridBagLayout());
    mainFrame.setBounds(100, 100, 500, 400);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.fill = GridBagConstraints.NONE;
    gbc.gridx = 0; gbc.gridy = 0;
    gbc.weighty = 1; gbc.weightx = 1;

    JPanel painterPanel = new JPanel();
    painterPanel.setBorder(BorderFactory.createLineBorder(Color.black));

    mainFrame.add(painterPanel, gbc);

    Painter apainter = new Painter();

    painterPanel.add(apainter);

    Block[][] ablock = new Block[2][2];
    ablock[0][0] = new Block(0, 0, Block.wall);
    ablock[0][1] = new Block(0, 1, Block.wall);
    ablock[1][0] = new Block(1, 0, Block.wall);
    ablock[1][1] = new Block(1, 1, Block.wall);

    apainter.setBlocks(ablock);
    apainter.setSizeBlock(25);

    apainter.repaint();

    mainFrame.setVisible(true);     
}

and here is Block class:

public class Block {

    public static final String wall = "wall";

    private int x;
    private int y;
    private String type;

    public Block(int x, int y, String type) {

        this.x = x;
        this.y = y;
        this.type = type;
    }

    //getters & setters
}

Why is my Painter not completely visible?

KunLun :

At advice of @CarlosHeuberger I overrided getPreferredSize, getMinimumSize and getMaximumSize which return a Dimension with values based on lengths of blocks and value of sizeBlock

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109550&siteId=1