Re: How to Draw a 'Line'...
There are a number of ways to do this. Save the point when you click, save the point when you release, and draw a line between the points. Or when you drag, save the previous mouse position, then draw a line from the previous mouse position to the current, and set previous equal to current. Or just draw points wherever you drag.
Which approach you take (and there are still others) depends on the exact effect you're going for. What's wrong with the Line2D.Double class?
Re: How to Draw a 'Line'...
I moved this thread to the Java2D forum for closer topic alignment.
1 Attachment(s)
Re: How to Draw a 'Line'...
Quote:
Originally Posted by
KevinWorkman
There are a number of ways to do this. Save the point when you click, save the point when you release, and draw a line between the points. Or when you drag, save the previous mouse position, then draw a line from the previous mouse position to the current, and set previous equal to current. Or just draw points wherever you drag.
Which approach you take (and there are still others) depends on the exact effect you're going for. What's wrong with the Line2D.Double class?
It's not necessary drawing a straight line, which is one coordinate to another, it's giving the user the ability to draw where the user drags the mouse to make a line, any line, squiggly, zigzag, etc, like this picture below:
But I initially got it to work, the problem was I was declaring new Rectangle2D.Double objects as the mouse moves IN the paint class.. while I was initially supposed to do it out of the class, and then paint all the rectangles according to their coordinates afterwards.
Good thing is it's working now though, thanks for the assistance!
Re: How to Draw a 'Line'...
Quote:
Originally Posted by
CuppaCoffee
It's not necessary drawing a straight line, which is one coordinate to another, it's giving the user the ability to draw where the user drags the mouse to make a line, any line, squiggly, zigzag, etc, like this picture below:
But I initially got it to work, the problem was I was declaring new Rectangle2D.Double objects as the mouse moves IN the paint class.. while I was initially supposed to do it out of the class, and then paint all the rectangles according to their coordinates afterwards.
Right. My suggestions took that into account. I would think you'd be able to use Line2D.Double in the same way you're using Rectangle2D.Double now.
Re: How to Draw a 'Line'...
Quote:
Originally Posted by
KevinWorkman
I would think you'd be able to use Line2D.Double in the same way you're using Rectangle2D.Double now.
I would recommend Path2D.Double with quadTo(...) / curveTo(...). Takes care of rapid mouse movements that can cause breaks in the drawn line arising out of the sampling rate for MouseEvents (which is system dependent)
db
Re: How to Draw a 'Line'...
Could you explain how, or show an example involving how to use Path2D.Double objects? It's sounds like it'd probably cover the problems I'm likely to have to deal with my algorithm
Re: How to Draw a 'Line'...
Quote:
Originally Posted by
CuppaCoffee
Could you explain how, or show an example involving how to use Path2D.Double objects? It's sounds like it'd probably cover the problems I'm likely to have to deal with my algorithm
How about you take a stab at it and see what happens, then post an SSCCE if you get stuck? How are you using Rectangle2D now? I would predict you're just using it to get at two endpoints, which Line2D already has.