Mouse cursor not changing possition after being moved - java

So, I am trying to make a program, and for it to start, i need the user to click on a start button, which is a square on the upper left-hand side of the screen, my way of seeing if the cursor is in the button is: checking the mouse's coordinates, seeing if those coordinates are inside the button, and reacting accordingly (starting the program or not), but, when I start the program, it just takes the mouse's coordinates and doesn't change them when i move them, so, for example, if the mouse starts on x:0 y:0, and i move it to x:100 y:100 and then click, the program thinks it's on 0,0 and doesn't do anything even though the cursor is in the button, and, as far as I can see, there's not much I can do (that's why I am asking here) The code that makes this happen is this: (the button doesn't start anything yet, it just changes the text on screen)
public class MouseHandler implements MouseListener
{
#Override
public void mouseClicked(MouseEvent e)
{
if(mX < 200 && mY < 200 && mX > 100 && mY > 100)
{
textLabel.setText("button pressed");
} else {
textLabel.setText("You're not pressing the button" );
}
}

Related

LWJGL: detecting left/right clicks when getButtonCount returns 0

On the Dell Inspiron 15 3000, the touchpad doesn't have any physical left/right buttons. Instead, it is one giant touchpad that is pressure sensitive. I'm assuming it detects right/left clicks based off of hand position on the trackpad.
In my LWJGL application, I detect mouse button clicks with Mouse.isButtonDown(0). This works fine on computers with a mouse with physical buttons, but doesn't work on touchpads that lack physical buttons. Mouse.getButtonCount() returns 0.
Has anybody had any success in detecting if a mouse button is pressed, should the user be using a trackpad that doesn't have physical buttons?
I think, instead of using
org.lwjgl.input.Mouse
This class could be what you are searching for:
org.lwjgl.input.Controllers
http://legacy.lwjgl.org/javadoc/org/lwjgl/input/Controllers.html
Im not entirely sure though since I only have a mouse and no way to test this with a touchpad.
For those who find this in the future, I did find a fix:
You cannot, should there be no physical buttons, use the Mouse.isButtonDown() method. Instead, you are going to have to read the event buffer. To do so, I wrote my own helper class:
public class Mouse{
private static boolean button_left = false, button_right = false;
public static boolean isButtonDown(int button){
if(button == 0) return button_left;
if(button == 1) return button_right;
return false;
}
public static void update(){
while(org.lwjgl.input.Mouse.next()){
if(org.lwjgl.input.Mouse.getEventButton() == 0) button_left = org.lwjgl.input.Mouse.getEventButtonState();
if(org.lwjgl.input.Mouse.getEventButton() == 1) button_right = org.lwjgl.input.Mouse.getEventButtonState();
}
}
}
The update() method is called every tick, and as such I can get button states using Mouse.isButtonDown(button).

JOptionPane Infinite Loop

I have a program that accepts a user's mouse click and uses the coordinates of the mouse to draw a series of hexagons. I have drawn on my JPanel a red rectangle that is supposed to represent the bounds the user is allowed to click in, so to make sure the user does, I've included a warning message that pops up whenever the user clicks out of bounds. It says, "Cannot click outside red rectangle. Try again."
After the message pops up, I want the user to be able to dismiss the message and try again, but whenever the user clicks "OK" or "X" in the upper-right corner, the message appears again, and I can't seem to close the pop-up box. Is there another way I can inform the user while letting them try again?
Below is my code for a MouseListener I've implemented for my JPanel, hexPanel.
hexPanel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
boolean clicked = false;
int left = HexDrawingPanel.iX(-rWidth/2), right = HexDrawingPanel.iX(rWidth/2);
int top = HexDrawingPanel.iY(rHeight/2), bot = HexDrawingPanel.iY(-rHeight/2);
while(!clicked) {
if(!(e.getX() > right || e.getX() < left ||
e.getY() < top || e.getY() > bot))
clicked = true;
else
JOptionPane.showMessageDialog(frame, "Must click inside red rectangle. Try again.");
}
HexDrawingPanel.radius = (float)Math.sqrt(Math.pow(e.getX() - left, 2) + Math.pow(e.getY() - top, 2));
hexPanel.repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
Your while-loop is blocking the Event Dispatching Thread, meaning that's impossible for any new mouse clicks from ever occurring...
It might be better to do something more like...
if(!(e.getX() > right || e.getX() < left ||
e.getY() < top || e.getY() > bot)) {
HexDrawingPanel.radius = (float)Math.sqrt(Math.pow(e.getX() - left, 2) + Math.pow(e.getY() - top, 2));
hexPanel.repaint();
} else
JOptionPane.showMessageDialog(frame, "Must click inside red rectangle. Try again.");
}
There are a couple of ways to do this:
You could make the listener stateful, setting a global flag before the option pane is displayed and ignoring mouse clicks until the flag is unset.
You could put a JLabel above the box and show (colored?) status messages there instead of using a dialog.

Create Java 2D gravity?

