Alright, so you’re writing a java application. Do you want to find out whether or not the mouse is accelerating? Or do you want to know exactly how many pixels the mouse accelerated by? Well here’s a quick and simple tutorial to show you how to do this.

(this can also be applied to anything that has an x or y value and you want to know if it has accelerated, or how much it has accelerated by)

First off, we’ll start from scratch if you’re new to java programming.

We are going to be needing to add some imports.

import javax.swing.*;
import java.awt.event.*;

Our application is going to be called AccelerationSample.java so we now are going to make the class. We want a JFrame so we are going to make it extend JFrame, which makes this class a type of JFrame. Same with MouseMotionListener and ActionListener, but instead we are going to use the word implement for those because they are abstract classes (don’t need to worry about that).

public class AccelerationSample
    extends JFrame implements MouseMotionListener, ActionListener
{
}

Now inside the class we are going to need a constructor as well as a few methods in order for this thing to even compile. Now you’ll notice that we have both mouseMoved and mouseDragged. We could use both but for this application we are only going to be using mouseDragged. However, because we have the class (program) implement MouseMotionListener, we have to have both mouseDragged AND mouseMoved

public static void main(String[] args)
{
}
public AccelerationSample()
{
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
}

Alright now compile that and everything should be fine. You could run it as well, but you’re not going to see anything. Now how about we start seeing something?

in main, we are going to create our JFrame, set it’s title, set it’s size, make it visible, and allow it to close. We do this by writing the following lines:

JFrame frame = new AccelerationSample();
frame.setTitle("Are You Accelerating?");
frame.setSize(800,600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Alright so now that we have the basic stuff done, we are going to get a bit more complicated. First off, we are going to write the variables that we are going to be using. This will be best put before the main() function, inside the class.

private double newX;
private double newY;
private double oldX;
private double oldY;
private double time;
private double accelX;
private double accelY;
private boolean xStop;
private boolean yStop;
private int score = 0;
private double amount = 100;
private Timer gameTimer;
private JLabel lblxAccel;
private JLabel lblyAccel;
private JLabel lblscore;

I know it’s a lot of variables that I’m not explaining right away, but don’t worry we’ll get to that. Now we are going to initialize all of these variables in the AccelerationSample() constructor. We are also going to be creating a JPanel, which we add the MouseMotionListener to.

public AccelerationSample()
{
  JPanel panel = new JPanel();
  panel.addMouseMotionListener(this);
  this.add(panel);

  newX = 0;
  newY = 0;
  oldX = 0;
  oldY = 0;
  accelX = 0;
  accelY = 0;
  time = 1;
  score = 0;
  amount = 100;

  lblxAccel = new JLabel();
  lblxAccel.setBounds(100,100,500,50);
  panel.add(lblxAccel);
  lblyAccel = new JLabel();
  lblyAccel.setBounds(100,150,500,50);
  panel.add(lblyAccel);
  lblscore = new JLabel();
  lblscore.setBounds(100,200,500,50);
  panel.add(lblscore);

  gameTimer = new Timer(100, this);
  gameTimer.start();
}

The timer we make has the argument 100. This means that the timer will “go off” every 10 milliseconds. If you compile again, it shouldn’t have any errors. If you run it, you’ll have a JFrame pop up with 3 labels that don’t have any text (so you won’t actually see them).

Now we are going to get into the acceleration section. We are going to start with the mouseDragged event. Every time the mouse is dragged, we are going to save the x and y co-ordinates of the mouse to variables.

public void mouseDragged(MouseEvent e)
{
  newX = e.getX();
  newY = e.getY();
}

Now for the final step. Every time the gameTimer goes off, the actionPerformed event is called. So every time we are going to calculate the acceleration of the mouse dragging movement. Acceleration is just displacement / time or (new – old) / time. Look familiar? So we are going to be doing that for the x and y positions of the mouse. Now we only want positive values for acceleration, so if it is negative (< 0) we are going to multiply it by -1, making it positive. Now we are going to use the labels to display our acceleration, and then lastly make the new values now the old values.

public void actionPerformed(ActionEvent e)
{
  if (e.getSource() == gameTimer) {
    accelX = (oldX - newX) / time;
    accelY = (oldY - newY) / time;

    if (accelX < 0)
      accelX *= -1;

    if (accelY < 0)
      accelY *= - 1;

    lblxAccel.setText("Horizontal Acceleration: " + accelX + " px/10ms");
    lblyAccel.setText("Vertical Acceleration: " + accelY + " px/10ms");

    oldX = newX;
    oldY = newY;
  }
}

We have now successfully calculated the acceleration for your program…but let’s just add a couple things to make it useful. Again in the actionPerformed, we are going see if the user accelerated more than 100 px/10ms. If they did, we are going to add 1 to their score. We use booleans to make it so they don’t keep adding to their score for one movement. This encourages the user to move back and forth, up or down. This can be useful in many games or apps. So until they change direction (slow down), the score wont be added by making the Stop booleans true if they slowed down and false if they already got a score for moving.

Put the following pre right after we make sure the accelerations are positive (accelY *= – 1;):

if (accelX <= 10)
  xStop = true;

if (accelY <= 10)
  yStop = true;

if (accelX > amount && xStop) {
  score++;
  xStop = false;
}

if (accelY > amount && yStop) {
  score++;
  yStop = false;
}

lblscore.setText( "Score: " + score);

And there you have it! If you have any feedback or questions about this tutorial, or even java in general, just leave us a comment.

Need this tutorial written for a different language than Java? Let us know and we will be happy to write it.

Click here to download the example.