Method paintComponent

Every JPanel, including our DrawPanel, has a paintComponent method (lines 9–22), which the system automatically calls every time it needs to display the DrawPanel. Method paintComponent must be declared as shown in line 9—otherwise, the system will not call it. This method is called when a JPanel is first displayed on the screen, when it’s covered then uncovered by a window on the screen, and when the window in which it appears is resized. Method paintComponent requires one argument, a Graphics object, that’s provided by the system when it calls paintComponent. This Graphics object is used to draw lines, rectangles, ovals and other graphics.

The first statement in every paintComponent method you create should always be ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.