views:

45

answers:

3

The problem is, I am unable to make it a dimension with 800x600. In other words, when I run the program, the frame is so small that I can not do anything with it.

How can I make the frame larger?

I have set the preferred size already ans set the canvas bounds.

Then what is the problem?

public class GameCanvas extends Canvas
{
    private BufferStrategy buffer = null;

    public GameCanvas() 
    {
        setBounds(0, 0, 800, 600);
        setIgnoreRepaint(true);

        addKeyListener(new KeyInputHandler());

        requestFocus();     
    }

    public void addNotify()
    { 
        super.addNotify();
        this.createBufferStrategy(2);
        buffer = this.getBufferStrategy();

        setBounds(0, 0, 800, 600);
    }
}

public class GameGuiFrame extends JFrame
{
    private JPanel panel = new JPanel();
    private GameCanvas canvas = new GameCanvas();

    public GameGuiFrame()
    {
        this.setName("My Game");

        this.pack();
        this.setResizable(false);
        this.setVisible(true);

        panel = (JPanel) this.getContentPane();
        panel.setPreferredSize(new Dimension(750,500));
        panel.setLayout(null);
        panel.add(canvas);
    }
}

public class GameManager
{
    public static void runGameLoop()
    {
        GameGuiFrame container = new GameGuiFrame();

        container.addWindowListener(new WindowAdapter() 
        {
            public void windowClosing(WindowEvent e) 
            {
                System.exit(0);
            }
        });
    }
}

public class Main
{
    public static void main(String [] args)
    {
        GameManager.runGameLoop();
    }
}
+1  A: 

You call to pack() will set the frame (and components within it) to their preferred size. However, you haven't specified a preferred size. I would suggest removing your two calls to setBounds() and calling setBounds() within the main method instead of pack().

Adamski
+5  A: 

Try packing the Frame after you set the preferred size of the content pane.

Mark Peters
+2  A: 

Not related to your question but based on the code you posted it looks like you've copied some old AWT code and are trying to use it in a Swing application.

I would suggest you only use Swing components. There is no need to use a Canvas with a BufferStrategy. Just use a JPanel it is double buffered by default. The code snippet you copied is old and that is not the way it is done in Swing.

Don't use a null layout. Swing was designed to be using with layout managers. Then the pack() method will be able to do its job properly.

There is no need to use a WindowListener to close the frame. These days people just use:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Also, the frame should be made visible AFTER components have been added to the frame.

Generally you should be use Key Bindings, not a KeyListener to listen for key events in a Swing application.

I suggest you look at the Swing tutorial for more information about the above concepts.

camickr