Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with draw line on mouse click  RSS feed

 
supriya riya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi my code is to draw lines on mouse click between mouse click and release.Though i am able to draw lines but ,previously drawn lines are also shown on JPanel.I want only recently drawn line to be shown.So somebody help me on this where should i make changes in my code to meet my requirement.

And my jpanel does not show the black line border which i have added in setBorder method of JPanel.
Suggestions will be helpfull.
thanks.


 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need call super.paint(g) first. If you need to draw multiple lines you'll need to store those, in a List or something.

A side note: if you copy the code from mouseReleased to mouseDragged (or simply call mouseReleased(e) from mouseDragged) you can see your line being drawn as you drag your mouse over the panel.
 
supriya riya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll be carefull about using the same thread.

I have used paintComponent method, and super.paintComponent(g) in first line of paintComponent
Still my deleted lines appear on jpanel.
I want to delete a drawn line on mouse click,i am writing code in mouseClicked() for deleting,i have used points.remove() to remove clicked coordinates that match the points vector storing all the coordinates,and hence line does not get drawn.
It works fine for single line,but when i need to show all drawn lines and when i click on particular end point of a particular line it must get deleted
It shows "clicked" in console,but changes are not seen in JPanel.It even deletes the coordinate from points vector.
Thats where i am not getting what is going wrong,i have done all changes that have been suggested ........

so any more suggestions about where i am going wrong in my code?
thanks

 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A mouse click triggers three events:
- mousePressed
- mouseReleased
- mouseClicked

Now when you only click, it will add sPoint, add ePoint and then remove only one of them, but never both. This is because of how you remove elements.

Consider only a single click, at (42,42). mousePressed adds point (42,42) to points. mouseReleased adds point (42,42) to points. points is now [(42,42), (42,42)]. mouseClicked then tries to delete:
- i == 0, points.size() == 2, p1 == (42,42); there is a match, and the first element is removed. points.size() becomes 1, and i becomes 1
- i == 1, points.size() == 1 => the loop ends

There are three ways of removing elements from a List (including Vector) while iterating.
1) Manually control the increment:

2) Loop backwards:

3) The much preferred way, use an Iterator:
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, when you remove one point, you must also remove its "buddy", or you will still get a problem; two points not related to each other all of a sudden become "buddies", and a line is drawn between them. Consider:

points = []
draw a line from (0,0) to (42,42); points = [(0,0), (42,42)]
click on (42,42); points = [(0,0)]
draw a line from (13,13) to (5,5); points = [(0,0), (13,13), (5,5)]
a line will now be drawn from (0,0) to (13,13), and (5,5) will be ignored.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!