Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p><em>"But can i put a mouse listener on an Image??"</em></p> </blockquote> <p>No, or at least, not in the context you are thinking.</p> <p>You have a couple of choices depending on what you want to achieve.</p> <p>The basic requirement though, is you are going to need to know exactly where on the image each click point is. This is best achieved by using an image editing program to map out the "hot spots" and then code these into your program</p> <h2>You could...</h2> <p>Use a <code>JLabel</code> to render the board image and attach a <code>MouseListener</code> to it.</p> <p>The problem I would have with this is trying to figure out how to update the image with the player markers.</p> <h2>You could...</h2> <p>Use a <code>JPanel</code> and override it's <code>paintComponent</code> to render the image and player moves/markers.</p> <p>You would then add a <code>MouseListener</code> to it and monitor the mouse clicks from there.</p> <p>Regardless of which method you used, I would probably create a <code>List</code> of <code>Rectangle</code>s which represented the hot spots the user can click. Each time <code>mousePressed</code> is called, I would walk this list and use <code>Rectangle#contains(Point)</code>, passing the mouse click point, to determine which hot spot was clicked.</p> <p>You would then to to compare this with the game model to determine if it's a valid move or not and take appropriate action as required.</p> <p>Take a look at <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html" rel="nofollow">How to write a Mouse Listener</a> and <a href="http://docs.oracle.com/javase/tutorial/uiswing/painting/" rel="nofollow">Performing custom painting</a> for more details</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload