-3

In a Swing application when the mouse is moved within the frame, the actionPerformed method stops cycling. How can I fix this?

Here's the basic layout of my program:

ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            // main game loop
        }
}

public void paintComponent(Graphics g) {
    // render loop
}

I found a similar question here. The user found that by lowering the polling rate of the mouse they fixed the problem; however I cannot change the polling rate on my apple trackpad, and no other solutions were offered. Also it is an inelegant solution that would require the user to change settings, and honestly there has to be a better way to fix the problem.

Basically the question boils down to this:

  • Is there a way for me to change the polling rate from within my program? I did some research and couldn't find a solution.
  • How can I disable mouse movement events, so as to not slow down my game loop? (Also perhaps move it to a separate process, and use the mouses x and y position provided by that process for logic in the game loop.)
  • What alternate solution can I implement to fix this problem?
Community
  • 1
  • 1
Teddy
  • 11
  • 2

1 Answers1

0

I think you need to implement the "ActionListener" where you can take it, because when you are moving will work the ActionListener, when you will click, it will be already ActionEvent. Also you can get more from: https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html and How can I get the location of the mouse pointer in a JPanel (Without any operation of the Mouse)?

Community
  • 1
  • 1
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
  • I have tried that implementation, this isn't a question about how to get the mouse location, I'm asking how I can avoid the delay problem. One solution I found indicated that lowering the mouse polling rate fixed the problem, but as that is an inelegant solution that is not necessarily doable (i.e. on mac) I wanted to ask for another solution. I have looked and there is no way that I found to change the polling rate on a mouse or trackpad with java. – Teddy Apr 26 '17 at 17:57