Results 1 to 11 of 11

Thread: Holding mouse

  1. #1
    d2r123 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Holding mouse

    Hi, I wish to write a java program that runs on an undecorated jFrame that detects mouse events.
    I already have one of these but I have run into a problem.

    I have a sub-program that essentially functions like a sliding bar. After you click on it, it begins to track the location of the mouse using a thread. If you let go of the mouse or move the mouse across the screen, as opposed to dragging, it ends the thread. The problem is that the thread canít tell when the mouse is released until after the mouse re-enters the JFrame. Because of this, the sliding barís position keeps changing and if you were to let go and then repress the mouse outside of the screen, the screen would continue to run the thread.

    I have two ideas that may let me get around this. First, I could set the thread to automatically end if the mouse exits but I would rather not have the mouse accidentally let go all the time.
    I could also set the thread to end when focus is lost but the thread would continue to track the unheld button until it was clicked again I think.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default Re: Holding mouse

    Print out all the mouse events that happen and see if any of them have any info that will help detect when the desired condition/event happens.

  3. #3
    d2r123 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Holding mouse

    That's the issue, there aren't any such events being triggered. Events only trigger while inside of the frame. I only know where the mouse is because of the thread that is constantly watching it's possition and the MouseInfo.getPointerInfo() thing doesn't seem to tell me anything about the mouse button.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Holding mouse

    Wehn does the mouseReleased(...) event get triggered?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default Re: Holding mouse

    If there are no events generated how can you tell when a state has changed?

  6. #6
    d2r123 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Holding mouse

    While it's in the JFrame, the events work just fine and mouse relesed(..) get's triggered when the mouse is released.
    Once it leaves the JFrame, there doesn't seem to be a way to trigger the event or to simply read the information from the mouse using toolkit.

    After you click on the bar, I want the thread to continue to pass information to the bar until the mouse is released. I am the one asking you how to detect the mouse state.

    On a related note, it it possable to create a transparent JFrame or other window and if you can, can java produce a mouse click?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default Re: Holding mouse

    Once it leaves the JFrame, there doesn't seem to be a way to trigger the event
    Can you write a small program that shows the problem?
    I just wrote a 12 line program that shows a mouse press and while I'm holding the press I move the mouse off the frame and release the mouse, the mouseReleased method is called.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Holding mouse

    Quote Originally Posted by d2r123 View Post
    While it's in the JFrame, the events work just fine and mouse relesed(..) get's triggered when the mouse is released.
    Once it leaves the JFrame, there doesn't seem to be a way to trigger the event or to simply read the information from the mouse using toolkit.
    Test first, post later. Don't guess.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    d2r123 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Holding mouse

    Well, I created a simpler case for it and it seems to be functioning differently.
    I don't know where in the jumble of code on my computer I managed to cut out this function, but I did. I wasn't guessing, I actualy managed to screw myself out of that function somehow. I will start looking now.
    Thank you for your time, sorry for squandering it.
    Java Code:
    import javax.swing.JFrame;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseEvent;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Point;
    import java.awt.MouseInfo;
    public class example extends JFrame implements MouseListener, MouseMotionListener, Runnable
    {
      public boolean A;
      public int var;
      public int anchorBoxx;
      public int anchorBoxy;
      public int anchorMousex;
      public int anchorMousey;
      public static void main(String[] args)
      {
        example v = new example();
        v.var = 100;
        v.setLocation(200, 200);
        v.setSize(200, 100);
        v.setDefaultCloseOperation(EXIT_ON_CLOSE);
        v.addMouseListener(v);
        v.addMouseMotionListener(v);
        v.setResizable(false);
        v.setUndecorated(true);
        v.setVisible(true);
      }
      
      public void paint(Graphics g)
      {
        int n = getWidth();
        int m = getHeight();
        g.setColor(new Color(255,255,255,255));
        g.fillRect(0,0,n,m);
        g.setColor(new Color(0,0,0,255));
        g.fillRect(0,0,((n*var)/200),m);
      }
      public void update(Graphics g)
      {
        int n = getWidth();
        int m = getHeight();
        g.setColor(new Color(255,255,255,255));
        g.fillRect(0,0,n,m);
        g.setColor(new Color(0,0,0,255));
        g.fillRect(0,0,((n*var)/200),m);
      }
      
      public void mouseClicked(MouseEvent e){}
      public void mousePressed(MouseEvent e)
      {
        A = true;
        Point p  = MouseInfo.getPointerInfo().getLocation();
        anchorMousex = p.getLocation().x;
        anchorBoxx = e.getX();
        var = ((200*anchorBoxx)/(getWidth()));
        repaint();
        new Thread(this).start();
      }
      public void run()
      {
        while(A)
        {
          Point p  = MouseInfo.getPointerInfo().getLocation();
          int xl = anchorBoxx+p.getLocation().x-anchorMousex;
          var = (int)((200*xl)/(getWidth()));
          if(var < 0)
          {
            var = 0;
          }
          else if(var > 200)
          {
            var = 200;
          }
        }
      }
      public void mouseReleased(MouseEvent e){A = false;System.out.println("ggg");}
      public void mouseDragged(MouseEvent e)
      {
        repaint();
      }
      public void mouseMoved(MouseEvent e){A = false;}
      public void mouseEntered(MouseEvent e)
      {
        repaint();
      }
      public void mouseExited(MouseEvent e){}
    }
    Last edited by d2r123; 03-13-2012 at 10:54 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default Re: Holding mouse

    What happens when you compile, execute and test this code?

  11. #11
    d2r123 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Holding mouse

    It works exactly how I want it to. The mouse events continue to be triggered as long as you hold the mouse.

    I repeate, I managed to screw myself out of this function in my larger program and I will now go figure out what I did wrong. Thread concluded.

Similar Threads

  1. Creating a file is Holding up java.
    By rizowski in forum New To Java
    Replies: 0
    Last Post: 04-20-2011, 06:47 PM
  2. Replies: 8
    Last Post: 11-23-2010, 12:03 PM
  3. Mouse
    By PhQ in forum AWT / Swing
    Replies: 18
    Last Post: 08-18-2010, 05:05 PM
  4. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 05:34 AM
  5. Mouse events, are they best or only way to go?
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-10-2009, 05:34 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
  •