Canvas Size Problem

I am having a problem with limiting the size of my canvas.

I am setting it to be 576 by 1024, but it ends up being created wider than it needs to be.

Note: red area = the proper size of the canvas, orange = the actual size of the canvas.

https://dl.dropbox.com/u/41733151/canvas_problem.png

I am using setSize, setMinimumSize, setMaximumSize, and setPreferredSize, but it still shows the canvas as larger than it should be.

Thanks,
novasharp

What contains it, and what layout manager does that container have?

call frame.pack() right before frame.setVisible(true), and don’t alter the frame after that.

I have a JFrame containing it.
The layout is just the default layout.

This is my code for initializing the frame:

this.width = width;
this.height = height;

Dimension size = new Dimension(width, height);

canvas = new Canvas();		
this.add(canvas);

canvas.setMinimumSize(size);
canvas.setMaximumSize(size);
canvas.setSize(size);
canvas.setPreferredSize(size);
canvas.setBounds(0, 0, width, height);

canvas.setBackground(Color.orange);

this.pack();

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setResizable(false);
setIgnoreRepaint(true);
getContentPane().setIgnoreRepaint(true);

call this.pack() right before this.setVisible(true), and don’t alter the JFrame after that.

Ok, thanks, that worked. I could not figure out what was going wrong.

novasharp

Unfortunately, I’m not going to explain it, because it requires knowledge of how Swing works, to the extend that you probably never will need to know, beyond this specific case.

You can switch to hardware acceleration (OpenGL) sooner or later, and all this obscurity disappears. :wink:

Thanks again.
If my game gets complex enough, I will switch over to opengl, but so far, my project is not complex enough to warrant all the graphics code.

novasharp