Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to make image rollover in awt.

 
nima lama
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell me how can i make image rollover in java awt. i mean when a cursor comes over the image the image will change into another and when left, restore to original.
can i give action command on the image so that it can perform some work. if so, how does it happen?
thank you.
waiting for reply.
 
Amit Agarwal
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi nima,
the image itself cannot listen to mouse events, so you need to add the mouse listener to the panel / applet that holds the image. you need to override the mouseEntered() and mouseExited() methods of the MouseListener class. since Applet extends from Panel, you can associate the mouselistener with the applet itsel. if the image is just a part of your applet, you can add it to the panel that holds the image.
You will have to declare a boolean variable in your class say mouseinside. whenever your mouse enters / exits the panel area set mouseinside to true / false and call a repaint() for the panel /applet.
override the paint() function of the panel / applet and depending on the value of the mouseinside variable draw the required image.
for mouse click use the normal mouseClicked() method for the panel. also, keep in mind that the slightest mouse movement when clicking will result in a mouse press - drag -release sequence of events and not a mouse click. So, to be on a safer side you can even use the mousePressed() for this.
I think that this should solve your problem.
Best wishes,
Amit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic