Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Detecting when mouse is a over an image  (Read 2308 times)
0 Members and 1 Guest are viewing this topic.
Offline xuan

Junior Newbie





« Posted 2007-11-29 10:20:46 »

Hi, I want to make a board games, and so selecting caracters on it. How can I detect when my mouse is over the .gif image of my char (only whith no transparent pix) something looking like mouseEntered but on a weird random pix area.

Hope I made myself clear (sorry for my english)
Offline turquoise3232

Junior Devvie




Java (games) rock!


« Reply #1 - Posted 2007-11-29 19:11:54 »

Hi,
I think that you should use mouseMoved callback and check yourself if the mouse position is in or out your "weird" zone.
Offline xuan

Junior Newbie





« Reply #2 - Posted 2007-11-30 07:22:18 »

Hi again, I finally managed to solve my pb by picking the color under my mouse ( thanx to this topic http://www.java-gaming.org/forums/index.php?topic=12780.0, sorry I should have search better before posting :p), i'll select a char  by clicking on the cell he's standing on,. It took me time to map my board ith a different color on each cell but it works well now Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #3 - Posted 2007-11-30 09:45:14 »

maybe a good way would be create a canvas, same size as your image and draw your image on this canvas graphics than add a mouslistener to this canvas. so no complexe programming as(if(imouse>imagesx && etc ....) then .... )

something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
MyImage extends Canvas
{
 Image image;
 MyImage(Image i)
 {
  this.image=i;
 }
 public void paint(Graphic g)
 {
  this.update(g);
 }
 public void update(Graphics g)
 {
  g.drawImage(this.image,0,0,null);
 }
}


than you can use something like that:
1  
2  
3  
MyImage i =new MyImage(anImage);
i.addMouseListener(this);
this.add(i);

Offline xuan

Junior Newbie





« Reply #4 - Posted 2007-11-30 10:09:42 »

The problem is that i'm using hex for the cases, so no way using a basic grid.
My solution is :
- An ugly .gif with different colors on each cases.
- A nice .jpg image I put in front of the other one
I get the coord of each case by simply picking  the the Blue and Red RGB value on the .gif => gives me X and Y position

it gives something like this :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   public void paint( Graphics g ) {
      g.drawImage(land.getLandImageMap(), 10 , 20, this ); //the ugly gif
      g.drawImage(land.getLandImage(), 10 , 20, this );  //the pretty jpg
   }
      public void mousePressed( MouseEvent e ) {
         try
            {
            int pcolor = land.getLandImageMap().getRGB(e.getX()-10,e.getY()-20);
            Color c = new Color(pcolor);      
           
            System.out.println("("+(c.getRed())+";"+(c.getBlue())+")");
            }
            catch(Exception ex)
            {
               System.out.println("out of image bounds");
            }
      }

the color of the cases on the .gif looks the same but they are not (+1bleu  for x and +1red for y)
Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #5 - Posted 2007-11-30 15:59:08 »

Given that your grid looks to have been created by a 3d render, if you knew the parameters of the camera with which the projected image was constructed you could mathematically compute the grid tile under the mouse cursor.

It's fairly complicated maths, but would probably be the most future-proof solution.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline xuan

Junior Newbie





« Reply #6 - Posted 2007-11-30 16:55:15 »

I'm only trying to do a small game atm. But maybe if I want to do a bigger one later i'll try this way.
Thanx for ur replies
Offline DzzD
« Reply #7 - Posted 2007-11-30 17:10:41 »

Given that your grid looks to have been created by a 3d render, if you knew the parameters of the camera with which the projected image was constructed you could mathematically compute the grid tile under the mouse cursor.

It's fairly complicated maths, but would probably be the most future-proof solution.

yep this is a nice solution, but you should be able to achieve this without knowing about camera by using the 2d pos of all your 6 points that make a polygone embeding your grid , than knowing this 6 pos and mouse pos you should be able to compute coordinate on grid. no time right now but I will post more information on how to achieve this later (in reallity you only need four point) , interresting stuff, just give me some minutes I will post explanation.

EDIT:


EDIT2:
hum there is a little mistake on last line computing xscalescreen
xscalescreen=xscale0-((xscale0-xscale)*mousey/fary)

Offline DzzD
« Reply #8 - Posted 2007-11-30 18:03:28 »

note that this technic can be applied to any 3d scene it enable retriving uv coordinate without knowing anything about it than three points and there corresponding uv coordinates. than you can know any other point uv from the scene directly with a simple afine tranform

I will try to share such source code soon as it seem an interresting tools to do. may help in extracting map from arbitrary 3d screenshot or pick an uv without cpu cost in a 3d scene, thanks for your question!

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #9 - Posted 2007-11-30 18:10:55 »

Quote
My solution is :
- An ugly .gif with different colors on each cases.
- A nice .jpg image I put in front of the other one

It's actually not such a bad solution at all, but you don't have to draw the 'ugly' gif, do you? You could just get the color from the offscreen gif. The pixels then don't have to be a matching color, but could just be indexes to the tiles.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline xuan

Junior Newbie





« Reply #10 - Posted 2007-12-01 07:28:36 »

Well thank you all, especially DzzD. For the moment I really want to put a char on my grid and move it, but once it's done, i'll dig in your way to make random grid, wih high ground for example.
Quote
You could just get the color from the offscreen gif
you right ^^
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (23 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (68 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!