0

So my question is, given a graphic (image) in Java which I place within a panel etc. how could I (if i can at all) store certain positions on the graphic then during run time draw something on the graphic at the positions i pre-defined.

e.g. I have a map, I programmatically define certain spots on the map (using the x and y coordinate of the point of the graphic) then during runtime say the user selected as place on the map e.g. a shop, a pointer is drawn onto the point pre-defined as the shops location.

I am trying to keep things simple so I dont need some complex implementation which uses Google Maps API etc.

I am not sure on how to do this in Java, any guidance would be greatly appreciated.

Thank you!

P.S This question has been marked as duplicate by one of the moderators, here is how it is different.

I am not sure how to store the specific coordinates of the certain locations within the graphic rather than drawing things with the mouse. So if the shop is at point (45,89), how would I go about drawing a image at point (45,89).

Thanks again.

User59
  • 487
  • 4
  • 19
  • 1
    Short answer, yes. Long answer, you need to separate some concepts (you have about 5 questions in there). First, you have a physical world (the map), you have a virtual world (the coordinates). You can use a `MouseListener`/`MouseMotionListener` to monitor the user's interactions and if they tap on the map, you can check if the location is within the bounds of a coordinate within the "virtual" world, if it is, you can overlay another image on top of the "physical" world through what ever rendering solution are you using – MadProgrammer Feb 10 '17 at 23:56
  • @MadProgrammer How would I define a set of coordinates of a point on the graphic as being the shop (or other locations)? – User59 Feb 11 '17 at 00:04
  • Lot so of ways, depending on you needs, me, personal, I'd probably use a `List` of `Rectangle`s, this will provide a clicked area "around" the point that you're interested in – MadProgrammer Feb 11 '17 at 00:06
  • @MadProgrammer Thanks! You have opened up a few paths to explore. – User59 Feb 11 '17 at 00:11
  • `I am not sure how to store the specific coordinates of the certain locations within the graphic rather than drawing things with the mouse.` - See the `Draw on Component` example from [Custom Painting Approaches](https://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/). Yes the example uses a mouse to define the rectangles, but that is not important. What is important is the `addRectangle(...)` method which you can manually invoke to define the Rectangles to be painted. – camickr Feb 11 '17 at 00:13

0 Answers0