I'm quite new to java.

I was developing a game using java and I realized that the position of pack() method can influence rendering. For instance:

Frame Class

public class Frame {
    public Frame(int width, int height, Game game) {
        JFrame frame = new JFrame("Practice Game");

        frame.setPreferredSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        frame.pack();  // pack method is used before adding game component to frame
        frame.add(game);
    }
}

Game Class (I skipped few methods for simplicity)

public class Game extends Canvas implements Runnable {
    // Dimension
    public static final int WIDTH = 320;
    public static final int HEIGHT = 320;

    public Game() {
        new Frame(WIDTH, HEIGHT, this);

        menu = new Menu(WIDTH, HEIGHT);

        start();
    }

    public void render() {
        BufferStrategy bs = this.getBufferStrategy();

        if (bs == null) {
            this.createBufferStrategy(3);
            return;
        }

        Graphics2D g = (Graphics2D) bs.getDrawGraphics();

        menu.render(g);

        g.dispose();
        bs.show();
    }

    public static void main(String[] args) {
        new Game();
    }

This set of code results in making an empty frame without any component displayed.

However, when I tried putting pack method after I added game component to the JFrame, it successfully displayed what I intended to display.

I heard that pack() method is used to pack the components within the JFrame based on the component’s preferred sizes. However, I don't understand how the position of pack() method could influence the entire rendering (only displaying an empty frame).


Also, the actual size of frame doesn't match the dimension that I assigned to the frame. To illustrate this:

Menu Class

public class Menu {
    public Menu(int width, int height) {
        this.width = width;
        this.height = height;

        squareSize = 64;

        squareRowNum = height / squareSize;
        squareColNum = width / squareSize;
    }

    public void render(Graphics2D g) {
        drawBackground(g);
    }

    public void drawBackground(Graphics2D g) {
        for (int i = 0; i < squareRowNum; i++) {
            for (int j = 0; j < squareColNum; j++) {
                g.setColor(Color.black);
                g.setStroke(new BasicStroke(3));
                g.drawRect(j * squareSize, i * squareSize, squareSize, squareSize);
                g.setColor(Color.white);
                g.fillRect(j * squareSize, i * squareSize, squareSize, squareSize);
            }
        }
    }

When running these 3 classes together, it display:

The rectangle box at the bottom are cut off instead of fully displaying 5 * 5 rectangles (each rectangles has dimension of 64, 64 and the frame size is supposedly 320, 320). I assumed that it's because I'm using mac. I think this can also be fixed using pack() method but I don't know how.

You must be careful with setting the size... if you set the size from the JFrame you override the ability of pack().

You should ONLY set the size of the content, not of the JFrame itself. this pic shows you the difference between content size and JFrame size: enter image description here

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.