Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How can i use mouse to click on an image and draw a point after i clicked ?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i use mouse to click on an image and draw a point(example a red dot) after i clicked ?
I also need to save and show the coordinate where i clicked.
 
Sheriff
Posts: 22004
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our UI forum.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your mousePressed/Clicked code:
you update some variables with the coordinates from the mouseEvent
call repaint()

in paintComponent:
draw the red dot using the info from the (above) variables

> I also need to save and show the coordinate where i clicked.
save where? in a file - you should be able to do that
show where? a JLabel? - just update the label's text with the coords (can be done from mousePressed/Clicked)
 
motress zlting
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Dunn wrote:in your mousePressed/Clicked code:
you update some variables with the coordinates from the mouseEvent
call repaint()

in paintComponent:
draw the red dot using the info from the (above) variables

> I also need to save and show the coordinate where i clicked.
save where? in a file - you should be able to do that
show where? a JLabel? - just update the label's text with the coords (can be done from mousePressed/Clicked)



I still cannot draw a red dot on image after i clicked .... Can tell me where i supposed to add the coding and what coding suppose to add ?
 
Rancher
Posts: 3205
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Custom Painting Approaches shows a couple of different ways. It draws a Rectangle but you can change it to paint a dot. It draws on a JPanel but you can change it to draw on a JLabel with an ImageIcon.
 
motress zlting
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Custom Painting Approaches shows a couple of different ways. It draws a Rectangle but you can change it to paint a dot. It draws on a JPanel but you can change it to draw on a JLabel with an ImageIcon.



But, for first time i clicked on image, it get coordinate and draw a red dot, for the second times after i clicked, the first dot should remain at there...
I don't don know where should i place those coding......
After i moved my mouse, that dot dissapear....
Help me take a look at my coding....Thanks...

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to draw more than one "dot" then you need to use a collection of "dots" or perhaps Points. Consider creating an ArrayList<Point> and adding a Point to this list each time the mouse is pressed. Then in your paintComponent method iterate through the list of Points painting them as you go.
 
No holds barred. And no bars holed. Except this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic