Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Picking the color under the mouse cursor on a JPanel

 
Andre Bandarra
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your link and found this in the console

After refreshing the page two of these came up
 
Andre Bandarra
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, i used a BufferedInputStream on the Applet now... It will probably work better. Try it, please...
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both applets load okay now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic