I agree with all andbin says, but I also accept that you may have created this odd construction just as a simple example, and it's useful. It shows that the
order in which you do things
matters. For example, try this instead:
public void setUp()
{
JFrame f = new JFrame("..");
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.setVisible(true);
}
It also demonstrates that it's useful to understand how Swing's painting mechanism works at a deeper level which you can explore by completing the
Custom Painting Tutorial.