-
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
-
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.
-
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
-
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.
-
Ah, right.
I'd misunderstood you.
I'll try that, it should work.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks