Hi all,

I am trying to make a simple game of Craps in Java. My idea was to have a JFrame with an image of the Craps betting table as the background image and when the player clicks on a section of the table they can place or remove a bet on that part of the table.

My question is, when the user clicks, what is the best way to determine what part of the image has been clicked? Obviously I need a MouseListener to determine where in the JFrame the click has occurred, but how can I transfer this to the position on the image? I am not looking for code, just a plan of attack really, so any thoughts would be appreciated.

Thanks in advance for any help on this,
darkagn :)

There are three methods in MouseEvent which could be of particular help, getX(), getY() and getPoint(), getPoint returns the X,Y position relative to the source component while getX() returns the X coordinate and getY() returns the Y coordinate.

Now what you could do is, divide the image in sections for bets and then on the mouse click get the coordinates from which you could decide what section of the image has been clicked on and hence what part of it has the bet been placed on.

commented: Good idea to section off the image, thanks :) +3

I like verruckt24's idea. You have an image and certain parts of that image represent different "sections" (i.e. $10 bet section, $50 bet section, etc.). You need to have a mapping from the (x,y) coordinate to the section. Depending on how those sections are mapped out, you may want to perhaps take advantage of Java's Shape classes? For example, if the shape is a rectangle, create a Rectangle object. If it's a polygon, create a Polygon object. You can then use the contains method for these objects to see if they have been clicked, so there is less math for you to figure out yourself and code.

commented: Good extension of what was already said, you have provided a great time saving technique, thanks! :) +3

So you're saying to create a Rectangle, for example, that does not appear at all on his GUI? Then he can set the position of this invisible rectangle and see if the user clicks there with contains?

So you're saying to create a Rectangle, for example, that does not appear at all on his GUI? Then he can set the position of this invisible rectangle and see if the user clicks there with contains?

Yes. For example if the $10 bet section is bounded by a range from (100, 100) to (200, 200), create a Rectangle with a top left corner of (100, 100) and a width and height of 100, don't display it in the paint function, but use it to determine whether the (x, y) coordinates clicked by the user are in the range of (100, 100) to (200, 200) using the Rectangle class's contains method.

Yeah, I understand. My advice would've been to do all the math by hand and see if the x and y values fall within the desired range. But that works better (if it does actually work).

:)

Hi all,

Thanks for the great suggestions everyone, I think you have given me some great ideas on how to determine where in the image the player has clicked. Just on the idea of creating shapes on the image, any idea how to tell the coordinates of certain areas in an image? Is there a program that can tell me where in an image I am if I point to it with the mouse for example, or is it a trial and error type of deal?

Thanks again to everyone for your input. Have a great day! :)

Hi all,

Thanks for the great suggestions everyone, I think you have given me some great ideas on how to determine where in the image the player has clicked. Just on the idea of creating shapes on the image, any idea how to tell the coordinates of certain areas in an image? Is there a program that can tell me where in an image I am if I point to it with the mouse for example, or is it a trial and error type of deal?

Thanks again to everyone for your input. Have a great day! :)

If you are drawing the image onto, say, a JPanel, and you are adding your MouseListener to that JPanel, in your call to the drawImage function, you are going to specify an upper-left corner of the image. So if the upper left corner of the image is to be placed at coordinates (50, 50) and some point inside the image that is (70, 70) from the top left of the image would be (50 + 70, 50 + 70) = (120, 120) relative to the upper left of the JPanel, so if getX and getY return (120, 120), then that's mapping to (70, 70) relative to the upper left corner of the image, so no guesswork and trial and error is involved presuming that you already know that (70, 70) relative to the upper left corner of the image is, say, inside the $10 bet section. If the question is how to come up with the (70, 70) relative to the image, I imagine you could write a quick and dirty program where you move the mouse around and have it display the x and y coordinates with System.out.println commands and decide where things start and stop, so I guess that'd be trial and error/paper and pencil, and it wouldn't even have to be within a Java program. Perhaps some simple Photo editing software where you drag a mouse and eyeball where everything starts and stops and write it down. That's how I'd do it at least.

And you can simply display the Rectangle (or some other Shape) in the paint function (take the display code out later) to see exactly where it is and change it as needed, so I guess that'd be a trial-and-error method, but a fairly quick one.

Ah that's great, will give it a try. Thanks again VD! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.