Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clicked Image

 
kenji jonathan
Greenhorn
Posts: 4
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends, I want to ask how to make an image with graphics2d added with mouseListener.


I want to make an image, if it is clicked, something occur. here is my code :



how to make this image clickable?..thanks
 
Maneesh Godbole
Saloon Keeper
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hints:
1) You know the Rectangle which displays the image
2) MouseEvent has the capability to provide you the x and y co-ordinates of the mouse click
3) Rectangle can tell if if a particular Point is inside/outside that rectangle
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is pretty simple really. one of your classes(usually the one you are drawing on) needs to implement MouseListener(or extend MouseAdapter) and override the mouseClicked method. here is an example.



i didnt use mouseClicked in this example but it should give you the idea. you have to implement all the methods of MouseListener even if they do nothing.

i forgot to mention in your constructor you have to add the MouseListener. in the example it looks like this:
addMouseListener(this);

another suggestion is to use mousePressed instead of mouseClicked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic