Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

12. Getting GUI: A Very Graphic Story > We can get an event. We can paint graph...

We can get an event. We can paint graphics. But can we paint graphics when we get an event?

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:

  1. 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.

    image with no caption
  2. The user clicks the button and the button creates an event object and calls the listener’s event handler.

    image with no caption
  3. The event handler calls repaint() on the frame. The system calls paintComponent() on the drawing panel.

  4. Voila! A new color is painted because paintComponent() runs again, filling the circle with a random color.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint