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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access