I need the mouse to update position even while the mouse is being pressed. This is my code so far and I'm not sure where to go from here to fix my problem:
This code is ran in my main loop and Mouse is a class I've created that implements MouseListener and MouseMotionListener that gets the position of the mouse coordinates and which mouse button was pressed.
When i press my mouse (I'm using a laptop, not sure if that matters or not) but it displays which button is pressed, which is good, but as I move my mouse around (with the button pressed) the white square that I have rendering where the mouse is, it just freezes up and doesn't move with the mouse. I have to release the mouse then move it again for it to get back where the mouse position is. Not sure what's going on here or how to fix it. I've been following java game tutorials on youtube from thecherno.
Graphics g = bs.getDrawGraphics(); // bs is a bufferstrategy
{
g.DrawImage(image, 0, 0, getWidth(), getHeight(), null); // my game image
g.setColor(Color.WHITE);
g.setFont(new Font("Verdana", 0, 20));
g.fillRect(Mouse.getX() - 32, Mouse.getY() - 32, 64, 64);
g.drawString("Button: " + Mouse.getButton(), 10, 30);
}
g.dispose();
bs.show();
My mouse class
package rain.input;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Mouse implements MouseListener, MouseMotionListener {
private static int mouseX = -1;
private static int mouseY = -1;
private static int mouseB = -1;
public static int getX() {
return mouseX;
}
public static int getY() {
return mouseY;
}
public static int getButton() {
return mouseB;
}
@Override
public void mouseDragged(MouseEvent e) {}
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
mouseB = e.getButton();
}
@Override
public void mouseReleased(MouseEvent e) {
mouseB = -1;
}
}