aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Drawing a line with two clicks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing a line with two clicks" Watch "Drawing a line with two clicks" New topic
Author

Drawing a line with two clicks

Terry Flint
Greenhorn

Joined: Sep 01, 2013
Posts: 20
I'm having difficulty figuring out how to draw a line with two mouse clicks. Every bit of sample code I've seen so far seems to either use a click and drag method, or create one long zig zag going through various selected points. I'm pretty sure it's a matter of knowing how to use the appropriate mouseListeners, but I can never seem to modify them in such a way as to get what I want.

Below is the code I've been trying to modify, which creates one line at a time using a click and drag approach. I've tried to change it in various ways, such as changing mousePressed to mouseClicked or adding in another MouseListener, and various similar approaches. I need some help with this.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
You want two clicks and to draw a line between the two points. You only need the mouseClicked method, which will give you a location relative to the panel. So, you put that location in an array, List or whatever. Then you repeat the procedure. Then you need to work out whether you have pairs of points which you can use to draw lines between. You might do well to create Line2D objects, and you can read about them under “primitives” in the Java Tutorials.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

I have heard it is better to use mouse released rather than mouse clicked so I would do it this way
private int click = 0;

on mouse released I would:
if click = 0 then increment it and save the point somewhere, else get the end point and draw the line between the two points and set click back to 0

it should work


SCJP
Visit my download page
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 700
    
  11
Randall Twede wrote:I have heard it is better to use mouse released rather than mouse clicked (...)

Did you also hear the reason why thay would be better?

When clicking, you have the possibility to aim the mouse accurately before the click, but there's always the chance that
you move the mouse slightly between a click and a release. Anyway, both methods work, of course.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
Have you made any progress with your line?
Terry Flint
Greenhorn

Joined: Sep 01, 2013
Posts: 20
Campbell Ritchie wrote:Have you made any progress with your line?


How pathetic is it that I still can't answer this in the affirmative?

I've found examples of what I was looking for that I was able to tweak, but I never managed to adjust this particular piece of code the way I wanted to.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
Please show us a simple short example of what you are trying.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2226
    
    8
Custom painting is done by overriding the paintComponent() method, not the paint() method. Why are you using paint()?

Read the section from the Swing tutorial on http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html for more information and examples. The examples will also show you the proper way to create the GUI by making sure all the code executes on the Event Dispatch Thread.
Terry Flint
Greenhorn

Joined: Sep 01, 2013
Posts: 20
Campbell Ritchie wrote:Please show us a simple short example of what you are trying.


Well, for starters, here is an example of how I misunderstood what Randall was saying:



Terry Flint
Greenhorn

Joined: Sep 01, 2013
Posts: 20
Okay, so that's not exactly a short example, but I was afraid of losing context, since the changes aren't just in one place.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
I think you are right the way you are recording locations of clicks.
If you add the points to a List<Point>, you don't need to bother about whether it is the first click or not.
Create a Line class (if that name isn't in use already) with start and end Points.
Iterate the List using the pairs of points to create lines.
Decide what to do if there are an odd number of Points in the List.
You may need a Listener to decide whether the List is empty or not, and you might need a button to get all the lines drawn.
Don't use paint(). Override paintComponent() but should have protected access and its first line needs to read super.paintComponent(g);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drawing a line with two clicks