I'm creating java game (I'm a beginner with this for now) and I'd like to start with some kind of platform game.
I'd like to know how I can make the player jump (I know how to move him up and down), but I don't know how how to make him go back down after going up.
Here is my code:
public void keyPress() {
if (listener.arrowUp) {
Jump();
}
}
private void Jump() {
if(player.get(1).getPosY() > maxJump) {
player.get(1).moveY(-10);
} else if(player.get(1).getPosY() == maxJump) {
player.get(1).moveY(85);
}
}
So.. the player moves -10px upwards as long as i press 'w' and when he hits maxJump (which is 375 and players position at the start is 465) he "teleports" back to 465 instead of sliding back down like he does when going up.. It's really hard to explain this without a video, but i hope somebody understands and can help me with this.
This question gives a basic answer to yours. Now in your jump function, you have to set the vertical_speed only once and only call fall() every frame and add some moveY.
Here are two alternatives:
Option 1. Simulating some very simple physics. You add forces to your player, so pressing the up arrow will add a force that makes the player move upwards. Gravity is constantly applied and thus it will gradually cancel out the force you applied when the up arrow was pressed. Perhaps you only want to use forces in the vertical direction you do something like this:
// Each frame
if(notOnTheGround){
verticalVelocity -= GRAVITATIONAL_CONSTANT;
}
// When user jumps
vertivalVelocity += JUMP_FORCE;
Option 2. An alternative is to kind of animate the jumps using projectile motion.
// Each frame
if(isJumping){
verticalVelocity = initialVelocity*sin(angle) - g*animationTime;
animationTime += TIME_STEP;
}
// When user jumps
isJumping = true;
animationTime = 0;
initialVelocity = ... // calculate initial velocity

Detecting mouse movement for a 3D first person world in Java

I am working on a first person game in Java, and I am trying to get the 3D movement working.
My problem is I would like to capture mouse movement, yet keep the mouse inside the window. After I capture the mouse movement, I figure the best way to keep the mouse in my window is to center the mouse in the window after moving, using Robot.moveMouse(x,y). This works fine, however the movement from the Robot triggers an event in my window which then gets interpreted as a normal event, and thus moves my character in the world.
I've tried various schemes of keeping state and ignoring movements until I am in the center, but they all seem finicky and don't quite detect which events are user vs Robot controlled.
Is there an easy way to detect that a mouse movement came from the Robot?
Is there perhaps a simpler way to solve my problem that I am overlooking?
I solved this by switching to NEWT with JOGL 2.0 RC4. In particular, I use GLWindow and warpPointer instead of an AWT Frame with the Robot.mouseMove. With the switch, I instantly got smooth movements. Some sample code similar to what I'm doing (mileage may vary):
public class MyClass implements MouseListener {
private GLWindow window;
private int centeredX = -1;
private int centeredY = -1;
// ...
public void mouseMoved(MouseEvent e) {
if (centeredX == -1 || centeredY == -1) {
center();
return;
}
int deltaX = e.getX() - centeredX;
int deltaY = e.getY() - centeredY;
// ... Do something with the deltas
centeredX = window.getWidth() / 2;
centeredY = window.getHeight() / 2;
window.warpPointer(centeredX, centeredY);
}
}
Well, I'm not 100% about this, but have you used the getsource() or getComponent() functions on your mouse event? They may return the robot as the source of it. Barring that, I would have a class variable like boolean robotControlling and anytime it takes control of the mouse, set that to true. Then, in you mouseListener, do a if(!robotControlling){...}. Hope this helps.
EDIT: if you have unused mouse buttons in your application (Java has Button 1, Button 2 and Button 3), you could make the robot press that, and in your mouse listener ignore any events with that code pressed. (use evt.getButton() for this) Of course, thats not exactly the cleanest solution :P

Mouse Events taking random number of clicks in java

The object of the code so far is the trade turns back and forth between player 1 and player 2 and allow the player whoose turn it is to turn one of their pieces invisible (Set icon to null). It works right now, turns trade back and forth and pieces turn invisible on click, but sometimes it is not the first click. It might take 3 or 4 clicks on a correct piece before it changes to null. Is there a reason this would be happening?
Robo2 is the icon for the first players pieces, robo1 is the icon for the second players pieces. The pieces are stored in an array of JButtons in the program with the icon set as the image of player 1 or player 2 piece.
public void mouseClicked(MouseEvent me) {
JButton clicked = (JButton)me.getSource();
if (player1) {
if (clicked.getIcon() == Robo2) {
clicked.setIcon(null);
player1 = false;
player2 = true;
}
else {
}
}
else if (player2) {
if (clicked.getIcon() == Robo1) {
clicked.setIcon(null);
player1 = true;
player2 = false;
}
else {
}
}
}
Figured out a solution, changing the mouse listener to an action listener solved the missing clicks problems. Using the events sent when the button gets clicked rather than detecting clicks themselves on the button. Thanks for the help.
when you double-click (or triple-click, or quadruple-click) something in Java you get this:
1st click: MouseEvent, clickCount = 1
2nd click: MouseEvent, clickCount = 2
3rd click: MouseEvent, clickCount = 3
etc.
so imagine you're double clicking the button by player1. The first event would change the player to player 2; the second event would change it right back to player1!
To remedy this situation - check clickCount (me.getClickCount()) and ignore the event if it isn't 1. Like
if (me.getClickCount() > 1) {
return;
}
// or else proceed as you do now

Categories