Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  ImageMap - OnMouseOver and OnMouseClicked  (Read 1439 times)
0 Members and 1 Guest are viewing this topic.
Offline error777

Senior Newbie





« Posted 2009-03-08 20:42:36 »

Hello,

I'm quite new here and this is my first post i think.
I've looked at lots of topics in this forum and I must say that is the best one that could probably answer my question and help me.

I must make a sort of ImageMap like in HTML or JavaScript.

Some Examples:

http://en.wikipedia.org/wiki/File:Map_of_USA_with_state_names.svg

and

http://www.elated.com/articles/creating-image-maps/

but I can't find out how do do this.
I searched a lot but unsuccessfuly.

What I need?

Well I'm in a small game development where you have to klick around quite a lot.
And for the map i already have the images but I have to put them together give on each of those an MouseListener and see when the mouse is over an image is should highlight (probably a second image will take this role) and if the image is klicked it should perform the next action (e.g. System.out.println("Image klicked!"); or something)

my biggest problem is that i have to give all these images together so that is looks like a map seen from above (2D).
And then the second problem is my MouseListener. how to put it on a image.

I hope you can give me some advice and/or code.
Thank you,

Joe
Offline Xyle
« Reply #1 - Posted 2009-03-09 04:16:46 »

I used this kind of enviroment for my hearts game. I used an applet which implements Runnable only, just for using threads. As for the mouse, I capture when the user clicks or moves the mouse and test what the user is clicking or moving the mouse over with 2 methods...

This captures what the user clicks, I stripped it down, but shows the gist of what Im using it for. I have a button painted at the coords listed in the method. If the user clicks between the coords, a button sound is played, and the start screen flag is changed to 0, which tells the paint method not to paint the start screen. Its a deprecated method, but I like it.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public boolean mouseDown(Event e, int x, int y) { //Deprecated
     mX = x;
      mY = y;

   if(startScreen==1){
      if(mX >= 285 && mX <= 385 && mY >= 300 && mY <= 350){
         butClick.play();
         startScreen = 0;
         return true;
      }
   }
   else{
      return false;
   }
}


This method constantly checks where the mouse is, if the mouse is over a button, then the hover flag is set to 1, telling the paint method to paint the hover effect on the button and to play the hover effect sound.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public boolean mouseMove(Event evt,int x,int y){
    mX = x;
    mY = y;
    if(mX > 0 && mY > 0){
       if( startScreen == 1){
            if(mX >= 285 && mX <= 385 && mY >= 300 && mY <= 350){
            if(startHoverFlag == 0){
               startHoverFlag = 1;
               snd_Hover.play();
            }
            }
            else{
               startHoverFlag = 0;
            }
       }
    }
    else{
       return false;
    }
}


Hopefully this will help you, or someone else can point out any problems they forsee with the above code.


Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline error777

Senior Newbie





« Reply #2 - Posted 2009-03-09 17:10:50 »

Thank you for the response.

Your methods brought me on one idea and combined with mine I almost finished what I wanted.

Panel.java

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
import ...;

@SuppressWarnings("serial")
public class Panel extends JPanel implements MouseListener,MouseMotionListener {
   
   private BufferedImage foreground, background;
   
   public Panel() {
      try {
         foreground = ImageIO.read(new File("foreground.png"));
         background = ImageIO.read(new File("background.png"));
      } catch (IOException e) {
         System.out.println("Couldn't read images!");
      }
   }
   
   public void paintComponent(Graphics g) {
      g.drawImage(foreground, 0, 0, null);
   }

   public void mouseClicked(MouseEvent e) {
      System.out.println("Mouse Clicked");
     
      Point p = e.getPoint();
     
      int i = background.getRGB((int)p.getX(),(int)p.getY());
      Color c = new Color(i);
     
      System.out.println("R: "+c.getRed()+" B: "+c.getBlue()+" G: "+c.getGreen());
     
   }

   public void mouseMoved(MouseEvent e) {
     
   }
   
   public void mouseEntered(MouseEvent e)    {}
   public void mouseExited(MouseEvent e)     {}
   public void mousePressed(MouseEvent e)    {}
   public void mouseReleased(MouseEvent e)   {}
   public void mouseDragged(MouseEvent arg0) {}
}


Main.java

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
import ...;

public class Main {

   private static JFrame mainFrame = new JFrame("Test Image Map");
   
   public static void main(String[] args) {
      Panel p = new Panel();
      p.addMouseListener(p);
      p.addMouseMotionListener(p);
     
      mainFrame.getContentPane().add(p);
      //mainFrame.setPreferredSize(new Dimension(570,570));
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainFrame.setVisible(true);
      mainFrame.pack();
   }
}


As you see in my code I have two Images.
The foreground I paint it into my Panel extended by JPanel.
So if the user Clicks somewhere in the image it checks witch Color the Background image has.
From there I can do anything.
The same thing I must use on the highlighter thing but with more images.

Images that I used are in the attchment.

Thank you.


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.

Riven (12 views)
2014-07-29 18:09:19

Riven (9 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (26 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!