I was developing a game using java AWT 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.pack();  // pack method is used before adding game component to frame

Game Class

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);


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

        if (bs == null) {

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



    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) {

    public void drawBackground(Graphics2D g) {
        for (int i = 0; i < squareRowNum; i++) {
            for (int j = 0; j < squareColNum; j++) {
                g.setStroke(new BasicStroke(3));
                g.drawRect(j * squareSize, i * squareSize, squareSize, squareSize);
                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.


1 Answer 1


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


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .