LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-02-2005, 10:31 AM   #1
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Rep: Reputation: 50
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...)
 
Old 06-03-2005, 03:51 AM   #2
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Hi there,

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()));
}
Anyway, hope this helps.
 
Old 06-03-2005, 04:02 AM   #3
Mega Man X
Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 63
Hi!

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:

http://www.linuxquestions.org/questi...hreadid=141754

Good luck!

Last edited by Mega Man X; 06-03-2005 at 04:07 AM.
 
Old 06-03-2005, 03:03 PM   #4
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Original Poster
Rep: Reputation: 50
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.
 
Old 06-03-2005, 03:52 PM   #5
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Original Poster
Rep: Reputation: 50
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!

titanium_geek
 
Old 06-03-2005, 11:57 PM   #6
aaa
Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 46
Code:
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()));
}
 
Old 06-04-2005, 11:41 AM   #7
titanium_geek
Senior Member
 
Registered: May 2002
Location: Melbourne Australia
Distribution: it died/ macosx
Posts: 2,478

Original Poster
Rep: Reputation: 50
wow. thanks aaa. thanks for the code- I think I understand now.

titanium_geek
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display an image in C, fullscreen mic Programming 8 06-01-2008 09:57 AM
Image display in Evolution djmurray Linux - Newbie 1 10-08-2007 07:46 PM
howto mount image from g4l ? pewi Linux - General 0 11-25-2004 03:46 AM
howto modify the image on the screen after logging in?? rohan208 Linux - General 3 07-13-2004 11:48 PM
howto mount dvd image on slackware? carboncopy Slackware 3 10-25-2003 03:10 PM


All times are GMT -5. The time now is 04:42 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration