Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Let’s hook up an event to a change in our drawing panel. We’ll make the circle change colors each time you click the button. Here’s how the program flows:
The frame is built with the two widgets (your drawing panel and a button). A listener is created and registered with the button. Then the frame is displayed and it just waits for the user to click.
The user clicks the button and the button creates an event object and calls the listener’s event handler.
The event handler calls repaint() on the frame. The system calls paintComponent() on the drawing panel.
Voila! A new color is painted because paintComponent() runs again, filling the circle with a random color.