I have an application I'm making... And it has a canvas in it. The problem is, on the y axis, my canvas is offset by ten pixels downward and I can't figure out why. You can see what I'm talking about in the screenshot. Do you know what's wrong?

Recommended Answers

All 11 Replies

Without the code? No, no idea.

Here's simplified code for the JFrame:

package gui;

import java.awt.*;

import javax.swing.*;

public class BoidFrame extends JFrame {


	JPanel content = new JPanel();  //The panel inside
	public Canvas canvas = new Canvas();

	public BoidFrame(int x, int y, final Insets insets) {

		setContentPane(content);
		setSize(x + (insets.left + insets.right), y + (insets.top + insets.bottom));
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		canvas.setSize(x, y);
		content.add(canvas);
	}
}

And here's the simplified main():

package gui;

import java.awt.*;

public class Main {
	static Insets insets;
	static int x = 400;
	static int y = 300;

	public static void main(String[] args) {

		LoadFrame load = new LoadFrame();
		load.setVisible(true);


		insets = load.getInsets();
		BoidFrame frame = new BoidFrame(x, y, insets);
		frame.setFocusableWindowState(false);
		frame.setVisible(true);
		Canvas canvas = frame.canvas;

		load.setVisible(false);
		frame.setFocusableWindowState(true);
		frame.toFront();
	}
}

What are the values returned by load.getInsets()? - looks like the canvas will be smaller than the frame by that amount?

22 pixels on top, 0 everywhere else. The insets are only used to figure out the size of the JFrame, because when you set the size of the JFrame it includes the insets. This means the insets do not affect the canvas.

I may still be worth spending 30secs changing them to 0 at the top and seeing what effect that has?

It just makes the frame smaller than it should be, with the same problem.

OK, well, thanks for trying anyway. Maybe it's a Mac Java problem? If you post a runnable version that demonstrates the problem I can try it under Win 7 Java 1.6.22 for you if you like.

Actually I'll try to run it on my laptop and see what happens. Thanks!

Er... Here's what it looks like on Windows 7. The size is different. Although in the picture it looks like the canvas offset is gone, whenever I resize it, it re-appears... Darn. I don't like GUIs...

What layout manager are you using? Personally I would always go with GridBagLayout for a window that can be re-sized - that way you have total control over what happens.

I fixed it! I forgot to set the layout to null.

Thanks so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.