You haven't added any mouse listener to the panel. You need to add this line:
this.addMouseListener(this);
(it would be cleaner if the panel itself was not a mouse listener, and if you used a separate, inner class for the listener).
Moreover, the mouse listener changes the value of i
, but doesn't ask the panel to repaint itself. So you need this line in the mouseEntered()
and mouseExited()
methods:
repaint();
Also, note that the main method should use SwingUtilities.invokeLater()
to construct and display the frame in the event disptach thread, and that you shouldn't override update()
.