aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Picking the color under the mouse cursor on a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Picking the color under the mouse cursor on a JPanel" Watch "Picking the color under the mouse cursor on a JPanel" New topic
Author

Picking the color under the mouse cursor on a JPanel

Andre Bandarra
Greenhorn

Joined: Jun 07, 2005
Posts: 17
Thats it.
I'm using a panel to draw a colored map. Each area of the map has a different color. And i want to get the color under the mouse cursor when the mouse moves so i can know over what area the mouse is over.

So i made a MouseMotionListener and when the mouse moves i'm able to get the point the mouse cursor is over. But i'm stuck trying to find out which color is at that point

If you want to get the idea of what i am doing, my work-in-progress is at this page (requires JRE 5.0)

http://www.bandarra.org/applets/JMapperApplet.html

Can anybody help me?


SCJA | SCJP 5.0 | SCJD | SCWCD
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I tried your link and found this in the console

After refreshing the page two of these came up
Andre Bandarra
Greenhorn

Joined: Jun 07, 2005
Posts: 17
I hadn�t seen the first error yet...

The second one happens when i try to reload the page under my Linux box. Things go fine on Windows.

Im making a MapPanel that can be used on an Applet or Desktop application... So the applet is just for testing purpose.

I think the problem is with the stream that downloads the map... On both cases.

Anyway... any idea how can i do to pick the color under the mouse cursor?
Andre Bandarra
Greenhorn

Joined: Jun 07, 2005
Posts: 17
Well, i used a BufferedInputStream on the Applet now... It will probably work better. Try it, please...
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Got the 'NegativeArraySizeException's again just before the progress bar go to the end. If you are using a BufferedImage you can use the 'getRGB(int x, int y)' method to find the color. The older PixelGrabber class (api has code example) allows this with the Image type.
Andre Bandarra
Greenhorn

Joined: Jun 07, 2005
Posts: 17
Damn... I�ve tested at my windows box here, at work and at a friends Linux box, both of them with JDK 5.0 and things went fine...

Whats your box? Whata JRE version are you using?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
win xp professional version 2002 sp2
C:\jexp>java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)
Andre Bandarra
Greenhorn

Joined: Jun 07, 2005
Posts: 17
Are you behind a proxy or something??

By the way, thanks for the BufferedImage tip. I was drawing directly into the JPanel. I used the BufferedImage for the colormap and it worked...
Andre Bandarra
Greenhorn

Joined: Jun 07, 2005
Posts: 17
Could somebody else test my applet and tell me if its working?
It doesnt works for Craig... At home, under Linux it works but gives me an exception when i try to reload the applet. I think it has to do with resource caching...

Under Windows, here at work, it works perfectly, reloading and all
Brian Duff
Greenhorn

Joined: Aug 16, 2001
Posts: 22
Works ok for me. No exceptions in the Java Console, only the word "init" printed each time I refresh.

Using Firefox 1.0.6 with Java 1.6 (1.6.0-ea-b35).

Brian
Andre Bandarra
Greenhorn

Joined: Jun 07, 2005
Posts: 17
Thanks Brian!
The init is supposed to appear...
That applet is giving me trouble on some random machines, and i cant find why...
Andre Bandarra
Greenhorn

Joined: Jun 07, 2005
Posts: 17
Originally posted by Craig Wood:
win xp professional version 2002 sp2
C:\jexp>java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)


Hey, Craig! Think i nailed the problem! could you test the link again? Everyone else, could you test it for me

http://www.bandarra.org/applets
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Both applets load okay now.
 
Don't get me started about those stupid light bulbs.
 
subject: Picking the color under the mouse cursor on a JPanel