The code you sent is not complete, it lacks how the panel is added to the JFrame and which LayoutManager is used.
Code seams must be correct. Are you sure JPanel does not take up the whole JFrame? Add System.out.println(drawingPanel.getSize()) to verify this. If you use BorderLayout, by default for JFrame and just added a panel without any restrictions, the panel will use the entire area. Preferred size is ignored.
Try this, just for testing:
public Drawing (){ drawingPanel = new JPanel(); drawingPanel.setPreferredSize(new Dimension(600,600));
but IMO is not the best or right way to do it. I would rather override the paintComponent() method from JPanel, as suggested by Torsten and Camicre.
But it will still use the entire JFrame area until another component is added to the JFrame or the LayoutManager is modified.
source share