java - howto display an image and find mouse coords on it?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
java - howto display an image and find mouse coords on it?
ok- for my new project (miraMira, comming to a freshmeat near you!) which is in java, I need define "hot spots" for an imagemap.
After pulling in an image (I can do this I think. JFileChooser?) how do I display it? I think a JScrollerPane would be useful for holding the image. (the images should be 600x800 that are being pulled in.)
Ok, and then I have to look at the pixel location of where the mouse is clicked so that I can generate an image map. So, how do I get the location (coordinates) of the mouse? In a specific area, not the whole screen.
thanks.
I'm really excited about this project, which I plan to opensource and release on freshmeat. It's still in alpha stage- 0.01
titanium_geek
PS (and believe me, I have searched and searched...)
One way to display an image is by setting the icon property of a JLabel.
Code:
JLabel myImage = new JLabel(labelText, new ImageIcon("path/to/image.jpg"));
For more info and examples, do a search on google for ImageIcon + JLabel. Also check out the Java API: 1.4.2
To capture mouse clicks, you'll need to add a MouseListener to whatever component you want to capture the click for. It's similar to how you add button press actions to JButtons. The method you'll need to implent is mouseClicked. Lookup MouseAdapter to narrow down your code as well. The actual mouse position is retrieved through getPoint() from the MouseEvent.
Code:
public void mouseClicked(MouseEvent event) {
System.out.println(event.getPoint()));
}
I've tried to code a Java application a while ago to be some sort of image viewer, but never finished, as many of other my projects . Still, you may want to take a look into the code for displaying the image. I've no idea about the mouse position though:
thanks guys!!!! thats really (and I mean REALLY) useful.
thanks for the thread Megaman X. I know totaly what you said about the Sams book- good for an overview but nyeh. (The teachyourself html was WAY way better) I also have Sybex's 'mastering java' which has a completly bizzare way of organizing the topics (especially after Sams) but tells you more.
umm... so I get the name of a file when I do the whole "file chooser" thing (that looks totaly cool...) and use the label to change the image? (as per megaman's thread)
listeners: I'll add the mouseclick listener to the JLabel component? (did I read you right?)
well, thanks for being totally rad guys.
WOOOHOOO!!!
titanium_geek
(I'm graduating... in an insanely short time... finally will have time to program!!!)
Last edited by titanium_geek; 06-03-2005 at 03:53 PM.
umm... I'm having trouble with adding the image icon to the jlabel.
I don't want to set an absolute- it needs to be a variable - because the user needs to be able to select thier own photos/pictures so I need to be able to change the image in the label to just about anything. Help!
JLabel the_label = new JLabel("label");
JFileChooser jc = new JFileChooser();
int ret = jc.showOpenDialog(parent);
if (ret = JFileChooser.APPROVE_OPTION) {
the_label.setIcon(new ImageIcon(jc.getSelectedFile().toURL()));
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.