Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: How to Draw a 'Line'...

  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default How to Draw a 'Line'...

    Just as the title says, I'm wondering how I could draw a line in Java, and by drawing a line, I don't mean a Line2D.Double object in the paint class. I'm talking about literally drawing a line with the mouse, like you would in Microsoft Paint.

    I was thinking about making an array of Rectangle2D.Double objects and then playing their x and y locations to where the mouse travels every 500 milliseconds.. Whenever I do this, though, the rectangle continuously just shows up over my mouse and doesn't leave a trail whatsoever.

    Any ideas how you would approach this issue?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default 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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to Draw a 'Line'...

    I moved this thread to the Java2D forum for closer topic alignment.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: How to Draw a 'Line'...

    Quote Originally Posted by KevinWorkman View Post
    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!
    Attached Thumbnails Attached Thumbnails How to Draw a 'Line'...-untitled.png  

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to Draw a 'Line'...

    Quote Originally Posted by CuppaCoffee View Post
    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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to Draw a 'Line'...

    Quote Originally Posted by KevinWorkman View Post
    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
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: How to Draw a 'Line'...

    Quote Originally Posted by CuppaCoffee View Post
    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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to draw a line?
    By Lund01 in forum New To Java
    Replies: 9
    Last Post: 10-27-2011, 06:09 PM
  2. How to draw line with animation
    By hnchnc in forum Java 2D
    Replies: 1
    Last Post: 11-18-2010, 10:49 PM
  3. Draw line
    By janes in forum Java 2D
    Replies: 6
    Last Post: 03-25-2010, 10:48 PM
  4. Draw more than one line on run time
    By aiman in forum Java Applets
    Replies: 3
    Last Post: 12-10-2009, 02:44 AM
  5. How to draw a thick line
    By johnt in forum Java 2D
    Replies: 1
    Last Post: 05-31-2007, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •