To plot a graph of Speed and Time
Description: The idea of the task was to check if we could find a proper biometric
which could authenticate a user
by moving the mouse.The parameters which I have used in this deliverable include variation in
speed of different users while moving the mouse,
deviation from the straight line between two points and angle. After all these parameters are
calculated, a graph is drawn which plots
these parameters with respect to time.
In order to plot the graph, a program was written to record the mouse coordinates of the user.
An applet was created which shows some random points on the screen. The user has to move the
mouse to the points.
The mouse coordinates are recorded every time the timer is triggered. Using these mouse
coordinates, the speed of the
mouse motion is calculated with the duration of the timer as the time interval between
two consecutive points.
Deviation from a straight line between two points is calculated by finding the minimum
distance of that point from the straight line.
Finally the angle of deviation specifying whether the mouse moves above the straight line
or below is calculated.
Below are some of the graphs, which show the speed, deviation and angle of different users.
Example:This is what my code outputs on these inputs.
The examples above show the different measuring parameters that I am testing to check the
authenticity of any user.
Each graph is from a different user.Y axis shows the speed,deviation and angle. X axis gives
the time interval which is
50 ms for hese examples.Green color shows the speed, blue shows the deviation and black
colour the angle of deviation.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* Title:Program to calculate the velocity of mouse movements */
/* Author : Shivani Hashia */
/* Date : 9 Feb 2004 */
// This class creates a panel of buttons
class MousePanel extends JPanel implements ActionListener {
public MousePanel() {
String[] measure = { "10","50","100","250"};
d = new DrawArea();
style=measure[0];
JComboBox measureList= new JComboBox(measure);
measureList.setEditable(true);
// Adding action Listener to Combo box and getting the value of duration for timer
measureList.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent evt) {
JComboBox sel = (JComboBox)evt.getSource();
String item = (String)sel.getSelectedItem();
temp= Integer.parseInt(item);
DURATION=temp;
}
});
JPanel patternPanel = new JPanel();
patternPanel.add(measureList); // Adding buttons to the panel
add(patternPanel);
setLayout(new BorderLayout());
add(patternPanel,BorderLayout.WEST);
add (d, BorderLayout.CENTER);
patternPanel.setBackground(Color.white);
d.setBackground(Color.pink);
startButton = new JButton("Start");
stopButton = new JButton("Stop");
patternPanel.add(startButton);
patternPanel.add(stopButton);
startButton.addActionListener(this);// Adding action listener to buttons
stopButton.addActionListener(this);
} //end Constructer
//Calling function if startButton/stopButton is pressed
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
//System.out.println("duration before timer is "+ DURATION);
if (source==startButton) {
startButton.setEnabled(false);
d.startAction(DURATION);
}
if (source==stopButton) {
d.stopAction();
}
} // End actionPerformed
private JButton startButton,stopButton; //Defining variables
private int temp;String style;
private int DURATION;
private DrawArea d;
} //End class
// This class defines another panel where the points are drawn
class DrawArea extends Canvas {
public DrawArea () {
setBackground ( Color.pink );
XRand=op.setXRand(); //Storing the random value coordinates returned by Test class method
YRand=op.setYRand();
FindSlope(XRand,YRand);
} //End constructer
public void FindSlope(int [] XRand,int [] YRand) {
for(int n=0;n<11;n++)
YRand[n]=600-YRand[n];
double y = YRand[0] ;
double x = XRand[0] ;
//System.out.println(" x , y is" +x +", " + y);
slope[0]= (y / x) ; c[0] = 0;
System.out.println("Slope of YRand[0] , XRand[0] is " +slope[0] +", " +XRand[0]+ ", " +YRand[0]);
for(int n=1;n<10;n++)
{
y=(YRand[n]-YRand[n-1]); x=XRand[n]-XRand[n-1];
slope[n] = y/x;
c[n] = YRand[n]-(slope[n]*XRand[n]);
}
}
// Method to start the timer
public void startAction(int duration) {
flag= true; DURATION=duration;
// Initializing timer and adding mouse listener
timer = new Timer(duration, new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
addMouseMotionListener( new MouseMotionAdapter() {
public void mouseMoved(MouseEvent me) {
//Checking if mouse is on random coordinates
X=me.getX(); Y=me.getY();Y=600-Y;
if(((XRand[l]-5) <= X ) && (X< (XRand[l]+5)) && ((YRand[l]-5) <= Y)
&& (Y< (YRand[l]+5)))
{
mousereached = true;
P=XRand[l+1];Q=YRand[l+1];
repaint(); l=l+1;
if(l==10) stopAction();
}
}
});
XCoor[i]=X; YCoor[i]=Y;
X1=((X+slope[l]*YCoor[i]-slope[l]*c[l])/(1+slope[l]*slope[l]));
Y1= (slope[l]*X1 +c[l]);
double q=Math.sqrt(((X-X1)*(X-X1) )+((Y-Y1)* (Y-Y1)));
if(l<10) Deviation[i] = q;
if(l==0)
{
double A= (double)(Math.sqrt(((X1*X1)+(Y1*Y1))/Deviation[i]));
double B= (double)(Math.sqrt((((X1-XRand[0])*(X1-XRand[0])) +
((Y1-YRand[0])*(Y1-YRand[0])))/Deviation[i]));
Angle[i]= Math.atan(A)+Math.atan(B);Angle[i]= Angle[i]*(180)/PI;
if((Y-slope[l]*X-c[l])<0) Angle[i]=-Angle[i];
}
else
{
double A= (double)(Math.sqrt((((X1-XRand[l-1])*(X1-XRand[l-1])) +((Y1-YRand[l-1])
*(Y1-YRand[l-1])))/Deviation[i]));
double B= (double)(Math.sqrt((((X1-XRand[l-1])*(X1-XRand[l-1])) +((Y1-YRand[l])
*(Y1-YRand[l])))/Deviation[i]));
Angle[i]= Math.atan(A)+Math.atan(B);Angle[i]= Angle[i]*(180)/PI;
if((Y-slope[l]*X-c[l])<0) Angle[i]=-Angle[i];
}
cntr=i;
i++;
}
});
timer.start();
}
// Method to stop the timer, calculating speed and drawing
//a graph by calling win class functions
public void stopAction()
{ timer.stop();
Speed=CalculateSpeed(XCoor,YCoor);
// Calling Win1 class function to form the graph
Win1 wingraph = new Win1(Speed,counter,DURATION,Deviation,Angle);
if (graph.isVisible()) graph.setVisible(false);
else graph.show();
} // end stopAction
public double [] CalculateSpeed(int[] XCoor,int [] YCoor){
i=0; while((XCoor[i]==0) && (YCoor[i]==0)||(XCoor[i]==0))
{i++;
m=i;}
for(i=m,j=0;i<cntr;i++,j++)
{ XCoor[j]=XCoor[i];
YCoor[j]=YCoor[i];
Deviation[j]=Deviation[i];
Angle[j]=Angle[i];
}
// Calculating distance travelled by mouse
for(int i=0;i<cntr;i++)
{
j=i+1;
XDist[k]= (XCoor[j]-XCoor[i])*(XCoor[j]-XCoor[i]);
YDist[k]= (YCoor[j]-YCoor[i])*(YCoor[j]-YCoor[i]);
Dist[k]= Math.sqrt(XDist[k]+YDist[k]);
j++;
k++;
counter=k;
} // Calculating speed of mouse
for(int k=0;k<counter;k++)
{
Speed[k] = Dist[k]/DURATION;
}
return Speed;
}
// Paint method draws the points on canvas
public void paint ( Graphics g)
{
String msg="";
g.drawString(
"1) Press Start.
2) Follow the dots as they appear on screen with the mouse and press stop after 10 dots",10,12);
g.setColor(Color.green) ;
g.fillOval(XRand[0] ,(600-YRand[0]),7,7);
msg=Integer.toString(1);
g.drawString(msg,XRand[0]+10,(600-YRand[0])+10);
g.setColor(Color.blue);
if( (mousereached==true) && (P!=0) && (Q!=0))
{
g.fillOval(P,(600-Q),5,5);
msg=Integer.toString(t+1);
g.drawString(msg,P+10,(600-Q+10)); t++;
}
mousereached = false;
}
Test op = new Test();
public int[] XRand = new int[11];
public int[] YRand = new int[11];
private static double[] slope = new double[12];
private static boolean mousereached = false;
private static double[] c= new double[12];
private static int DURATION;
private final static double PI=3.1415;
private static int P,Q;
private final static int MAX =2000;
private Timer timer;
private static int X,Y,cntr=0,counter=0,l=0,m=0;
private int t=1,i=0,j=0,k=0;
private static double X1,Y1;
private int[] XCoor = new int[MAX];
private int[] YCoor = new int[MAX];
private int[] XDist = new int[MAX];
private int[] YDist = new int[MAX];
private static double [] Angle = new double[MAX];
private double[] Dist = new double[MAX];
private static double[] Deviation = new double[MAX];
private static double[] Speed = new double[MAX];
private JFrame graph = new GraphFrame();
}
// Class to create graph
class GraphFrame extends JFrame
{
public GraphFrame()
{ setTitle("Graph");
setSize(900, 700);
setBackground ( Color.white );
Container contentPane = getContentPane();
contentPane.add(new Win1());
}
}
public class MouseMovement12 extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.add(new MousePanel());
}
}
|