"or explain a better way to get the exact dimensions of the drawing area I would appreciate it."
Override getPreferredSize()
in your JPanel
class
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
When you you paint, make use of getWidth()
and getHeight()
, inherited from the JPanel
class. Using these methods, your painting will resize dynamically with the resizing of the panel.
int width = getWidth();
int height = getHeight();
g.fillRect((int)(width * 0.9), (int)(height * 0.9),
(int)(width * 0.8), (int)(height * 0.8));
Side Note
You should call super.paintComponent
inside the paintComponent
method so as not to break the paint chain.
paintComponent
should be protected
not public
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}