The java eclipse eventsystem only works when there is a print function

Bernd Koggel :

I have a class that handles all the mouse events, this way it's very easy to check if a mouse button is clicked, but it seems to only work when I add this line: system.out.println(mouseManager.getMouseX());.

The MouseManager class:

public class MouseManager implements MouseListener, MouseMotionListener {

    private boolean leftPressed, rightPressed;
    private int mouseX, mouseY;

    public MouseManager() {

    }

    public boolean isLeftPressed() {
        return leftPressed;
    }

    public boolean isRightPressed() {
        return rightPressed;
    }

    public int getMouseX() {
        return mouseX;
    }

    public int getMouseY() {
        return mouseY;
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            leftPressed = true;
        }
        else if (e.getButton() == MouseEvent.BUTTON3) {
            rightPressed = true;

        }
    }

}

As you can see the MouseManager class is very simple, but it's used to get called within other classes to make mouse inputs easier. But in the Visuals class (which isn't an appropriate name, it's more of the main class) things go wrong.

public void Update() {
    if (mouseManager.isLeftPressed()) {
        Vector2 posClicked = GetRoundedMousePos();
        int index = GetPosInArray(posClicked);
        System.out.println(Grid.cells.get(index).cellType.toString());

        if (Grid.cells.get(index).cellType == Cell.CellType.GROUND) {
            Grid.cells.get(index).cellType = Cell.CellType.SAND;
            repaint();
        }
    }
    System.out.println(mouseManager.getMouseX());
}

As you can see I added prinln to make sure it works.

NOTE:

  • I only show the Update method, because the whole script is too long
  • The Update method is called every frame
  • mouseManager is declared above with this line: private static MouseManager mouseManager = new MouseManager();

Without System.out.println(mouseManager.getMouseX()); at the bottom of Update, mouseManager.isLeftPressed() doesn't work. So how to fix that?

AND this may be related to the bug above, but even with the println() function the mouseManager seems to not always detect mouse presses.

drekbour :

Mouse updates are occurring on another Thread and there is no explicit handling for this. Volatile fields are probably all you need here given the uni-directional flow of information from MouseManager to caller:

private volatile boolean leftPressed, rightPressed;
private volatile int mouseX, mouseY;

As an aside, you should look at replacing that static class-field with a final instance-field (and further, passing it in as a constructor argument):

private final MouseManager mouseManager = new MouseManager();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357932&siteId=1