mouse problem

hi, i have created a game, which works except for the mouse,

basically I want the user to be able to click on a tile or location on my screen so I can add functionallity to my game.

my game world is held inside a 2d array of integers, who’s values corrispond to what 20x20 tile image will be printed to the screen.

the following shows a small part of how an image is printed to the screen, if the integer at the current place in the 2d array equals it.

for (int i = 0; i < 28; i++) {
			for (int j = 0; j < 38; j++) {
				
				
				
				Junctions.updateLights(i, j);
				
				if(gridArray[i][j] == 1) {
                                       // k and l == 20, so the spaces in the area are drawn 20x20 pixels apart.
					g.drawImage(cacheImages.getImage("straight1.gif"), k * j, l * i ,this);

                   }
}
				} 

I’ve added mouse listener, but despite trying, my current progress is only to retrieve the current mouses position:

public void mouseClicked(MouseEvent e) {	
		
		 
		mouseX = e.getX();
		mouseY = e.getY();

	}
	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}

	public void mousePressed(MouseEvent e) 
		
	}
	public void mouseReleased(MouseEvent e) {

		
	}

any help on how i could detect whether a part of the screen, preferably a place in the array or a tile has been clicked

thanks

what I can think of now is creating a Rectangles as you create images with same coordinates and dimensions. Later you can test if mouse click (x,y) are on specific rectangles by calling some_rect.contains(x,y). contains() actually works for all Shapes

so before each image is created create a rectangle like:

Shape rect1 = new Rectangle2D.Float(k * j, l * i, 20, 20);

?

as i only want certain images for this to be applicable to

yes, but the problem is at some point you’ll need to loop through all the rectangles and test if mouse click is on them, so I guess you’ll need put those rectangles you create in array(list).

Hi,
From your code I can say that your grid is homogeneous, and without rectangles overlapping.
So you can find which rectangle has been clicked without checking all of them
With this code:

int mouseI = mouseX%k;
int mouseJ = mouseY%l;
if(gridArray[mouseI][mouseJ] == 1){
  //Do your work here
}

You can get the exact tile where the mouse was clicked/pressed.

Rafael.-

nice rdcarvallo
sorry blindman I didn’t pay attention, I just saw you are drawing pics and need to detect clicks on them :slight_smile: Of course if you know and have stored exactly where pics are drawn then you don’t have to check them all, just the one with that coords (and you know how to reach it).

Modulo? :slight_smile:

mx=(mousex)/CELL_SIZE;
my=(mousey)/CELL_SIZE;

if(map[mx][my]==whatever){…}

Right!! It’s integer division, not modulo. (Just a typo :-[)

Rafael.-

right, awsome, thanks

I will try and implement it, and will tell you if im successful.

sorry, even though Im generally quite alright at programming, I have never programmed a gui type program/game, before, so am a ‘newless clubies’ in this respect

sorry for sounding thick, but I’m having trouble of where to put this, do i put it in the 'public void mouseClicked(MouseEvent e) ’ or in the array loop that is showed in the first chunck of code i pasted at the top.

thanks for helping so far though :slight_smile:

you’re missing some essential knowlage in programming (logic). You should put it where you need it to be. Ask yourself what do you need and then imagine in what should be done to acomplish this, where is the beggining, what should follow and how should it end. I will try to awser to your question now, but you won’t learn anything if we do the logic for you.

It seems you need something done with tile when you click on it. You should put it then in mouseClicked or MousePressed where you determine what tile has been clicked on and then do something with it…