File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drawing a line with two clicks

 
Terry Flint
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Piet Souris
Ranch Hand
Posts: 1099
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you made any progress with your line?
 
Terry Flint
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us a simple short example of what you are trying.
 
Rob Camick
Ranch Hand
Posts: 2476
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic