• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

clicked Image

 
Greenhorn
Posts: 4
Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic