So I’d like to make a Canvas based Java application. I’ve extended my main class to Canvas and I size it in it’s constructor.
public CanvasApp() {
Dimension size = new Dimension(640, 480);
setSize(size);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
}
and in the main function, I make a frame for it, like this:
CanvasApp cnv = new CanvasApp();
JFrame frame = new JFrame("");
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(null); //I've tried this
panel.setSize(640,480); //but still doesn't work =(
panel.add(cnv, BorderLayout.CENTER);
frame.setContentPane(panel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
But the content pane appears 650×490 in size. Why is this?
I’ve attached a picture too.
Because a JFrame has a 5px border around it, look at any frame on your computer screen you’ll notice a kind of embossed border – that border is 5 px in width adding 10 pixels onto height and width. You’ve only assigned the panel to 640 x 480 and plonked it inside the frame – the frame then adds it’s own border onto that.