Results 1 to 8 of 8
  1. #1
    gtopcu is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Lightbulb Getting all mouse locations

    Hi all,

    I've been searching around the topic including this forum but haven't come across anything helpful.

    I'm basically trying to build the "brush" tool in my "paint" program, which I would like to act much like the one in MS Paint or any other paint program you can find.

    Apart from some other issues I'm trying to figure out, currently the major one is about painting on the canvas/image while the mouse is being "dragged".

    If I use mouse listeners to capture pressed/dragged/released events, the dragged method gets called by the EDT(event dispatching thread) with too large periods for a brush tool - since it fires MouseEvent e for a dragged point, it does not contain all the points that the mouse pointer has moved since the dragging started from the source point. Thus I get gaps between each brush stroke, the faster the mouse is dragged, the larger the gap. I wrote a trigonometric function that divides the line between source/target drag points to the needed number of points with each dx/dy increments in the direction from source to target, but this doesn't feel very convenient.

    I have also tried capturing Component.getMouseLocation() from a separate thread and invoking paints on the EDT using SwingUtilities.invokeAndWait(), and even created a second non-EDT thread later, one for capturing mouse location, the other for calling EDT to do painting, but still, gaps are too wide and the code is just getting more and more complicated & messy.

    I'm seriously hoping that I'm missing a simple something and this is really child's play. I appreciate all the ideas.

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Well. In response to just the title:
    Java, nor Windows, supports multiple mice.

    And to the text:
    The event if fired exactly for each move. If the caps are too wide the reasons can only be:
    Your are moving to mouse very fast.
    Your mouse settings make the mouse go very fast.
    Your imagination makes you think the gapes are too wide.
    Or; something is seriously wrong without your [physical] mouse.

    So the mouse does not move contiguously, pixel for pixel.
    It is updated when it is moved, and that move can be pixels wide,
    Java is not missing any updates, the [physical] mouse does not
    report often enough to cover each pixel between the locations.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

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

    Default

    Path2D and its methods curveTo, quadTo may help.

    db

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    The simple mathematics of [and description on] Bézier curves:
    Bézier curve - Wikipedia, the free encyclopedia
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  5. #5
    gtopcu is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Hibernate View Post
    Well. In response to just the title:
    Java, nor Windows, supports multiple mice.

    And to the text:
    The event if fired exactly for each move. If the caps are too wide the reasons can only be:
    Your are moving to mouse very fast.
    Your mouse settings make the mouse go very fast.
    Your imagination makes you think the gapes are too wide.
    Or; something is seriously wrong without your [physical] mouse.

    So the mouse does not move contiguously, pixel for pixel.
    It is updated when it is moved, and that move can be pixels wide,
    Java is not missing any updates, the [physical] mouse does not
    report often enough to cover each pixel between the locations.
    Well, non of those 4 points apply to my problem.

    Let me describe the requirement with an example. I'm using an Ellipse2D for the brush tool. Let's say the user is drawing on a 100x100 pixel canvas. He presses the mouse at [0, 0], and moves to the opposite corner of the canvas until he gets to the [100, 100] coordinate while holding down the mouse button (dragging), and releases exactly at [100, 100]. While doing so (although this is practically impossible), he follows the canvas rectangle's diagonal perfectly, so he moves through all the points from [0, 0], [1, 1], [2, 2]... so on until he gets to [100, 100]. If this is the case, the mouse listener's mouse dragged function gets called at different intervals based on the mouse speed. If the user is moving the mouse slowly, it can capture all the interim points, if not, the mouseDragged function gets called for some of these coordinates such as [0, 0], [10, 10], [35, 35], [48, 48], [67, 67], [75, 75], [81, 81], [96, 96] and finally [100, 100]. This is why I get gaps between each brush stroke. The user sees the mouse pointer move through all these points, but the listener is not called for every location the pointer passes through. That's why I tried using an endless loop to get the current mouse coordinate in a separate thread in an effort to capture all coordinates.

    Alternatively, I could draw a Line2D between all the interim point pairs instead of using the Ellipse2D for the tool, but we know that no user can drag the mouse from point a to point b with a perfect line, so the real brush effect would be lost this way. I know about the bezier curves/quads, but there's no 3rd reference coordinate that could be used to define the curvature and even if there was one, I would be facing the same issue as using the Line2D.

    I hope the issue is clear now, thanks.

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I think the problem is »Your imagination makes you think the gapes are too wide.».

    When you move the mouse it feels like the mouse moves smoothly from all (or at least most) of pixels,
    but in really it makes small (c:a 15 pixel) jumps.

    Did you notice that the first gap was 10 pixels width, followed by 15, 13, 19, 8, 6, 4.
    First the mouse speeds up, then speeds down.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  7. #7
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Test moving your mouse fast in a circle and you can see that it can make jumps around 100 pixels (and higher).
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  8. #8
    gtopcu is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Hibernate View Post
    I think the problem is »Your imagination makes you think the gapes are too wide.».

    When you move the mouse it feels like the mouse moves smoothly from all (or at least most) of pixels,
    but in really it makes small (c:a 15 pixel) jumps.

    Did you notice that the first gap was 10 pixels width, followed by 15, 13, 19, 8, 6, 4.
    First the mouse speeds up, then speeds down.
    Hey - I made those coordinates up myself just to describe the problem - the gaps could be the same or even increasing depending on the speed the user moves his mouse!

    So it has nothing to do with my imagination and my question is clear I believe. Any more ideas anyone?

Similar Threads

  1. file locations in Java EE Application
    By niteangell21 in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 05-07-2011, 06:46 PM
  2. Help Reading certain Address locations from file.
    By kriogenic in forum Java Applets
    Replies: 7
    Last Post: 11-05-2010, 12:45 PM
  3. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  4. Referencing Image Locations within Eclipse
    By haroldjclements in forum Eclipse
    Replies: 0
    Last Post: 03-23-2009, 02:34 PM
  5. Setting all locations at once in 2D arrays
    By Singing Boyo in forum New To Java
    Replies: 14
    Last Post: 03-22-2009, 10:49 PM

Tags for this Thread

Posting Permissions

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