The reason you are creating a custom component is to do custom painting of a Rectangle. The size of the Rectangle that you want to paint becomes a property of your component. Therefore your component should also be responsible for determining the proper size of the component, which would be based on the size of the Rectangle you want to paint. So you override the getPreferredSize()
method to make sure the component paints properly.
For example if you are painting a Rectangle like the Rectangle in you last question:
Rectangle rect = new Rectangle(50,50,50,50);
then the preferred size should probably be (150, 150) so that the Rectangle has equally sized border around the entire component.
If you leave this to the programmer using your component they may do something like:
somePanel.setPreferredSize( new Dimension(50, 50) );
Now the component will not paint properly because the programmer just used a random size.