Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
motress zlting
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our UI forum.
 
Michael Dunn
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 ?
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • 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...

 
pete stein
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic