Image Listeners?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Image Listeners?

  1. #1
    Join Date
    May 2005
    Posts
    22

    Image Listeners?

    Is it possible to add a listener to an image so that it will pick up mouse clicks within the bounds of the image?

    Either that, or can you overlay a 'transparent' component over an image so that it will act as a listener over that graphic?

    I can't work out if this is possible, and moreso how to do it in java

  2. #2
    Join Date
    Jul 2005
    Posts
    4
    You should look into loading the image as a JButton.

    For example:

    //where the argument for the ImageIcon constructor is just the filepath
    //to the image that you want to load.
    ImageIcon big_world = new ImageIcon ("usable map 5 (2).JPG");
    JButton myWorldButton = new JButton (big_world);

    The JButton will display the image, and you can then add a listener to the JButton.

    If you've never used JButtons before, you should also look into Layout managers, otherwise they might not work how you expect them to. Good luck.

  3. #3
    Join Date
    May 2005
    Posts
    22
    I possibly phrased that badly, I need something that will track where the mouse is clicked on an image, for arguments sake, if it was split up in four, the listener would take the mouseclick coordinates and then work out which coordinate the pointer is in

  4. #4
    Join Date
    Jul 2005
    Posts
    4
    JButtons allow you to addMouseActionListener's to them. The MouseActionListener that you build must have a MouseClicked(MouseEvent e)
    function.
    MouseEvent should give you the methods getX() and getY() which should give you the coordinates of the cursor when its clicked. I'm not exactly sure what you're looking to do, but you should be able to implement different behaviors based on the cursor's location when the mouse was clicked.
    That's the best that I can do for you, sorry.

  5. #5
    Join Date
    May 2005
    Posts
    22
    Ah, right.
    I'd misunderstood you.

    I'll try that, it should work.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If this doesn't fit your requirements you could implement it as a simple JPanel extension
    that loads and displays images using a filepath/url and supply it with a MouseListener
    and MouseMotionListener.

    If its an option for you then let me know, I have the code.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center