Question

I am trying to create a little game of Atari GO, and i have a problem with the interface. I have a class called GameBoard that extends the JPanel class. In the main method i create such a GameBoard, then I create a JDialog, add the GameBoard to it, and invoke the pack() method on the JDialog object. Instead of getting this : My expected output

I get this : What i get

Here is my code:

    package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

public class GameBoard extends JPanel{
    BufferedImage backgroundImage;
    int boardSize;
    public GameBoard(){
        super();
    }
    public void initUi(){
        this.setBackground(Color.white);
    }
    public boolean setBoardSize(int nr) {
        //setting the dimension of the board
        this.boardSize = nr * 30;
        this.setSize(boardSize, boardSize);
        //i do some drawing next on a BufferedImage
        backgroundImage = new BufferedImage(this.boardSize, this.boardSize, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D)backgroundImage.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setColor(new Color(153,102,51));
        g2.fillRect(0, 0,this.boardSize, this.boardSize);

        g2.setColor(new Color(255,255,51));
        for (int i = 0; i < nr; i++) {
            g2.drawLine(0, 15 + i * 30, this.boardSize, 15 + i * 30);
            g2.drawLine(15 + i * 30, 0, 15 + i * 30, this.boardSize);

        }

        g2.setColor(new Color(255,51,102));
        g2.drawRect(2, 2, this.boardSize-4, this.boardSize-4);

        return true;
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.drawImage(backgroundImage, 0, 0, null);
    }
    public static void main(String []args){
        //i create here a GameBoard
        GameBoard board = new GameBoard();
        board.initUi();
        board.setBoardSize(10);

        JDialog gameDialog = new JDialog();
        gameDialog.setLocationRelativeTo(null);
        gameDialog.setModal(true);
        gameDialog.add(board);
        gameDialog.pack();
        gameDialog.setVisible(true);
    }

}

I've tried using different layout managers on the JDialog component , but with no result. What should i do to get the output i want? Thank you.

Was it helpful?

Solution

Override the panels getPreferredSize method and return the, well, preferred size you want.

When called, pack will ask the layout manager the preferred size of the content pane and ensure that the viewable area, as much as it can, meets those requirements

Ps: don't forget to dispose of the Graphics context from the BufferedImage

OTHER TIPS

Override the method getPreferredSize() in your class GameBoard like this:

@Override
public Dimension getPreferredSize() {
    return new Dimension(boardSize, boardSize);
}

And remove the this.setSize(boardSize, boardSize);.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top