0

I've been doing a CrossWord puzzle in java for a project. However I haven't been able to fix one problem I'm having with drawing the CrossWord into a JPanel

The program takes the data from a 2D array of chars and generates a grid of a custom type of JLabels with it. This is the code:

public void update(Observable o, Object o1) {
    if(model.getMatrix() != null){
        configurePanel(model.getRows(), model.getCols());
        this.add(panel);
        this.pack();
        this.revalidate();
        this.repaint();
    }

}

private void configurePanel(int w, int h) {
    if (panel!=null){
        this.remove(panel);
    }

    panel = new JPanel();

    panel.setBounds(40, 40, w*50, h*50);
    panel.setLayout(new GridLayout(w, h));

    labels = createLabels(model.getMatrix());

    for (int i = 0; i < w; i++){
        for(int j = 0; j < h; j++){

            panel.add(labels[i][j]);
        }           
    }
}

private CWlabel[][] createLabels(char[][] matrix) {
    int w = matrix.length;
    int h = matrix[0].length;
    labels = new CWlabel[w][h];

    for (int i = 0; i < w; i++){
        for(int j = 0; j < h; j++){
            char c = matrix[i][j];
            labels[i][j] = new CWlabel();
            labels[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
            labels[i][j].setOpaque(true);
            if (c != ' '){
                labels[i][j].setBackground(Color.white);
            } else {
                labels[i][j].setBackground(Color.black);
            }
        }
    }
    return labels;
}

My main problem is in configurePanel(), where the size of the panel are set proportional to the crossword dimensions, which should ensure every component inside it is perfectly square, however this is not the case as seen here

In the example shown the difference is subtle but still notizable. The strange part its that if I manually replace,

panel.setBounds(40, 40, w*50, h*50);

with,

panel.setBounds(40, 40, 400, 450);

the result appears to be proportional to the amount of rows and columns as shown here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Roberto Mora
  • 43
  • 1
  • 1
  • 10
  • 2
    1) Don't `setBounds()`! what's the point in using a [layout manager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) then? 2) For better help sooner please post a [mcve], 3) Have you read the [GridLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html) tutorial? – Frakcool Sep 06 '16 at 22:19
  • 2
    [Don't use `setBounds()` or `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Sep 06 '16 at 23:41

1 Answers1

1

By my count you have 9 rows and 8 columns so calling configurePanel like the following:

configurePanel(model.getRows(), model.getCols());

Is going to result in a width of 450 and a height of 400, but you want a width of 400 and a height of 450. Thus switch around the first and second parameters, like the following:

configurePanel(model.getCols(), model.getRows());

Note: You shouldn't use absolute positioning, it causes many formatting problems down the road (resizing components, adding components, etc.). You should instead use a layout manager with more customization like GridBagLayout or MigLayout.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43