protected void dispatch_event () {
    event = display.next_event ();

    switch (event.code ()) {
    case gnu.x11.event.ButtonPress.CODE:
      exit ();
      break;

    case ClientMessage.CODE:
      if (((ClientMessage) event).delete_window ()) exit ();
      break;

    case Expose.CODE:
      if (((Expose) event).count () == 0) paint ();
      break;
	
    case KeyPress.CODE: {
      KeyPress e = (KeyPress) event;
	
      int keycode = e.detail ();
      int keystate = e.state ();
      int keysym = display.input.keycode_to_keysym (keycode, keystate);

      if (keysym == 'q' || keysym == 'Q' 
        || keysym == gnu.x11.keysym.Misc.ESCAPE) exit ();
      break;
    }
    }
  }